# Simplify an integral involving trigonometric functions

We were doing variation of parameters in differential equations tonight and had to do the following integral, the result given by hand calculations.

$$\int \cos t\tan^2 t\,dt=\ln|\sec t+\tan t|-\sin t$$

The students then tried the following in Mathematica:

v2 = Integrate[Cos[t] Tan[t]^2, t]


and got the following result:

-Log[Cos[t/2] - Sin[t/2]] + Log[Cos[t/2] + Sin[t/2]] - Sin[t]


I was able to come home and send them some hand calculated steps to equate this to our solution (minus the absolute value), but I was unable to show them equivalency using Mathematica.

Any suggestions?

-
v1 = Cos[t] Tan[t]^2; Integrate[v1, t]; D[v2, t] == v1 // Simplify – Dr. belisarius Mar 10 '15 at 5:26
What is "our solution"? – Dr. belisarius Mar 10 '15 at 5:29

Looking at Mathematica result $-\log\left( \cos\frac{t}{2}-\sin\frac{t} {2}\right) +\log\left( \cos\frac{t}{2}+\sin\frac{t}{2}\right)$ and using $\log\left( \frac{x}{y}\right) =\log x-\log y$ then Mathematica result can be written as

$$\log\left( \frac{\cos\frac{t}{2}+\sin\frac{t}{2}}{\cos\frac{t}{2}-\sin \frac{t}{2}}\right) =\log\left( \frac{1+\tan\frac{t}{2}}{1-\tan\frac{t}{2} }\right)$$

Using $\tan\frac{t}{2}=\frac{\sin t}{1+\cos t}$ then above becomes

\begin{align*} \log\left( \frac{1+\frac{\sin t}{1+\cos t}}{1-\frac{\sin t}{1+\cos t}% }\right) & =\log\left( \frac{1+\cos t+\sin t}{1+\cos t-\sin t}\right) =\log\left( \frac{1+\sin t}{\cos t}\right) \\ & =\log\left( \frac{1}{\cos t}+\frac{\sin t}{\cos t}\right) =\log\left( \sec t+\tan t\right) \end{align*}

Which is what the book gives. But interestingly, Rubi 4.7 gives different answer to this integral.

Clear[t];
mmaResult = Integrate[Cos[t] Tan[t]^2, t];
rubiResult = Int[Cos[t] Tan[t]^2, t];
Grid[{{Plot[mmaResult, {t, -2 Pi, 2 Pi}, PlotRange -> All,
PlotLabel -> Column[{"Mathematica", mmaResult}]],
Plot[rubiResult, {t, -2 Pi, 2 Pi}, PlotRange -> All, PlotLabel ->
Column[{"Rubi", rubiResult}]]
}}, Frame -> All]


Rubi gives ArcTanh[Sin[t]] - Sin[t] It is left as an exercise to the reader to determine which is the correct answer.

-
Interesting. The commands Plot[-Sin[t] + Log[Sec[t] + Tan[t]], {t, -2 Pi, 2 Pi}, PlotRange -> All] and our hand calculated answer Plot[-Sin[t] + Log[Abs[Sec[t] + Tan[t]]], {t, -2 Pi, 2 Pi}, PlotRange -> All] give the same plots. Which is understandable because Reduce[Sec[t] + Tan[t] > 0, t [Element] Reals] // Simplify gives C[1] [Element] Integers && -([Pi]/2) < t - 2 [Pi] C[1] < [Pi]/2. By the way, is Rubi some sort of package you add to Mathematica. I ask this as I see you use the command rubiResult = Int[Cos[t] Tan[t]^2, t]; in your Mathematica code. – David Mar 10 '15 at 17:08

The result is a neat trick relying on D[Sec[x]+Tan[x],x]=Sec[x]^2+Sec[x]Tan[x]

(1) Transform Tan-squared to 1-Sec[x]^2

(2) Expand

(3) Integrate Cos[t]-Sec[t]

First is simple and the second Sec[t] can be done using the trick

Multiply by (Sec[x]+Tan[x])/(Sec[x]+Tan[x])

(4) You'll see the substitution u=Sec[x]+Tan[x] and the form Integrate[du/u,u]

Mathematica gets the half angle formula by representing Sec[x]+Tan[x] in terms of Sin[x/2] and Cos[x/2].

-
Sorry just read your notebook. You are well aware of the trick - just how do we get Mathematica to do the same?! – iqchef Mar 10 '15 at 6:11
Using TrigFactor[] on Sec[x]+Tan[x] and (Cos[x/2]+Sin[x/2])/(Cos[x/2]-Sin[x/2]) gives equivalency. – iqchef Mar 10 '15 at 6:14
You can format inline code and code blocks by selecting it and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. – Michael E2 Apr 12 '15 at 1:28

Nasser:

Comparing plots of the MMA and Rubi integrations with that of the integrand indicates that the Rubi integration is the correct one, no?:

Clear[t];
integrand = Cos[t] Tan[t]^2;
mmaResult = Integrate[Cos[t] Tan[t]^2, t];
rubiResult = Int[Cos[t] Tan[t]^2, t];
Grid[{{Plot[mmaResult, {t, -2 Pi, 2 Pi}, PlotRange -> All,
PlotLabel ->
Style[Column[{"Mathematica", mmaResult}], FontSize -> 7]],
Plot[rubiResult, {t, -2 Pi, 2 Pi}, PlotRange -> All,
PlotLabel -> Column[{"Rubi", rubiResult}]],
Plot[integrand, {t, -2 Pi, 2 Pi}, PlotRange -> All,
PlotLabel -> Column[{"Integrand", integrand}]]}}, Frame -> All]


-

One should keep in mind that Mathematica anti-differentiates functions as functions of complex variables. Consequently one should expect two indefinite integrals to differ by a complex constant on any connected component of the domain over which the antiderivatives are continuous. In particular Log has a branch cut, and the integral has discontinuities (shown below).

The following shows that the difference between the "standard" antiderivative and Mathematica's is a different constant on different intervals. Mathematica has trouble simplifying, as the OP mentions. The two Simplify examples below show two constants that occur, but the do not work for arbitrary translations of t.

int1 = (Log@Abs[Sec[t] + Tan[t]] - Sin[t]);
int2 = Integrate[Cos[t] Tan[t]^2, t];

Simplify[int1 - int2, 0 < t < Pi/2]
(*  0  *)

Simplify[int1 - int2 /. t -> t + Pi/2, 0 < t < Pi/2]
(*  I π  *)


The result of Reduce shows that the difference is a constant, integer multiple of I Pi.

Reduce[
difference == int1 - int2 && 0 < t < 4 Pi,
{difference, t}
]
(*
(difference == 0 &&
(t == 2 π ||
2 π < t < 4 π - 4 ArcTan[1 + Sqrt[2]] ||
0 < t < -4 ArcTan[1 - Sqrt[2]] ||
4 π + 4 ArcTan[1 - Sqrt[2]] <= t < 4 π ||
4 ArcTan[1 + Sqrt[2]] <= t < 2 π)) ||
(difference == -I π &&
4 π - 4 ArcTan[1 + Sqrt[2]] < t < 4 π + 4 ArcTan[1 - Sqrt[2]]) ||
(difference == I π &&
-4 ArcTan[1 - Sqrt[2]] < t < 4 ArcTan[1 + Sqrt[2]])
*)


The plot of the real and imaginary parts of the difference illustrates the periodic discontinuities.

Plot[
Evaluate@Through[{Re, Im}[int1 - int2]],
{t, 0, 8 Pi}, PlotRange -> All]


-

Please let me know if I'm missing something but, contrary to what's been said above, it doesn't appear that the following two expressions are equal:

e1 = -Log[Cos[t/2] - Sin[t/2]] + Log[Cos[t/2] + Sin[t/2]]

e2 = Log[Sec[t] + Tan[t]]

e1 /. t -> Pi

gives: -i Pi

e2 /. t -> Pi

gives: i Pi

e1 /. t -> Pi/2.

gives: 37.0834

e2 /. t -> Pi/2.

gives: 38.025

EDIT: When t=Pi/2, the arguments of the logs are some very large numbers, so we may be getting round-off errors.

-
You can format inline code and code blocks by selecting it and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. – Michael E2 Apr 12 '15 at 0:28