% Newtraph.m
% Find the roots of an equation
% Uses Newton-Raphson method
% Calls user defined functions f.m and df.m
a = input('Enter guess at root ');
tol = input('Select desired tolerance on solution ');
error = 1; % initialize error
n = 0; % iteration counter
% iteration loop
while error > tol
x = a - f(a)/df(a);
error = abs(x-a);
n = n+1;
if n > 25
error('Excessive iteration and no convergence.');
end
a = x;
end
disp('Tolerance (s)')
disp(tol)
disp('Number of iterations')
disp(n)
disp('Root of function')
disp(x)
% end of script