User Tools

Site Tools


gibson:teaching:fall-2013:math445:lecture6functions

pisquared6.m: a function to approximate pi^2/6 from a series, using basic programming tools (summing over a for-loop)

function sum = pisquared6(N);
% approximate pi^2/6 with series 1 + 1/2^2 + 1/3^2 + ... + 1/N^2

  sum = 0;
  for n=1:N
    sum = sum + 1/n^2;
  end
  
end

pigraph.m: a script to draw a plot of error versus N for the above approximation to pi^2/6

M=15;  % double the size of the series this many times

error = zeros(M,1); % store the error for the truncated series 
N     = zeros(M,1); % the length of the truncated series

for k=1:M
  N(k) = 2^k; % N is number of terms in series expansion for pi^2/6
  error(k) = abs(pisquared6(N(k)) - pi^2/6);
end

loglog(N,error,'k-o');
xlabel('N')
ylabel('error of series truncated at Nth term')
gibson/teaching/fall-2013/math445/lecture6functions.txt · Last modified: 2013/09/16 18:56 by gibson