%estimates a root of f via the secant method

format long;

f = @(x) x^3+2*x+cos(x);
phi = @(x,y) x-f(x)*(x-y)/(f(x)-f(y));

n = 5;            %number of iterations
x_old = 1;        %first guess
x_new = 0.9;      %second guess

for i=1:n
    x = phi(x_new,x_old); 
    x_old = x_new;
    x_new = x;
end

disp('estimate of root is...')
x_new

disp('true value is...')
-0.419659764448812