Secant method in Matlab

5.5k Views Asked by At

enter image description here enter image description here

I have done following, but I don't know how to put that function into this:

function [x,y] = secant(fun,x0,x1,tol,max)
x(1) = x0;
x(2) = x1;
y(1) = feval(fun,x(1));
y(2) = feval(fun,x(2));
    for i = 3 : max
        x(i) = x(i-1) - y(i-1)/((y(i-1)-y(i-2))/(x(i-1)-x(i-2)));
        y(i) = feval(fun,x(i));
        if abs(x(i) - x(i-1)) < tol
            disp('Secant method converged!');
            break;
        end
        if i== max
            disp('Zero not found!');
        end
    end
n = length(x);
k = 1:n;
out = [k' x' y']; 

disp(out);