All pastes #343693 Raw Edit

Stuff

public text v1 · immutable
·published 2007-02-07 06:07 UTC
rendered paste body
% 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