%estimates a root of f in the interval [a,b] via the false-position method

f = @(x) x^3+2*x+cos(x);
a = -1;                   %left endpoint
b = 0;                    %right endpoint
n = 10;                   %number of iterations

for i=1:n
    c = (a*f(b)-b*f(a))/(f(b)-f(a));
    if sign(f(c)) ~= sign(f(a))
        b = c;           %root is in [a,c]
    else
        a = c;           %root is in [c,b]
    end
end

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

