Running a While Loop and Not Getting a Different Answer
1 view (last 30 days)
Show older comments
Hello All,
I am attempting to code the Vectorial Newton Raphson Method to solve a system of equations and my code does not seem to be working. Rather, after running 100 iterations of the code I am merely getting my input back as the answer.
Here is the code:
function[xfinal,yfinal]=NewtonVectorial(x, y, d1, d2, R, H, tol, maxiter)
xR=x;
yR=y;
iter=0;
while(1)
xold=xR;
yold=yR;
new=[xold;yold]-([-sin(xold)-sin(xold+yold),- sin(xold+yold);cos(xold)+cos(xold+yold),cos(xold+yold)])\([d1.*cos(xold)+d2.*cos(xold+yold)-R;d1.*sin(xold)+d2.*sin(xold+yold)-H])
iter=iter+1;
if abs(new-[xold;yold])<tol
break
end
if iter>maxiter
break
end
xold=new(1,1);
yold=new(2,1);
end
xold
yold
iter
I have hard coded my partial derivatives for my Jacobian as well as the two functions I am trying to solve.
As an example for the output, this is what I am getting:
>> NewtonVectorial(pi/2,pi/4,1,1,1,1.1,.01,100)
xold =
1.5708
yold =
0.7854
iter =
101
Where yold and xold are just the numbers I initially put into the code.
Any insight as to why my code is not yielding correct values would be greatly appreciated.
Thanks.
0 Comments
Accepted Answer
Geoff Hayes
on 27 Oct 2014
Edited: Geoff Hayes
on 27 Oct 2014
Drake - the problem is that the xold and yold are always being reset to x and y in the first two lines of the while loop
while(1)
xold=xR;
yold=yR;
Just move these two lines outside of the loop, getting rid of xR and yR as
xold=x;
yold=y;
iter=0;
while(1)
new=[xold;yold]- ...
% etc.
end
and try running your code again.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!