the initial guess in the following loop is actually inside the loop, why is that?
function [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,varargin)
if nargin<3,error('at least 3 input arguments required'),end
if nargin<4|isempty(es),es=0.0001;end
if nargin<5|isempty(maxit),maxit=50;end
iter = 0;
while (1)
xrold = xr;
xr = xr - func(xr)/dfunc(xr);
iter = iter + 1;
if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end
if ea <= es | iter >= maxit, break, end
end
root = xr;
=======================
thanks a lot

답변 (1개)

Mischa Kim
Mischa Kim 2015년 5월 6일
편집: Mischa Kim 2015년 5월 6일

0 개 추천

B, the initial guess is provided through the newtraph call. In other words, in order to execute/call this function you need to provide an xr value. That is the initial value that is then updated in the loop.

댓글 수: 4

B
B 2015년 5월 6일
thanks but why don't we place xrold=xr outside the loop?
Stephen23
Stephen23 2015년 5월 6일
편집: Stephen23 2015년 5월 6일
@B: because the variable xrold is not the initial value, it is actually the previous value. On every loop it is assigned to be the value of xr from the last loop iteration. Only on the first iteration is it the "initial" value.
Mischa Kim
Mischa Kim 2015년 5월 6일
With xrold = xr you keep track of the last value of xr so you can compute how the solution changes. If the difference between consecutive solutions of xr becomes smaller than es (= 0.0001) then the problem is declared solved and the loop is exited.
B
B 2015년 5월 6일
Thanks a lot Stephen and Mischa. I got now :)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

B
B
2015년 5월 6일

댓글:

B
B
2015년 5월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by