Help with Newton's method function?

조회 수: 9 (최근 30일)
Lauren Hux
Lauren Hux 2016년 11월 20일
댓글: Walter Roberson 2016년 11월 20일
This is the function I've been making to run Newton's method, but I can't get it to work. Any help with what I might be missing? Thanks!
function xn = newtonmethod(f,fd,x0,tol)
xn=x0;
err=abs(f(xn)-0);
i=0;
while err>tol
i=i+1;
xnew=xn-(f(xn)/fd(xn));
err=abs(f(xnew)-0);
end

답변 (1개)

Walter Roberson
Walter Roberson 2016년 11월 20일
I suspect your
err=abs(f(xnew)-0);
should be
err=abs(f(xnew)-f(xn));
unless you know that your f() has a "right" value of 0.
  댓글 수: 2
Lauren Hux
Lauren Hux 2016년 11월 20일
It does. Do you see anything else wrong with the code?
Walter Roberson
Walter Roberson 2016년 11월 20일
You do not update xn = xnew

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by