# Bug? Analytical integration of cosines gets the sign wrong

Discussion in 'Mathematica' started by K, Feb 4, 2010.

1. ### KGuest

Hello everyone,

the analytical integration in Mathematica 7.01.0 on Linux x86 (64bit)

faultyInt =
Integrate[Cos[ph]*1/Pi*Cos[4*ph]*Cos[2*ph], {ph, Pi, 3/2*Pi}]

gives as result:

19/(105 \[Pi])

which is as a decimal number

N[faultyInt]

0.0575989

The numerical integration

NIntegrate[Cos[ph]*1/Pi*Cos[4*ph]*Cos[2*ph],{ph,Pi,3/2*Pi}]

gives

-0.0575989

which I believe is correct by judging from the plot

Plot[Cos[ph]*1/Pi*Cos[4*ph]*Cos[2*ph], {ph, Pi, 3/2*Pi},
PlotRange -> {-1/Pi, 1/Pi}]

and because the quadgk function in another system gives the same
negative result. Could anyone try this at home (or work, rather)
and confirm or disprove it?
Thanks,
K.

K, Feb 4, 2010

2. ### Norbert P.Guest

Not sure what's going on,
but I' m getting the same thing on 7.0.1 on Win32:

In :=
Integrate[Cos[ph]*1/Pi*Cos[4*ph]*Cos[2*ph], {ph, Pi, 3/2*Pi}]
Out = 19/(105 \[Pi])

But the indefinite integral is correct:

In :=
Integrate[Cos[ph]*1/Pi*Cos[4*ph]*Cos[2*ph],
ph] /. {{ph -> 3/2 Pi}, {ph -> Pi}}
Out = {-(19/(105 \[Pi])), 0}

In := D[Integrate[Cos[ph]*1/Pi*Cos[4*ph]*Cos[2*ph], ph], ph] ==
Cos[ph]*1/Pi*Cos[4*ph]*Cos[2*ph] // FullSimplify
Out = True

Best,
Norbert

Norbert P., Feb 5, 2010

3. ### DCGuest

Same system, same result, but

In:= int = Integrate[Cos[ph]*1/Pi*Cos[4*ph]*Cos[2*ph], ph]

Out= (2 Sin[ph] + 2/3 Sin[3 ph] + 2/5 Sin[5 ph] +
2/7 Sin[7 ph])/(8 \[Pi])

In:= (int /. ph -> 3/2 Pi) - (int /. ph -> Pi)

Out= -(19/(105 \[Pi]))

-Francesco

DC, Feb 5, 2010
4. On my version (7.0 for Microsoft Windows (32-bit) (February 18, 2009))
I get the same issue.

Moreover, it is very specific to the functional form, so that it
appears to be an algebraic error.
Or, perhaps, it's a feature...

5. ### Sjoerd C. de VriesGuest

I get the same on my winxp32 machine. Looks like a bug to me...

Cheers -- Sjoerd

Sjoerd C. de Vries, Feb 5, 2010
6. ### FredGuest

K,

I get the same wrong answer with Mathematica 7.0.1.0 under Windows 64-bit

In:=
f[x_] = Cos[x]*Cos[2*x]*Cos[4*x]/Pi;
Integrate[f[x], {x, Pi, 3 Pi/2}] // N
NIntegrate[f[x], {x, Pi, 3 Pi/2}]

Out= 0.0575989
Out= -0.0575989

Mathematica does get it right if we use indefinite integral

In:=
fPrim[x_] = Integrate[f[x], x];
fPrim[3 Pi/2] - fPrim[Pi] // N

Out= -0.0575989

Mathematica even gets it right if we use the definite integral with bounds
not involving Pi?

In:=
lowerbound = 3.141592654;
upperbound = 3 lowerbound/2;
Integrate[f[x], {x, lowerbound, upperbound}] // N

Out= -0.0575989

IMHO this is a bug, and should be reported to http://support.wolfram.com/submitabug.cgi

Regarde Arnold Smit

Fred, Feb 5, 2010
7. ### WetBlanketGuest

If One substitutes the sequence, {1.9, 1.99, 1.999, ... 2.0}
Mathematica gets the correct answer for as long as I tried except, of
course for 2.0.

WetBlanket, Feb 10, 2010
8. ### DrMajorBobGuest

That's a VERY simple integrand; for Mathematica to get it wrong is
worrisome.

Below is an analytical confirmation of the NIntegrate result.

expr = Cos[ph]*1/Pi*Cos[4*ph]*Cos[2*ph];
Integrate[expr, {ph, Pi, 3/2*Pi}]

19/(105 \[Pi])

expr2 = Cos[ph]*1/Pi*Cos[4*ph]*Cos[2*ph] // TrigReduce
Integrate[expr2, {ph, Pi, 3/2*Pi}]

(Cos[ph] + Cos[3 ph] + Cos[5 ph] + Cos[7 ph])/(4 \[Pi])

-(19/(105 \[Pi]))

Bobby

DrMajorBob, Feb 11, 2010
9. ### Louis TalmanGuest

Actually, something very strange---even stranger than has already been
pointed out---is going on.

Try, in version 7 of Mathematica (be prepared to wait a little while):

--------------------
f[x_] := Integrate[Cos[ t ] Cos[ 2 t ] Cos[ 4 t ], { t, Pi, x }]

Plot[ f[x], {x, Pi, 2 Pi}, PlotRange -> All ]
--------------------

Then compare the resulting plot with the one obtained from

--------------------
F[x_] = Integrate[ Cos[ x ] Cos[ 2 x ] Cos[ 4 x ], x]

Plot[ F[x] - F[Pi], { x, Pi, 2 Pi}, PlotRange -> All ]
--------------------

On my PPC iMac running OS X, v10.5.8, the first plot is the reflection
of the second plot about the x-axis.

Louis Talman, Feb 11, 2010
10. ### Tony HarkerGuest

This looks like a bug, and one that crept in between versions 6 and 7.
Interestingly,

Integrate[
TrigExpand[Cos[ph]*1/Pi*Cos[4*ph]*Cos[2*ph]], {ph, Pi, 3/2*Pi}]

gives the right answer [7.0 for Microsoft Windows (32-bit) (February 18,
2009)].

Tony

]-> -----Original Message-----
]-> From: WetBlanket [mailto:]
]-> Sent: 10 February 2010 08:35
]-> To:
]-> Subject: Re: Bug? Analytical integration of
]-> cosines gets the sign wrong
]->
]-> > Hello everyone,
]-> >
]-> > the analytical integration in Mathematica 7.01.0 on Linux
]-> x86 (64bit)
]-> >
]-> > faultyInt =
]-> > Integrate[Cos[ph]*1/Pi*Cos[4*ph]*Cos[2*ph], {ph, Pi, 3/2*Pi}]
]-> >
]-> > gives as result:
]-> >
]-> > 19/(105 \[Pi])
]-> >
]-> > which is as a decimal number
]-> >
]-> > N[faultyInt]
]-> >
]-> > 0.0575989
]-> >
]-> > The numerical integration
]-> >
]-> > NIntegrate[Cos[ph]*1/Pi*Cos[4*ph]*Cos[2*ph],{ph,Pi,3/2*Pi}]
]-> >
]-> > gives
]-> >
]-> > -0.0575989
]-> >
]-> > which I believe is correct by judging from the plot
]-> >
]-> > Plot[Cos[ph]*1/Pi*Cos[4*ph]*Cos[2*ph], {ph, Pi, 3/2*Pi},
]-> > PlotRange -> {-1/Pi, 1/Pi}]
]-> >
]-> > and because the quadgk function in another system gives the same
]-> > negative result. Could anyone try this at home (or work, rather)
]-> > and confirm or disprove it?
]-> > Thanks,
]-> > K.
]-> If One substitutes the sequence, {1.9, 1.99, 1.999, ... 2.0}
]-> Mathematica gets the correct answer for as long as I tried
]-> except, of
]-> course for 2.0.
]->
]->

Tony Harker, Feb 11, 2010
11. ### Steve LuttrellGuest

Plot[f[x] // Evaluate, {x, Pi, 2 Pi}, PlotRange -> All]

would plot much quicker because it evaluates the Integrate once and for all
inside the Plot.

Alternatively, omit the // Evaluate, and use = rather than := in the
definition of f[x], which evaluates the Integrate once and for all in the
definition of f[x].

--
Stephen Luttrell
West Malvern, UK

Steve Luttrell, Feb 14, 2010