Solving a Nonlinear Equation using Newton-Raphson Method
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I tried making codes that solving a nonlinear equ using Newton-Raphson method but there is an error that 'Index exceeds the number of array elements. Index must not exceed 1.' at line 3 'df1dx1 = (u(x(1)+del*x(1),x(2))-u(x(1),x(2)))/(del*x(1)); ' But Idk why there's an error and how to fix it. I think I did well. how to fix it? There's my code below
function [J,f]=jfreact(x)
del = 0.000001;
df1dx1 = (u(x(1)+del*x(1),x(2))-u(x(1),x(2)))/(del*x(1));
df1dx2 = (u(x(1),x(2)+del*x(2))-u(x(1),x(2)))/(del*x(2));
df2dx1 = (v(x(1)+del*x(1),x(2))-v(x(1),x(2)))/(del*x(1));
df2dx2 = (v(x(1),x(2)+del*x(2))-v(x(1),x(2)))/(del*x(2));
J=[df1dx1 df1dx2; df2dx1 df2dx2];
f1=u(x(1),x(2));
f2=v(x(1),x(2));
f=[f1;f2];
end
function f = u(x,y)
f = y+x^2-x-0.75;
end
function f = v(x,y)
f = y+5*x*y-x^2;
end
--------------From here, this is a code that find the roots (initial values are all zeros, iteration is 3)
clear;clc;
format short e;
x=[0;0];
for i=1:3
[J,f]=jfreact(x);
x=x-J\f;
end
------------I'd like to know how to run well.... Please help me...
0 Commenti
Risposta accettata
Torsten
il 15 Apr 2022
x=[0;0];
for i=1:3
[J,f]=jfreact(x);
x=x-J\f
end
function [J,f]=jfreact(x)
del = 0.000001;
df1dx1 = (u(x(1)+del,x(2))-u(x(1),x(2)))/del;
df1dx2 = (u(x(1),x(2)+del)-u(x(1),x(2)))/del;
df2dx1 = (v(x(1)+del,x(2))-v(x(1),x(2)))/del;
df2dx2 = (v(x(1),x(2)+del)-v(x(1),x(2)))/del;
J=[df1dx1 df1dx2; df2dx1 df2dx2];
f1=u(x(1),x(2));
f2=v(x(1),x(2));
f=[f1;f2];
end
function f = u(x,y)
f = y+x^2-x-0.75;
end
function f = v(x,y)
f = y+5*x*y-x^2;
end
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!