Integral of trigonometric function gives different answer [duplicate]

I am working on an integral on the following trigonometric functions

$$\int_{-\pi}^\pi \frac{\cos[(4m+2)x] \cos[(4m+1)x]}{\cos x}dx$$

where $m$ is positive integer. I am running the following code in mathematica

Assuming[Element[m, Integers] && (m > 0),
Integrate[
Cos[(4 m + 2) x] Cos[(4 m + 1) x]*1/Cos[x], {x, -π, π}]]


It gives me result of $\pi$. Since $m$ could be any positive number, I expect the integral should be $\pi$ if I replace $m$ with an integer before the integral. However, I got zero if I replace $m$ with number first.

• @DanielLichtblau - I think there may be something more subtle going on here. I get an error in evaluating this integral Sep 26, 2016 at 21:23
• @Mikado Okay, I'll withdraw my close vote. There are some overlapping issues with the Assumptions/Assuming posts that have come up in past, nd also with the Assumptions->Element[something,Integers] posts. But I see now that this also involves a singularity that the assumptions will not appropriately handle, so there are subtleties. And of course a memory exception is not an anticipated outcome here. (I have not seen that result yet, because my evaluation has been running for minutes with no outcome yet). Sep 26, 2016 at 22:04

The problem is very similar to that of [1] How to compute the integral of trigonometic function with multiple angle

Hence we don't repeat what we have discussed there but both answers should be read as complementary.

Let us focus here on the question of convergence of the integral.

The integrand is

g2[m_, x_] := Cos[(4 m + 2) x] Cos[(4 m + 1) x]*1/Cos[x];


The interesting values of x are those at which the Cos[x] becomes zero, i.e. at x = -π/2 and x = + π/2.

A series expansion about these points gives

Normal[Series[g2[m, x], {x, π/2, 0}]]

(* Out[184]= -Cos[2 m π]^2 - 4 m Cos[2 m π]^2 - (
Cos[2 m π] Sin[2 m π])/(-(π/2) + x) + 2 Sin[2 m π]^2 +
4 m Sin[2 m π]^2 *)


For better visibility in LaTeX:

$$-\frac{\sin (2 \pi m) \cos (2 \pi m)}{x-\frac{\pi }{2}}+4 m \sin ^2(2 \pi m)+2 \sin ^2(2 \pi m)-4 m \cos ^2(2 \pi m)-\cos ^2(2 \pi m)$$

and

Normal[Series[g2[m, x], {x, -π/2, 0}]]

(* Out[185]= -Cos[2 m π]^2 - 4 m Cos[2 m π]^2 + (
Cos[2 m π] Sin[2 m π])/(π/2 + x) + 2 Sin[2 m π]^2 +
4 m Sin[2 m π]^2 *)


LaTeX:

$$\frac{\sin (2 \pi m) \cos (2 \pi m)}{x+\frac{\pi }{2}}+4 m \sin ^2(2 \pi m)+2 \sin ^2(2 \pi m)-4 m \cos ^2(2 \pi m)-\cos ^2(2 \pi m)$$

These expressions remain finite if the numerator

num2 = Cos[2 m π] Sin[2 m π];


of the pole is zero.

This means

Simplify[num2]

(* Out[187]= 1/2 Sin[4 m π] *)

Reduce[% == 0, m] /. C[1] -> n

(* Out[188]= n ∈ Integers && (m == n/2 || m == (π + 2 n π)/(4 π)) *)


Which means that m must be of the form m = n/4 with n integer.

The integral of the OP is

f2[m_] := Integrate[g2[m, x], {x, -π, π}]


The first few values are

Table[{m/4, f2[m/4]}, {m, -5, 5}]

(* Out[195]= {{-(5/4), 0}, {-1, 2 π}, {-(3/4), 0}, {-(1/2), 2 π}, {-(1/4),
2 π}, {0, 0}, {1/4, 2 π}, {1/2, 0}, {3/4, 2 π}, {1, 0}, {5/4,
2 π}} *)


For values of m which are not integer multiples of 1/4 the integral is divergent.

For the OP [1] the convergence condition is m = n/2, n integer.

• @ Dr. WolfangHintze : Something to adjust because of $\sin(2\pi m)=0$ for each integer $m.$ Sep 23, 2016 at 13:11
• Integer multiples of quarters include halfs and integers. Sep 23, 2016 at 13:13
• @ Dr. Wolfang Hintze : Sorry, don't understand it. Look at the formula under " For better visibility is LaTeX " in your answer. Sep 23, 2016 at 13:18
• Also Assuming[Element[m, Integers] && (m > 0), Limit[Cos[(4 m + 2) x] Cos[(4 m + 1) x]*1/Cos[x], x -> Pi/2]] ouputs -1-4m. Sep 23, 2016 at 13:22
• Thanks all. It makes sense to expand the integrand to figure the singularities. I just wonder why mathematica will directly given the answer $2\pi$ without considering the divergence. In what assumption mathematica makes to give that result. Sep 23, 2016 at 20:42

Solution #2: proof

Here's the missing proof that the integral

$$f(\text{m})=\int_{-\pi }^{\pi } \sec (x) \cos ((4 m+1) x) \cos ((4 m+2) x) \, dx$$

is zero for m = 0, 1, 2, ...

Following the idea of Yarchik in How to compute the integral of trigonometic function with multiple angle we observe that the integrand can be represented as the finite sum

s = -Sum[(-1)^n Cos[2 n (x)], {n, 1, 4 m + 1}];


Indeed

FullSimplify[s == Cos[x (4 m + 1)] Cos[x (4 m + 2)] Sec[x], m ∈ Integers]

(* True *)


Now the integral over s can be done for each summand separately which is

Integrate[Cos[2 n x], {x, -π, π}]
Simplify[%, {n ∈ Integers, n > 0}]

(* Out[288]= Sin[2 n π]/n *)

(* Out[289]= 0 *)


Now a finite sum of zeroes is zero. Hence the integral over s is zero and so is the original integral. QED.

Remark

Examples of finite sums over trigonometric functions of the type discussed here can be found in Gradshteyn/Ryshik 1.341

• Gradshteyn&Ryzhik 1.341 does not include the formula suggested by Yarchik (see dropbox.com/s/5p6cpdetf9l3mtf/screen26.09.16.docx?dl=0 ) Sep 26, 2016 at 19:09
• Yes, you are right, this example is not contained in GR. That's is why I provided it after some playing around with the sum in Mathematica. Sep 27, 2016 at 7:44

When I execute the code, I get an error. Have we found some sort of bug?

\$Version
Assuming[Element[m, Integers] && (m > 0),
Integrate[
Cos[(4 m + 2) x] Cos[(4 m + 1) x]*1/Cos[x], {x, -π, π}]]
(* "11.0.0 for Linux x86 (64-bit) (July 28, 2016)" *)

(* Throw::sysexc: Uncaught SystemException returned to top level. Can be caught with Catch[…, _SystemException]. *)

(* SystemException["MemoryAllocationFailure"] *)

• Maybe. A memory exception is certainly not what one would anticipate here. Sep 26, 2016 at 22:01