Newton Raphson method for a system of non-linear equations?

216 ビュー (過去 30 日間)
Ali Almakhmari
Ali Almakhmari 2023 年 2 月 12 日
コメント済み: Torsten 2023 年 2 月 12 日
If I have a non-linear system of two equations that are each in terms of two variables x and y:
eqn1(x,y) = 0
eqn2(x,y) = 0
How will I go about solving those two equations using Newton-Raphson in MATLAB? I know how to solve this system using the MATLAB's Solve function but not Newton-Raphson.

採用された回答

Alan Stevens
Alan Stevens 2023 年 2 月 12 日
Here's a simple example for you to follow:
% Functions
f = @(x,y) x^2 + y - 5.94; % i.e. the equation is x^2+y=5.94
g = @(x,y) x + y^2 - 3.41; % similarly this represents equation x+y^2=3.41
F = @(x,y)[f(x,y); g(x,y)];
% Jacobian
% J = [df/dx, df/dy;
% dg/dx, dg/dy];
J = @(x,y) [2*x 1; 1 2*y];
% Initial guess
xy = [1; 1];
% Tolerance
tol = 10^-6;
err = 1;
while err>tol
xynew = xy - J(xy(1),xy(2))\F(xy(1),xy(2));
err = abs(xynew-xy);
xy = xynew;
end
disp(xy)
2.2000 1.1000
  3 件のコメント
Alan Stevens
Alan Stevens 2023 年 2 月 12 日
Since Newton-Raphson is essentially numerical it seems pointless to use symbolics at all!
However, you might try J = @(x,y) j(x,y)
I don't have the symbolic toolbox, so can't actually check!
Torsten
Torsten 2023 年 2 月 12 日
% Functions
syms x y
f = x^2 + y - 5.94; % i.e. the equation is x^2+y=5.94
g = x + y^2 - 3.41; % similarly this represents equation x+y^2=3.41
F = [f; g];
% Jacobian
% J = [df/dx, df/dy;
% dg/dx, dg/dy];
J = jacobian(F);
F = matlabFunction(F)
F = function_handle with value:
@(x,y)[y+x.^2-2.97e+2./5.0e+1;x+y.^2-3.41e+2./1.0e+2]
J = matlabFunction(J)
J = function_handle with value:
@(x,y)reshape([x.*2.0,1.0,1.0,y.*2.0],[2,2])
% Initial guess
xy = [1; 1];
% Tolerance
tol = 10^-6;
err = 1;
while err>tol
xynew = xy - J(xy(1),xy(2))\F(xy(1),xy(2));
err = abs(xynew-xy);
xy = xynew;
end
disp(xy)
2.2000 1.1000
disp(F(xy(1),xy(2)))
1.0e-15 * 0.8882 0

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by