Running a While Loop and Not Getting a Different Answer

조회 수: 16 (최근 30일)
Drake
Drake 2014년 10월 27일
댓글: Drake 2014년 10월 27일
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.

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 10월 27일
편집: Geoff Hayes 2014년 10월 27일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Newton-Raphson Method에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by