% Secant method looks for a zero of function f using an % iterative method, with first guess x=a, b. It stops when |f(x_n)| < tol. % it returns the root and the iterates (x). % % function [root,x] = secant(a,b,tol) function [root,x] = secant(a,b,tol) test = abs(f(a)); i = 1; x0 = a; x1 = b; x(i) = x1; while test > tol x_new = x1-f(x1)*(x1-x0)/(f(x1)-f(x0)); x0 = x1; x1 = x_new; test = abs(f(x_new)); i = i+1; x(i) = x_new; end root = x_new;