%estimates a root of f via modified Newton's method

f = @(x) x^3+2*x+cos(x);

a = -1;      %left endpoint
b = 0;       %right endpoint
m = 2;       %lower bound for f' in [a,b]
M = 6;       %upper bound for f' in [a,b]
K = 1-m/M;   %contraction constant
n = 10;      %number of iterations
x = 0;       %initial guess

for i=1:n
    x = x - f(x)/M;
end

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

disp('upper bound for error is')
(K^n)*(b-a)/(1-K)    %upper bound obtained via contraction mapping theorem
