0

I define a very simple function like this,

g[k_] := 1. + 2 Sum[1/(1 + (2 n)^2), {n, 1, k}]

g[50] give the result is: 1.70279.

But g[n] /. n -> 50 give aother result: 1.70279 +0. I, It is a complex, something wrong?

user26992
  • 1
  • 2

2 Answers2

1

Without the 1. +, your expression evaluates to:

1/2 I (PolyGamma[0, 1 - I/2] - PolyGamma[0, 1 + I/2] - 
   PolyGamma[0, 51 - I/2] + PolyGamma[0, 51 + I/2])

When you add 1., although the imaginary parts cancel, the answer remains a complex number.

The reason you get a real number when you evaluate g[50] is that the sum doesn't use the general form, but finds the specific form (a rational number with a large denominator).

Try comparing these expressions to see what I'm talking about.

2 Sum[1/(1 + (2 n)^2), {n, 1, 50}]
1. + %

2 Sum[1/(1 + (2 n)^2), {n, 1, k}]
% /. {k->50}
1. + %
2012rcampion
  • 7,851
  • 25
  • 44
  • g[k_Integer] := 1. + 2 Sum[1/(1 + (2 n)^2), {n, 1, k}] Here _Integer is used for g[k], then g[50] and g[n]/. n->50 have the same result. – user26992 Mar 14 '15 at 16:54
0
Clear[g]

g[k_] = 1 + 2 Sum[1/(1 + (2 n)^2), {n, 1, k}] // FullSimplify

(1/2)(PiCoth[Pi/2] - IHarmonicNumber[-(I/2) + k] + IHarmonicNumber[I/2 + k])

For integer arguments you can use FunctionExpand to get an exact rational result with very large numerator and denominator. Use N to convert to approximate real result.

Table[{n, g[n] // FunctionExpand // N}, {n, 0, 100, 5}]

{{0, 1.}, {5, 1.62227}, {10, 1.66514}, {15, 1.68045}, {20, 1.68831}, {25, 1.69309}, {30, 1.6963}, {35, 1.69861}, {40, 1.70034}, {45, 1.7017}, {50, 1.70279}, {55, 1.70368}, {60, 1.70442}, {65, 1.70506}, {70, 1.7056}, {75, 1.70607}, {80, 1.70648}, {85, 1.70684}, {90, 1.70716}, {95, 1.70745}, {100, 1.70771}}

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198