# Finding number of terms determined by the error for sum of series

So inf is the actual numerical value taken of the sum from $n=0$ to $n=\infty$ for $(-1)^n/(3n+1)$. Now I'm trying to find how many terms I need so that the sum of the series comes out with a chosen tolerance:

Terms[inf_, tol_] := Block[{i, n, id},
i = 0;
Do[id = Sum[((-1)^(n)) (1/(3 n + 1)), {n, 0, i}];
i++;
If[Abs[inf - Abs[id]] < tol, Break[];
{i, 0, 10000000}]; Print[i + 1]]]


For some reason, I always get i=2 back even though I have a Do function and a break condition...Please tell me what I'm doing wrong, I'm relatively new to Mathematica..

• Have you entered this correctly? Because I copy-and-pasted, and your command is incomplete. Also, can you give a sample input for inf and tol? – march Oct 11 '16 at 20:03
• So guess that's the other part that I'm having trouble - I want to print out at the i th term that the Do function would stop given with the Break condition. So I'm trying inf = .835648848 and tol=.0001 – Laura Oct 11 '16 at 20:18

A direct fix for what you've done:

terms[inf_, tol_] := Block[{i, n, id},
Do[
id = Sum[((-1)^(n)) (1/(3 n + 1)), {n, 0, i}];
If[Abs[inf - Abs[id]] < tol, Print[i]; Break[]],
{i, 0, 10000000}]
]


Do already has an iterator ({i, 0, 10000000}), so you don't need to include the i=0 and i++ part. You need to Print the current value of i before the Break.

How I would have done it:

Sum[(-1)^n/(3 n + 1), {n, 0, ∞}]
inf = N@%
(* π/(3 Sqrt[3]) + Log[2]/3 *)
(* 0.835648848264721 *)


Then

terms[inf_, tol_] := Module[{id = 1, i = 1},
While[Abs[inf - Abs[id]] >= tol && i <= 100000,
id = id + (-1)^i/(3 i + 1);
i++];
i]


and

terms[0.835648848264721, 0.0001]
inf - Sum[((-1)^(n)) (1/(3 n + 1.)), {n, 0, % - 1}]
inf - Sum[((-1)^(n)) (1/(3 n + 1.)), {n, 0, %%}]
(* 1666 *)
(* 0.00010005 *)
(* -0.00009999 *)

• Hi, so I tried to do the very first method, but nothing seems to print out...? – Laura Oct 11 '16 at 20:26
• It works for me. You might need to restart your kernel, since there might be lingering definitions. But you should use the second version anyway. It's faster, and it's good Mathematica coding practice (in my opinion) to avoid Break[] statements. If you're worried about infinite loops, you can add a test at the beginning to quit the While once you've hit a certain number of iterations, as I've done in the edited post. – march Oct 11 '16 at 20:31

March's answer is I think the answer you were looking for so I have upvoted it. We could use some other ideas and play a bit with the problem. Mind you, I am not saying that march missed this or could not do better, just that he was confined by the parameters of your question and here we are not.

We know from the Leibniz rule for alternating series that the tail of that series is less than the magnitude of the first neglected term. So we solve

Solve[1/(3n+1)==1/10000,n]


which yields n = 3333. This tells us we will need 3333 terms to get an error <= .0001 This is a well known way to bound these but we can see that it is not the sharpest. The direct summation showed we really only need about half of that.

An offhand comment by alex.jordan over here See Ross Millikan's answer

suggests taking the mean of two consecutive partial sums. This might result in faster convergence and therefore less terms to get the desired error.

$\sum _{n=0}^{\infty } \frac{(-1)^n}{3 n+1}-\frac{1}{2}\left ( \left (2 \sum _{n=0}^b \frac{(-1)^n}{3 n+1} \right )+\frac{(-1)^{b+1}}{3 (b+1)+1} \right )$

Now we are searching for the smallest b that yields a difference <= .0001

Mathematica can do that easily

NMinimize[{b,Abs[Sum[(-1)^n/(3n+1),{n,0,\[Infinity]}]-(2Sum[(-1)^n/(3n+1),{n,0,b}]
+(-1)^(b+1)/(3(b+1)+1))/2]<1/10000,3333>b>0},b]


yields b -> 27.52553345330246, so we will use b = 28.

To check,

s = Sum[(-1)^n/(3 n + 1), {n, 0, \[Infinity]}];
s-(2Sum[(-1)^n/(3n+1),{n,0,b}]+(-1)^(b+1)/(3(b+1)+1))/2/.b->28.//Chop

(*-0.00009679302515119836 *)


We see we do indeed have an absolute value error of less than .0001 and have used just 29 terms to get it.

• You can use Chop to get rid ofthe spurious imaginary part of the final result. – corey979 Oct 12 '16 at 7:33
• @corey979 I have added that, thanks! – bobbym Oct 12 '16 at 10:17