% Bsp 37-a
%Integralkriterium ergibt |Rn| <= pi/2-atan(n)

tol = 1e-6;
sum = 1;n=1;
while pi/2-atan(n) > tol
    n=n+1;
    sum = sum + 1/(1+n^2);
end
sum

% Bsp 37-b
% Leibnizkriterium ergibt |R_n| <= an

tol = 1e-3;
sum = 0; n=0;
s=1;an=1;
while an > tol
    sum = sum + s*an;
    n=n+1;
    s=-s;
    an = 1/(2*n+1);
end
sum
% Lecture 10
% Summation von Reihen
% Bsp 37-c
% Quotientenkriterium ergibt |r_n| <= 3 * (2/3)^(n+1)

tol = 1e-5;
sum = 0; i=0;
s1=1;s2 = 1; q= 2/3;
while 3*s2>tol
    sum = sum+s1-s2;
    s1 = s1/3;
    s2 = s2*q;
end
sum

% Bsp 38
%
% Abbruchfehler |R_2n+1(x)| <= |xmyx|^(2n+1)/(2n+1)!

xmax = pi/4; tol = 1e-5;
x=0:xmax/100:xmax; x2= x.^2;
e = xmax; p = xmax^2; sum = 0;
a= x;i=0;
while e > tol
    sum = sum +a;
    i=i+1;
    a = -a.* x2 / (2*i*(2*i+1));
    e =  e.* p / (2*i*(2*i+1));
end
z = sin(x) -sum;
plot(x, z, -fliplr(x), -fliplr(z))