% Usage: [x n] = secant(f,x0,x1,tol) % The secant method for solving f(x)=0 % % Input: % f - Matlab inline function f(x) % x0, x1 - initial guesses % tol - target tolerance: output x satisfies |f(x)| <= tol % % Output: % x - computed solution % n - number of iterations % % Examples: % f=inline('sin(t)'); % y=newton(f,1,2,0.01); function [x n]= secant(f,x0,x1,tol) z = f(x1); n = 0; while norm(z) > tol x = x1 - z * (x1 - x0) / (f(x1) - f(x0)); z = f(x); n = n + 1; x0 = x1; x1 = x; end;