# channelflow.org

### Site Tools

gibson:teaching:fall-2013:math445:lecture6

# Matlab diary for pi^2/6 lecture

```% How to estimate pi^2/6 from N terms of series 1 + 1/2^2 + 1/3^2 + 1/4^2 + ...

% Using a for-loop
sum=0;
N=100;
for n=1:N
sum = sum + 1/n^2;
end

sum
sum =  1.6350

pi^2/6
ans =  1.6449

sum-pi^2/6
ans =  -0.0100      % off by about 0.01 for N=100 terms --not so great

% To try different values of number of terms, write a function
% see file pisquared6.m

pisquared6(100)
ans =
1.6350
pisquared6(100) -pi^2/6
ans =
-0.0100
pisquared6(1000) -pi^2/6
ans =
-9.9950e-04
pisquared6(10000) -pi^2/6
ans =
-9.9995e-05

% Even better, produce a plot of value of series versus N
% See pigraph.m

% Now, do the same calculations with one line of Matlab

sum((1:10).^(-2)) % sum 1/n^2 over n=1 through 10
ans =
1.5498

sum((1:100).^(-2)) % sum 1/n^2 over n=1 through 100
ans =
1.6350

sum((1:100).^(-2)) - pi^2/6
ans =
-0.0100

sum((1:1000).^(-2)) - pi^2/6
ans =
-9.9950e-04

sum((1:10000).^(-2)) - pi^2/6
ans =
-9.9995e-05

sum((1:100000).^(-2)) - pi^2/6
ans =
-9.9999e-06``` 