# How to calculate the integral only in real domain?

I have the following integral:

Integrate[1/(Sqrt[2] + Cos[4 X] + Sin[4 X]), X,
Assumptions -> Element[X, Reals]] // Simplify

 (-((1 + 3 I) + Sqrt[2]) Cos[2 X] + ((1 + I) - I Sqrt[2]) Sin[2 X])/(
4 ((1 + I) + Sqrt[2]) Cos[2 X] + 4 I ((-1 - I) + Sqrt[2]) Sin[2 X])


The result appears to be very complex and it depends on I.

What I want is to integrate over real numbers and get a real solution. How can I do it?

• Welcome to Mathematica SE. To start: 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, since the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) consider accepting the answer, if any, that solves your problem, by clicking checkmark sign, 4) give help too, by answering questions in your areas of expertise.
– bmf
Jan 16 at 0:23

At first sight it seems surprising that one gets a result involving I, but this not especially surprising for those with a substantial practice in Mathematica. Our integrand can be recast in many ways and so can the result. Quite a straightforward way yielding a result in our case manifestly real exploits TrigFactor.

intd = 1/(Sqrt[2] + Cos[4 x] + Sin[4 x]);
intdf = TrigFactor[intd]

1/(Sqrt[2] + Sqrt[2] Sin[Pi/4 + 4 x])

int = Integrate[ intdf, x]

Sin[Pi/8 + 2 x]/(2 Sqrt[2] (Cos[Pi/8 + 2 x] + Sin[Pi/8 + 2 x]))


We need not playing with external packages to get a satisfactory result.

D[int, x] - intd // Simplify

0


The result is correct, but may be not optimal. This might be a grade "B" in cas integration tests:

ClearAll[x]
integrand = 1/(Sqrt[2] + Cos[4 x] + Sin[4 x]);
anti = Integrate[integrand, x];


Simplify[D[anti, x] - integrand]


You can try Rubi. This gets "A" grade because it is missing I in the anti

<< Rubi
anti = Int[integrand, x]


Simplify[D[anti, x] - integrand]


Clear["Global*"]

expr1 = 1/(Sqrt[2] + Cos[4 X] + Sin[4 X]);

sol1 = Assuming[Element[X, Reals],
Integrate[expr1, X] // FullSimplify]

(* -(((1 + I) + (2 I + Sqrt[2]) E^(4 I X))/((4 + 4 I) +
4 Sqrt[2] E^(4 I X))) *)


Verifying that sol1 is a valid anti-derivative

D[sol1, X] == expr1 // Simplify

(* True *)


The imaginary part of sol1 is a complex constant

I*Im[sol1] // ComplexExpand // FullSimplify

(* -(I/(4 Sqrt[2])) *)


Consequently, the imaginary part can be discarded

sol2 = (sol1 - I*Im[sol1]) // ComplexExpand // FullSimplify

(* -((2 + (1 + Sqrt[2]) Cos[4 X] + (-1 + Sqrt[2]) Sin[4 X])/
(4 (2 + Sqrt[2] Cos[4 X] + Sqrt[2] Sin[4 X]))) *)


Verifying that sol2 is a valid anti-derivative

D[sol2, X] == expr1 // Simplify

(* True *)


This is a nice example of a problem showing the interplay between mathematics and a CAS tool moderated by the user.

Appoach 1:

Simplify the integrand before integrating, and then integrate to find an obvious real result:

In[120]:= f[x_] := 1/(Sqrt[2] + Cos[4 x] + Sin[4 x])

In[123]:= g[x_] := 1/Sqrt[2] 1/(1 + Cos[4 x - \[Pi]/4])

In[124]:= f[x] == g[x] // Simplify

Out[124]= True

In[125]:= h[x_] = Integrate[g[x], x]

Out[125]= -(Tan[\[Pi]/8 - 2 x]/(4 Sqrt[2]))


Approach 2:

Find the antidrivative a[x] by integrating, as you have done. Then plot the real and imaginary part of that antiderivative, notice that the imaginary part seems to be constant, and finally find an equvalent antiderivative a1[x] by adding an appropriate constant as which we chose -a[0].

In[126]:= a[x_] = Integrate[1/(Sqrt[2] + Cos[4 x] + Sin[4 x]), x]

Out[126]= (-((1 + 3 I) + Sqrt[2]) Cos[
2 x] + ((1 + I) - I Sqrt[2]) Sin[2 x])/(
4 ((1 + I) + Sqrt[2]) Cos[2 x] + 4 I ((-1 - I) + Sqrt[2]) Sin[2 x])

In[128]:= a1[x_] = a[x] - a[0] // Simplify

Out[128]= Sin[2 x]/(2 ((1 + Sqrt[2]) Cos[2 x] + Sin[2 x]))


This quantity is a real antiderivative, and this also confirms that the imaginary part was really constant.

See that the results are equvalent

In[145]:= a1'[x] == h'[x] // FullSimplify

Out[145]= True