Non linear system by Newton-Raphson

24 views (last 30 days)
Hey, I m trying to solve a non-linear system by Newton-Raphson method with an iteration max of 10 and 10^(-8) max error.
It 's my 1st time using matrices in Matlab and i think i don t get it right because my answer shoul be somewhat around x=0,88 y=0,99
I would love to know what is going wrong in my code ( I hope my derivatives are right (they should be))
thanks !
f1=@(x,y) atan(y)-y.*x.^2;
f2=@(x,y) x.^2+y.^2-2*x;
x1 = 1;
y1 = 1;
P = [x1 ; y1];
F= @(P) [atan(y1)-y1.*x1.^2 ; x1.^2+y1.^2-2*x1];
JF = @(P) [-2.*x1.*y1 (1/(1+y1.^2))-(x1.^(-2)) ; 2.*x1-2 2.*y1];
error = 1;
k = 1;
while k<10 && error > 10^(-8)
Q = P-JF(P)\F(P);
error = abs( norm(Q-P)/norm(Q+eps));
P=Q;
k=k+1;
end
P

Accepted Answer

Rohit Kulkarni
Rohit Kulkarni on 13 Mar 2024
Hi Grégoire,
It seems like there's a small mistake in how you're handling the variables within your functions `F` and `JF`. Specifically, you're using `x1` and `y1` directly inside these functions, which means they're not actually using the updated values of `P` (i.e., the current guesses for `x` and `y`) in each iteration of your loop. Instead, they keep using the initial values of `x1` and `y1` that you set before the loop.
To fix this, you should modify your functions `F` and `JF` to use the current values of `x` and `y` from `P` in each iteration. You can do this by extracting `x` and `y` from `P` inside these functions. Here's how you can adjust your code:
f1=@(x,y) atan(y)-y.*x.^2;
f2=@(x,y) x.^2+y.^2-2*x;
% Initial guess
x1 = 1;
y1 = 1;
P = [x1 ; y1];
% Define F using the current values of P
F= @(P) [atan(P(2))-P(2).*P(1).^2 ; P(1).^2+P(2).^2-2*P(1)];
% Define JF also using the current values of P
JF = @(P) [-2.*P(1).*P(2), (1/(1+P(2).^2))-P(1).^2 ; 2.*P(1)-2, 2.*P(2)];
error = 1;
k = 1;
while k<10 && error > 10^(-8)
Q = P-JF(P)\F(P); % Calculate the next approximation
error = abs(norm(Q-P)/norm(Q+eps)); % Update the error
P = Q; % Update the point for the next iteration
k = k + 1;
end
% Display the final approximation
disp(P);
0.8873 0.9936
Changes made:
1. `F` and `JF` now take `P` as their input and correctly use `P(1)` and `P(2)` to represent `x` and `y`, respectively, ensuring they use the updated values in each iteration.
2. Adjusted the derivative in the Jacobian matrix `JF`. The partial derivative of `f1` with respect to `y` should be `1/(1+y^2) - x^2`, not `(1/(1+y^2))-(x.^(-2))`. The latter would imply `x^(-2)` which is not correct according to the original function `f1`. The corrected form is directly `1/(1+y^2) - x^2` without subtracting `x.^(-2)`.
With these corrections, your code should now correctly implement the Newton-Raphson method for solving the given system of non-linear equations, and you should get an answer close to `x=0.88, y=0.99` as expected.
Hope this resolves your query!
Thanks
  1 Comment
Grégoire
Grégoire on 13 Mar 2024
Hey Rohit,
Thank you for taking the time to explain everything it is super clear :)
Have a nice day !

Sign in to comment.

More Answers (1)

Torsten
Torsten on 13 Mar 2024
x1 = 1;
y1 = 1;
P = [x1 ; y1];
F= @(x,y) [atan(y)-y.*x.^2 ; x.^2+y.^2-2*x];
JF = @(x,y) [-2.*x.*y (1/(1+y.^2))-(x.^(-2)) ; 2.*x-2 2.*y];
error = 1;
k = 1;
while k<10 && error > 10^(-8)
Q = P-JF(P(1),P(2))\F(P(1),P(2));
error = abs( norm(Q-P)/norm(Q+eps));
P=Q;
k=k+1;
end
P
P = 2×1
0.8873 0.9936

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!