% Fixed point iteration with Newton method to find cube root.
% n - calculate cube root of n
% x - guess of n's cube root that is iteratively refined
% xnew - educated guess of new x according to Newton
% err - relative difference between x & new x, used as error
n= input('Number to find cube root of: ');
x= input('First guess of the #''s cube root: ');
tol= 0.0001; % Error tolerance
err= 1; % Assumed error to start loop
while (err > tol) % Still too far from answer?
xnew= x - (x^3 - n)/(3*x^2); % Newton's method for next guess
err= abs((xnew - x) / x); % relative error calculation
x= xnew;
disp(x)
end
fprintf('The cube root of %g is %g!\n', n, x);