Error in script for estimating $\pi$

49 Views Asked by At

I am new to matlab, and am trying to write a script for this equation for n=100:

enter image description here

And am using this:

n = 100; % set number of terms
sum = 1; % starting point of the sum
sign = 1; % an integer +/- 1
for k = 1 : n % forloop - loops k from 1 to n
    term = sign/(k^2); % compute the term
    sum = sum + term; % update the rolling sum
    sign = - sign; % flip the sign
end;
disp(n); % print n
disp((12*sum)^(0.5)); % print the approximation to pi
disp((12*sum)^(0.5)-pi); % print the error

But this is returning some strange values:

100 - fine

    4.6764 ????

    1.5348 ????

Would anyone be able to point out the mistake I made? I have a feeling it might be the way I raised the sum to the power of 0.5, but am not entirely sure.

Thanks