% ### EXnewtonRun.m ###       11.02.16

% find roots of  f(x)=a*(x^3)+ b*(X^2)+ c*(x)+ d= 0
% via external function newtonF.m, whose author writes:
% "roots.m in the MATLAB library can find all the roots of a 
% polynomial with arbitrary order. But this method, gives the one the roots 
% based on the initial guess and it gives the number of iteration required 
% to converge.


clear
% =====================
polyC= [4 1 3 1];  % polynomial coefficients [a b c d]
initial= -30;            % initial guess for a/the root
tolerance= 10^-2;        % threshold for convergence
maxiteration= 10^4;      % maximum # of iterations to compute (will stop if convergence flag not reached)
% =====================

% run the external function
[rootF,number_of_iteration] = EXnewtonF(polyC,initial,tolerance,maxiteration);
disp(sprintf('Newtons method found root = %.2f (within tolerance) after %.0f iterations ',rootF,number_of_iteration));

% via Matlab's built-in routine
rootsF2= roots(polyC);
disp(sprintf('Matlabs built-in routine found the following roots:')); rootsF2