% Unit 2 (MATLAB Course @ UAM)
% (c) Alexander Knebe

%--------------------------------------------------------------------------
% Leibniz series for pi/4
%--------------------------------------------------------------------------
clear all
close all
clc

Nmax = 200;

% exercise #1:
%--------------
% for-loop solution
f    = zeros(1,Nmax);
for N=1:Nmax
    n = [0:N];
    f(N) = sum((-1).^n./(2.*n+1));
end
% alternatively one could use cumsum()
n = [1:Nmax];
f = 1+cumsum((-1).^n./(2.*n+1));  % note: n=0 gives 1

figure(1), hold on
plot([1:Nmax],f,'b')

% exercise #2:
%--------------
g = 2./3+cumsum(2./((4.*n+1).*(4.*n+3))); %note: n=0 gives 2/3
plot([1:Nmax],g,'g','LineWidth',2)

% overplot approximate value pi/4
plot([1:Nmax],[pi/4],'r.')

legend('sum #1','sum #2','pi/4')
xlabel('N')
ylabel('sum')

