Solving a Nonlinear Equation using Newton-Raphson Method

4 ビュー (過去 30 日間)
민석 최
민석 최 2022 年 4 月 15 日
コメント済み: 민석 최 2022 年 4 月 16 日
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...

採用された回答

Torsten
Torsten 2022 年 4 月 15 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNewton-Raphson Method についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by