%estimates root r=0 of f(x)=x^2 and g(x)=x(1+x) via Newton's method
%for f convergence is linear; for g it is quadratic

f = @(x) x^2;
g = @(x) x*(1+x);
f_p = @(x) 2*x;
g_p = @(x) 2*x+1;

n = 1;     %number of iterations
x = 1;      %initial guess

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

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

for i=1:n
    x = x - g(x)/g_p(x);
end

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