% cubeRoot.m
% Computes "the" cube root of

clear; 
format long g;

p = input('Enter a number to find its cube root: ');

x = p;          % initial guess for the cube root
inv3 = 1/3;     % one third
tol = 1e-12;    % toterance: max. acceptible error
maxIt = 200;    % max. number of iterations
iter = 0;       % iteration counter
dx = 1;         % the change in x from 1 iteration to the next 
while iter <= maxIt & abs(dx) > tol
    iter = iter + 1;
    dx = -inv3 * ( x - p / ( x*x ) );   % compute the change in x
    x = x + dx,                         % next value of x
end

if iter > maxIt
    disp('Maximum number iteration (20) exceeded!');
else
    disp(['A cube root of ' num2str(p) ' is: ' num2str(x)]);
    disp(['The exact solution is: ' num2str(p^(inv3))]);
end







