%estimates a root of f via Newton's method

f = @(x) x^3+2*x+cos(x);
f_p = @(x) 3*x^2+2-sin(x);    %f_p = f'

n = 10;     %number of iterations
x = 0;      %initial guess

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

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