%The following implements Newton's
%     method for 1/(x^2+1 )-1/2=0. Experiment with
%     different initial value x and the number of iterations to observe
%     convergence behavior. The graph of the function is given at bottom. It's
%     also worth trying different equations. Try sin(x) - x =0.


% set initial apprximation here, change this value for experiments
x=2;
% 20 Newton's iterations
fprintf(1,'  \nIteration\tApproximation\tError Estimate\n\n');
for i=1:20,
  fx=1/(x^2+1)-1/2;
  fprime=-2*x/(x^2+1)^2;
  xnew=x-fx/fprime;
  error=abs(x-xnew);
  fprintf(1,'  %d\t\t%2.16e\t\t%e\n',i,xnew,error);
  x=xnew;
end
%plot the function
f=inline('1/(u^2+1)-1/2','u')
fplot(f,[0,2])

