Newton's Method Issues

Hi, I'm having a little trouble with my newton's method code.
One of my biggest issues is that I'm not able to create an output, rHist, which is a vector consisting of x0 and the outputs after every iteration of newton's method and my number of iterations is off by 1.
My code (posted below) also doesn't seem to work when the initial guess, x0 satisfies the stopping criterion.
% function [r,N,fRoot] = newtonRaphE7(fHan,dfHan,x0,fTol,iterMax)
k=1;
while k<=iterMax
x=x0-fHan(x0)/dfHan(x0)
if abs(x-x0)<fTol*abs(x)
return
x0=x;
r=x0;
k=k+1;
N=k;
fRoot=abs(fHan(r));
end
end

回答 (1 件)

Matt J
Matt J 2012 年 11 月 19 日

0 投票

It should contain lines like this somewhere
rHist=zeros(1,iterMax+1);
....
rHist(k)=x;
My code (posted below) also doesn't seem to work when the initial guess, x0 satisfies the stopping criterion.
Because you're exiting without updating the outputs r,N, fRoot

4 件のコメント

SB
SB 2012 年 11 月 19 日
Well, r, fRoot and N are the final x, final f(x) and number of iterations respectively, can't they be at the end (since the actual method portion is working)? I understand that rHist would need to be inside the loop.
Matt J
Matt J 2012 年 11 月 19 日
編集済み: Matt J 2012 年 11 月 19 日
It doesn't do any good to put them at the end if the code never reaches the end, and it doesn't, because you call RETURN before it gets there.
SB
SB 2012 年 11 月 19 日
Alright, how would I actually rewrite it, just move the statements after the if?
Matt J
Matt J 2012 年 11 月 19 日
Hint: MATLAB executes lines of code in order from top to bottom.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeApp Building についてさらに検索

質問済み:

SB
2012 年 11 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by