On Tue, Sep 25, 2012 at 7:46 AM, Joza
<address@hidden> wrote:
Hi guys, new here!
So I'm trying to compute the value of pi using a series with a billion terms
(n):
for i=1:1:n
x(i) = ( (-1)^(i + 1) ) / ( (2*i) - 1 );
end
% Sum all the values in the vector x
value = 4*sum(x)
When I run this however, nothing happens ... I just get a blinking prompt
and have to CTRL+C to get back to normal.
<snip>
played around with ways of implementing your loop. I'm running on a fairly weak 32bit laptop at the moment.
---
n=1e5;
tic;
for i = 1:1:n; x(i) = ( (-1)^(i + 1) ) / ( (2*i) - 1 );endfor
toc
newpi=4*sum(x)
n = 1e3, Elapsed time is 0.006 seconds.
n = 1e4, Elapsed time is 0.056 seconds.
n = 1e5, Elapsed time is 0.585 seconds.
n = 1e6, Elapsed time is 9.698 seconds.
n = 1e7, Elapsed time is 394.3 seconds.
i didn't run more than that., but if we extrapolate using P(2) time...
n = 1e8, ~8.5hrs, n = 1.9, ~34 days.
-------------
also, as comes up very often, you're re-casting the x array each time. you can pre-define it to length n, and it shaves off quite a bit at the higher levels. should help with any memory limits as well as you won't need to store the array twice at the end of each iteration:
n=1e5;
tic;
x=zeros(n,1);
for i = 1:1:n; x(i) = ( (-1)^(i + 1) ) / ( (2*i) - 1 );endfor
toc
newpi=4*sum(x)
n = 1e3, Elapsed time is 0.006 seconds.
n = 1e4, Elapsed time is 0.061 seconds.
n = 1e5, Elapsed time is 0.608 seconds.
n = 1e6, Elapsed time is 6.933 seconds.
n = 1e7, Elapsed time is 65.48 seconds.
so, if it's still P(2)
n = 1e8, ~20min, n = 1.9, ~16hrs
so, if you really want to go there, I guess you can.