# Integral over real valued function becomes complex

I've tried to solve the following integral, but I get a complex solution even tough it should only have a real part.

$$f(x)=- \frac{10^{-20} x}{0.99005- e^{10^{-12}x}}$$

Now I want to calculate the following:

$$\int_0 ^{10^9} f(x) dx$$

which shoud just give a real solution if we look at the graph

But using the following code I get a complex solution:

Integrate[f[x], {x, 0, 10^9}]


Which gives the following solution.

$0.471182 - 6.29146\times10^{-13} i$

Can anyone explain to me why the solution has a complex part? I suppose I can neglect this part but I don't know why. Also I calculated the indefinite integral and I believe that part of the problem comes from the PolyLog:

Integrate[f[x], x] // Simplify

(* Out:
x (-5.05025 10^(-21) x + 1.01005*10^(-8) Log[1. - 1.01005 Exp[1. 10^(-12) x)]] +
10100.5 PolyLog[2., 1.01005 Exp[1. 10^(-12) x]]
*)

• If you use NIntegrate, which you should since you have floating points in your definition of f, then it gives a real result May 25, 2016 at 14:25
• Yes using NIntegrate gives the same real result (so without the im part), but I still get the same problem when I use a symbolic function instead of floating points (which I would prefer since i need to change the numbers in my function) May 25, 2016 at 14:36
• Please format inline code and code blocks by selecting the code and clicking the {} button above the edit window. It is recommended that you browse the Markdown help . Apply this to the function definition f. May 25, 2016 at 14:39
• @JasonB Thanks for the hint; I didn't even recognize it
– user36273
May 25, 2016 at 16:39
• @rewi it was tricky to differentiate the the font sizes in the TeX with a superscript on a superscript May 26, 2016 at 5:32

## 2 Answers

Use higher precision

f[x_] = -(10^-20 x)/(0.99005 - E^(10^-12 x)) // Rationalize // Simplify;

int = Integrate[f[x], {x, 0, 10^9}] // FullSimplify;

int // N[#, 20] & // Chop[#, 10^-20] &

(*  0.47118211649097404645  *)

• Can you explain how this code gives higher precision? Because as I see it you just replace numbers that are close to zero with zero May 26, 2016 at 12:01
• @DeanTheMachine - The Chop[#, 10^-20] & only eliminates absolute values less than 10^-20. With machine precision the artifact was on the order of 10^-12. Increasing the precision with N[#, 30] & would further reduce the artifact. Since you can reduce the artifact to an arbitrarily small value, it is very strong evidence that the imaginary part is actually zero. May 26, 2016 at 15:11

First, if you use Integrate, you should define your function exactly. Don't use approximate numbers like 0.99005.

f[x_] = -(x/(10^20*(99005/100000 - Exp[x/10^12])))
Integrate[f[x], {x, 0, 10^9}]
(* Complicated exact expression involving Log and PolyLog *)


Evaluating this approximately indeed yields a small imaginary part.

%//N
(* 0.471182 - 2.86155*10^-12 I *)


However, this is an artifact of incomplete cancellation of the constant imaginary parts of the PolyLog component of the indefinite integral in step-by-step numerical evaluation. If, instead of asking for approximate evaluation, you ask a different question, you can show the result is real:

FullSimplify[Im[Integrate[f[x], {x, 0, 10^9}]]] == 0
(* True *)

• For the general problem I use symbolic integration and then replace the variables with their value at the end but I still get this problem. Is it possible to make mathematica compute the integral properly? May 26, 2016 at 12:03
• It computes the integral properly, and if you replace the variables with exact values you will get an exact (but complicated) result. If you replace the variables with inexact values, or use N[] to convert them, inexact arithmetic may fail to cancel out the imaginary part (and will be similarly inexact along the real axis). However, since you can show that the imaginary part is exactly zero (see above), you may simply use Re[] to get rid of the imaginary part if that matters. May 26, 2016 at 12:20
• No need to show anything anymore: N[(-200*(1 + 1000*Log[20000] - 1000*Log[-19801 + 20000*E^(1/1000)] - 10 00000*PolyLog[2, 19801/20000] + 1000000*PolyLog[2, 19801/(20000*E^(1/1000))]))/19801]' gives 0.47118211649034336 without any complex stuff. Dec 29, 2021 at 2:48