Help with Newton's method function?

3 vues (au cours des 30 derniers jours)
Lauren Hux
Lauren Hux le 20 Nov 2016
Commenté : Walter Roberson le 20 Nov 2016
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

Réponses (1)

Walter Roberson
Walter Roberson le 20 Nov 2016
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 commentaires
Lauren Hux
Lauren Hux le 20 Nov 2016
It does. Do you see anything else wrong with the code?
Walter Roberson
Walter Roberson le 20 Nov 2016
You do not update xn = xnew

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by