# I got stuck with a loop after the value gets to NaN

4 views (last 30 days)
alexander fastiggi on 7 Sep 2021
% Newton-Raphson
clear; clc
% Symbolic math to compute the derivative
syms x y
u = x^2+x*y-10;
v = y+ 3*x*y^2-57;
dudx=diff(u,x);
dudy=diff(u,y);
dvdx=diff(v,x);
dvdy=diff(v,y);
% Covert symbolic functions into regular functions with inputs of (x,y)
u = matlabFunction(u);
v = matlabFunction(v);
dudx = matlabFunction(dudx,'Vars',[x y]);
dudy = matlabFunction(dudy,'Vars',[x y]);
dvdx = matlabFunction(dvdx,'Vars',[x y]);
dvdy = matlabFunction(dvdy,'Vars',[x y]);
% Initial guess for the root
xr=1;
yr=1;
% Maximum number of steps
N=1000;
tol=1e-5;
% Plot function
x=linspace(-5,5,100);
y=linspace(-5,5,100);
U=zeros(100,100);
V=zeros(100,100);
for i=1:100
for j=1:100
U(i,j)=u(x(i),y(j));
V(i,j)=v(x(i),y(j));
end
end
figure(1); clf(1);
% Plot u(x,y)
surf(x,y,U')
hold on
% Plot v(x,y)
surf(x,y,V')
% Iterate
for i=1:1000
% Store the old value
xro=xr;
yro=yr;
% Update xr & yr
xr=xro-(u(xro,yro)*dvdy(xro,yro)-v(xro,yro)*dudy(xro,yro)) ...
/ (dudx(xro,yro)*dvdy(xro,yro)-dudy(xro,yro)*dvdx(xro,yro));
yr=yro-(v(xro,yro)*dudx(xro,yro)-u(xro,yro)*dvdy(xro,yro) ...
/ (dudx(xro,yro)*dvdy(xro,yro)-dudy(xro,yro)*dvdx(xro,yro)));
% Plot current guess for root
hold on
plot3(xr,yr,u(xr,yr),'o','Markersize',10)
plot3(xr,yr,u(xr,yr),'o','Markersize',10)
pause(1);
% Output to command window
fprintf('Iter=%5i, xr=%5.5f, yr=%5.5f, Error=%5.5e \n', ...
i,xr,yr,max(abs(xr-xro),max(abs(yr-yro))))
% Stop loop if converged
if max(abs(xr-xro),abs(yr-yro))<tol
fprintf('The root is (x,y)=(%5.10f,%5.10f) \n',xr,yr)
break %stop the loop
end
end
##### 3 CommentsShow 1 older commentHide 1 older comment
alexander fastiggi on 8 Sep 2021
Edited: alexander fastiggi on 8 Sep 2021
if you run the code, after the 5th iteration the value of xr,yr will go to NaN and the loop is suppose to 'break' when the value is NaN
Paul Hoffrichter on 8 Sep 2021
If xr is NaN, then max(abs(xr-xro),abs(yr-yro)) is also NaN and max(abs(xr-xro),abs(yr-yro))<tol is false. To test for NaN, use isnan. But then you drop into the if-body, and since xr is NaN, you will get NaN for the result.

per isakson on 8 Sep 2021
Edited: per isakson on 8 Sep 2021
The values of xr and yr do not converge to a solution that you expect. xr goes to zero and yr goes to "infinity".
>> Newton_Raphson_multidim
Iter= 100, xr= 1.17, yr=1.57e+02, Error=1.56e+02
Iter= 100, xr= 0.14, yr=-1.37e+07, Error=1.37e+07
Iter= 100, xr=-0.00, yr=1.10e+21, Error=1.10e+21
Iter= 100, xr= 0.00, yr=5.96e+57, Error=5.96e+57
Iter= 100, xr= 0.00, yr=-1.19e+154, Error=1.19e+154
Iter= 100, xr= NaN, yr= NaN, Error= NaN
Iter= 100, xr= NaN, yr= NaN, Error= NaN
Iter= 100, xr= NaN, yr= NaN, Error= NaN
"the loop is suppose to 'break' when the value is NaN" I modified the "ouput" statements
% Output to command window
fprintf('Iter=%5i, xr=%5.2f, yr=%5.2e, Error=%5.2e \n', ...
i,xr,yr,max(abs(xr-xro),max(abs(yr-yro))))
% Stop loop if converged
if max(abs(xr-xro), abs(yr-yro))<tol || isnan(xr) || isnan(yr)
fprintf('The root is (x,y)=(%5.2e,%5.2e) \n',xr,yr)
break %stop the loop
end
Now I get
>> Newton_Raphson_multidim
Iter= 100, xr= 1.17, yr=1.57e+02, Error=1.56e+02
Iter= 100, xr= 0.14, yr=-1.37e+07, Error=1.37e+07
Iter= 100, xr=-0.00, yr=1.10e+21, Error=1.10e+21
Iter= 100, xr= 0.00, yr=5.96e+57, Error=5.96e+57
Iter= 100, xr= 0.00, yr=-1.19e+154, Error=1.19e+154
Iter= 100, xr= NaN, yr= NaN, Error= NaN
The root is (x,y)=( NaN, NaN)
>>
alexander fastiggi on 8 Sep 2021
thank you!

### Categories

Find more on Linear Algebra in Help Center and File Exchange

R2019a

### Community Treasure Hunt

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

Start Hunting!