Im trying to calculate the value of PI by using George's Series or the Leibniz Formula which is the sum of several values which alternate in adding and subtracting values. The goal of my program is to create for loops for the odd and then even values and then take the sum of those values.
This is what the series looks like for some reference,
pi/4=1-1/3+1/5-1/7+1/9-....
Here's my code: I keep receiving zero and I'm really stumped as to why, I could use any help!
int main()
okay, I've changed it as you suggested and I'm closer now but still not there. I received back a value of 3.13759. Should I increase the increment from 500 to something higher?
for (int i = 1; i <500; i= i + 4)
{
indval1 =indval1+ 1.0 / i;
}
for (int j=3; j<500; j=j+4)
{
indval2 =indval2+ 1.0/j;
}
It is an alternating series with diminishing terms, so (before multiplying by 4) the error should always be less than the magnitude of the first omitted term.
If you have got to j=500 then the error in the series sum could be as big as 0.002 and when you multiply by 4 that would potentially give an error of 0.008. Your answer is within the range of 3.14159... plus or minus 0.008, so OK.
In general, your error could be as large as 4/j, where j is the largest value that you get to. This series doesn't converge particularly quickly, I'm afraid.
Yes, my understand is that this approximation is relatively simple to code, but converges to a result verrry slowly.
Try increasing to 5000, then 50000 then 500000 iterations and so on. Of course it will slow down the processing. You may also reach the limit of the integer type. If that happens, try using unsignedlonglongint instead of int.
Leibniz's formula converges extremely slowly: it exhibits sublinear convergence. Calculating π to 10 correct decimal places using direct summation of the series requires about five billion terms
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.