2
$\begingroup$

I am using Mathematica 8.0.1.0. I defined the following function for use in an answer on math.SE:

f[j_] := Sum[1/(j-2k+1)/4^k, {k, 0, Floor[j/2]}]

I then used this function in

NSum[(-1)^i f[i], {i, 0, 7}, WorkingPrecision -> 20]

I was expecting a positive answer, but got -0.83296130952380952381, whereas

N[f[0]-f[1]+f[2]-f[3]+f[4]-f[5]+f[6]-f[7], 20]

gives 0.83296130952380952381 which is the answer I expected.

If I define the function as

f[j_] := Evaluate[Sum[1/(j-2k+1)/4^k, {k, 0, Floor[j/2]}]]

then f[0] yields 1/8 (4 ArcTanh[1/2] - 4 (2 + ArcTanh[1/2])) which simplifies to -1. This is very surprising since f[0] is a sum of positive values. In fact, Simplify[f[k]] gives the negative of what the answer should be, at least for the integers k that I have tried. This possibly narrows down where the problem lies when I use the original function in NSum

Is this a problem with version 8.0.1.0, or is this reproducible in more recent versions of Mathematica?


There is a second problem when I use the original function in

NSum[(-1)^k f[k], {k, 0, 7}, WorkingPrecision -> 20]

I get the errors

Power::infy: Infinite expression 1/0 encountered. >>

and

NSum::nsnum: Summand (or its derivative) (-1)^k f[k] is not numerical at point k = 0. >>

I don't understand these errors. I thought the local variable names were treated as if used in Block, so I don't see why the k in the definition of the function should interfere.

$\endgroup$
  • 1
    $\begingroup$ Looks like a bug in Sum (for f[j]). I get the negative sum in V11.2. $\endgroup$ – Michael E2 Mar 3 '18 at 20:21
  • $\begingroup$ @MichaelE2: Thanks for the verification! I was getting a similar negative result for another sum yesterday, so I was worried my computer was possessed. $\endgroup$ – robjohn Mar 3 '18 at 20:37
1
$\begingroup$

It looks like the symbolic sum is computed wrong:

Clear[f];
f[j_] := Sum[1/(j - 2 k + 1)/4^k, {k, 0, Floor[j/2]}];

f[j] /. j -> 0.  (* sum first with symbolic limit, then with 0. substituted *)
f[0.]            (* sum with numeric limit *)
(*
  -1.
   1.  
*)

Fixes:

Restrict j to numeric values:

Clear[ff];
ff[j_Integer] := Sum[1/(j - 2 k + 1)/4^k, {k, 0, Floor[j/2]}];
NSum[(-1)^i ff[i], {i, 0, 7}, WorkingPrecision -> 20]
(*  0.83296130952380952381  *)

The same thing, with k protected by Module[{k},...]:

Clear[ff];
ff[j_Integer] := Module[{k}, Sum[1/(j - 2 k + 1)/4^k, {k, 0, Floor[j/2]}]];
NSum[(-1)^k fff[k], {k, 0, 7}, WorkingPrecision -> 20]
(*  0.83296130952380952381  *)

Avoid Sum (which I usually do when I want to calculate the sum of a finite number of terms):

Clear[fff];
fff[j_Integer] := Total@Table[1/(j - 2 k + 1)/4^k, {k, 0, Floor[j/2]}]
NSum[(-1)^i fff[i], {i, 0, 7}, WorkingPrecision -> 20]
(*  0.83296130952380952381  *)
$\endgroup$
  • $\begingroup$ Thanks! This confirms my suspicions that there was something wrong in the symbolic sum since the symbolic result for f[0] simplified pretty easily to -1. $\endgroup$ – robjohn Mar 3 '18 at 21:12
1
$\begingroup$

Well, you can read in the documentation that "NSum first localizes the values of all variables, then evaluates f with the variables being symbolic, and then repeatedly evaluates the result numerically".

I suggest you use Trace to see how different is NSum[] related to N[Sum[]]. I would simply do:

f[j_] := Sum[1/(j - 2 k + 1)/4^k, {k, 0, Floor[j/2]}];
N[Sum[(-1)^(i) f[i], {i, 0, 7}], 20]

(* 0.83296130952380952380952380952380952381 *)
$\endgroup$
  • $\begingroup$ Thanks! This tells why the k might interfere. I will use Trace to investigate further. However, that f[j_] := Sum[1/(j - 2 k + 1)/4^k, {k, 0, Floor[j/2]}]; NSum[(-1)^(i) f[i], {i, 0, 7}, WorkingPrecision -> 20] returns the negative of the correct answer still does not make sense. $\endgroup$ – robjohn Mar 3 '18 at 19:51
  • $\begingroup$ I think it can help if you define f[j_]=Sum[...] and run f[0], you might understand what is happening. $\endgroup$ – José Antonio Díaz Navas Mar 3 '18 at 19:54
  • $\begingroup$ Yes, that's the same result for f[0] that I mention in my question: 1/8 (4 ArcTanh[1/2] - 4 (2 + ArcTanh[1/2])) which simplifies easily to -1. $\endgroup$ – robjohn Mar 3 '18 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.