# Why does this integral of a real, analytic, absolutely integrable function give a complex result?

I am using Mathematica to develop some interesting" problems for students to solve using Fourier series.

The following computation seems as though it should yield a real result:

$$B_n = \int_0^1 \exp(-9 x^2) \cos(n \pi x)\, dx~~ n\in Integers,~n\ge 0$$

When I code this in Mathematica, I find it returns a complex result, which does not seem plausible. Here is the code for the first term $$(n=0)$$ (which is simpler than the general case):

B[0] = Integrate[Exp[-9x^2], {x, 0, 1}, Assumptions -> {x ∈ Reals}]


This returns $$\tfrac{1}{6}\sqrt{\pi} \,\textrm{erf}{(3)}$$, which is the correct answer.

However, when I compute the integral for the values of $$n>0$$, I find the following:

B[n_] = 2 Integrate[Exp[-9x^2] Cos[n Pi x], {x, 0, 1},
Assumptions -> {n ∈ Integers && n > 0 &&  x ∈ Reals}]


Returns: $$\frac{1}{6} \sqrt{\pi } e^{-\frac{1}{36} \pi ^2 n^2} \left[\text{erf}\left(3-\frac{i \pi n}{6}\right)+\text{erf}\left(3+\frac{i \pi n}{6}\right)\right]$$

This is a bit baffling. I see no reason that we should have wandered into the complex plane to compute this integral. Anyone have some perspective here?

Thanks.

• All the imaginary parts are zero, so in fact it's a real result expressed in terms of complex numbers. Oct 24, 2020 at 17:50
• I noticed that, but I am unable to prove it. In other words, there should be a simple way to explicitly show that the complex terms cancel. So far, I am not able to do so. Any ideas would help! Oct 24, 2020 at 17:57
• I think it follows from Conjugate[Erf[Conjugate[z]]] // FullSimplify. (See for example, the Schwarz reflection principle and the "identity theorem".) Oct 24, 2020 at 18:11
• Another "proof": 1/6 E^(-(1/36) n^2 \[Pi]^2) Sqrt[\[Pi]] (Erf[Conjugate[z]] + Erf[z]) // Im // FullSimplify[#, n > 0 && n \[Element] Integers] & Oct 24, 2020 at 18:20
• As for how the complex components arose, I think TrigToExp[Exp[-9x^2] Cos[n Pi x]] puts the integrand in a form that reveals where Erf[] with a complex argument comes from. Oct 24, 2020 at 19:15

First, Erf[z] + Erf[Conjugate[z]] is real since Conjugate[Erf[Conjugate[z]]] == Erf[z]:

Conjugate[Erf[Conjugate[z]]] // FullSimplify

(*  Erf[z]  *)


The following shows how one might come up with the Mathematica solution by hand.

First expand the integrand in terms of the exponential function:

Exp[-9 x^2] Cos[n Pi x] // TrigToExp

(*  1/2 E^(-I n π x - 9 x^2) + 1/2 E^(I n π x - 9 x^2)  *)


Each term is of the form E^(I 2 B x - A^2 x^2), with A and B real. The two terms differ in the sign of B. Each term has an integral in terms of Erf and Erfi, and the Erfi from each term cancel out.

Integrate[ E^(I 2 B x - A^2 x^2), {x, 0, 1},
Assumptions -> A > 0 && B ∈ Reals]
% /. B -> -B
% + %% // Simplify


$$\frac{\sqrt{\pi } e^{-\frac{B^2}{A^2}} \left(\text{erf}\left(A-\frac{i B}{A}\right)+i\, \text{erfi}\left(\frac{B}{A}\right)\right)}{2 A}$$

$$\frac{\sqrt{\pi } e^{-\frac{B^2}{A^2}} \left(\text{erf}\left(A+\frac{i B}{A}\right)-i\, \text{erfi}\left(\frac{B}{A}\right)\right)}{2 A}$$

$$\frac{\sqrt{\pi } e^{-\frac{B^2}{A^2}} \left(\text{erf}\left(A-\frac{i B}{A}\right)+\text{erf}\left(A+\frac{i B}{A}\right)\right)}{2 A}$$