function x = newton(f,fprime,x0) % NEWTON Newton's method for a scalar equation. % Input: % f function that outputs values of f(x) % fprime function that outputs values of f'(x) % x0 initial root approximation % Output % x vector of root approximations (last is best) % Operating parameters. funtol = 100*eps; xtol = 100*eps; maxiter = 40; x = x0; fn = f(x0); dx = Inf; n = 1; while (abs(dx) > xtol) & (abs(fn) > funtol) dfn = fprime(x(n)); dx = -fn/dfn; % Newton step x(n+1) = x(n) + dx; n = n+1; if n==maxiter warning('Maximum number of iterations reached.') break end fn = f(x(n)); end