% Usage: [x n] = newton(f,df,x0,tol) % Newton's method for solving f(x)=0 % % Input: % f - Matlab inline function f(x) % df - Matlab inline function df(x), the derivative of f(x) % x0 - initial guess % tol - target tolerance: output x satisfies |f(x)| <= tol % % Output: % x - computed solution % n - number of iterations % % Examples: % f=inline('sin(t)'); % df=inline('cos(t)'); % y=newton(f,df,1,0.01); function [x n]= newton(f,df,x0,tol) x = x0; z = f(x); n = 0; while norm(z) > tol x = x - df(x) \ z; z = f(x); n = n + 1; end;