function A1

xx = -4:0.1:4;
ff = f(xx);

subplot('position',[0.25 0.35 0.5 0.5]);
plot(xx,ff,'r');
axis([xx(1) xx(end) ff(1) ff(end)]);
hold on;
plot([xx(1) xx(end)],[0 0],'--b');
plot([0 0],[ff(1) ff(end)],'--b');

xFix = 1.391745200270735; % Newton springt zwischen xFix und -xFix hin und her

x0   = 0.78;   % Beide Verfahren konvergieren
x0   = 1.42;   % Newton und Newton_FixedDerivative divergieren
x0   = 1.20;   % Newton konvergiert, Newton_FixedDerivative divergiert

[N,err]       = Newton(x0);
%[M,err_Fixed] = Newton_FixedDerivative(x0);
%[O,err_Bisec] = BisectionMethod(x0-2,x0+2);
xlabel x
ylabel arctan(x)
title('Newton-Verfahren');
text(x0-0.1,-0.1,'\uparrow Startwert');
print -deps2 tutor1_1.eps

%figure;
%semilogy(1:N,err,'.-k');
%hold on;
%semilogy(1:M,err_Fixed,'.-k');
%semilogy(1:O,err_Bisec,'.-m');




function [N,err] = Newton(x0)
plot([x0 x0],[0 f(x0)],'.-g');
N = 0; err = [];

while (abs(x0)>1e-15) & (abs(x0)<500)
   x1 = x0 - f(x0) / df(x0);
   plot([x0 x1],[f(x0) 0],'.-k');
   plot([x1 x1],[0 f(x1)],'.-g');
   x0 = x1;
   err = [err abs(x0)];
   N = N+1;
end;




function [N,err] = Newton_FixedDerivative(x0)
plot([x0 x0],[0 f(x0)],'.-g');
N = 0; err = [];

fprime = df(x0);
while (abs(x0)>1e-15) & (abs(x0)<500)
   x1 = x0 - f(x0) / fprime;
   plot([x0 x1],[f(x0) 0],'.-k');
   plot([x1 x1],[0 f(x1)],'.-g');
   x0 = x1;
   err = [err abs(x0)];
   N = N+1;
end;




function [N,err] = BisectionMethod(x1,x2);
N = 0; err = [];

while 1
   x3=(x1+x2)/2; 
   f1=f(x1);     % Funktionswert links ...
   f2=f(x2);     % ... rechts ...
   f3=f(x3);     % ... und in der Mitte

       if f1*f3<=0 x2=x3;      % Nullstelle im 1. Teilintervall
   elseif f2*f3<=0 x1=x3; end; % Nullstelle im 2. Teilintervall

   err = [err abs(x2-x1)];
   N = N+1;

   ysize = 1/2^N;
   plot([x1 x1],[-ysize ysize],'m');
   plot([x2 x2],[-ysize ysize],'m');
   if (err(end)<1e-15) | (f1*f2>0) break; end;
end;




function ff = f(x)
ff = atan(x);



function dd = df(x)
dd = 1./(1+x.^2);
