# How to change the form $A\cos \omega t + B\sin \omega t$ into the form $\sqrt{A^2+B^2}\cos(\omega t-\phi)$ [duplicate]

I'm preparing notes for teaching next week's class in differential equations. My hand calculation solution of $$y''+192y=0, y(0)=1/6, y'(0)=-1$$ is $$y=\frac16\cos 8\sqrt3 t-\frac{\sqrt3}{24}\sin 8\sqrt3 t$$ Now a few more hand calculations provides this equivalent solution: $$y=\frac{\sqrt{19}}{24}\cos(8\sqrt3 t-\tan^{-1}(-\sqrt3/4))$$

Now, when we solve the equation using DSolveValue, we get the following result.

sol = DSolveValue[{u''[t] + 192 u[t] == 0, u[0] == 1/6, u'[0] == -1}, u[t],
t]


Out[276]= 1/24 (4 Cos[8 Sqrt[3] t] - Sqrt[3] Sin[8 Sqrt[3] t])

Which agrees with my hand calculated solution. Moreover, I was able to check that my final form was equivalent to the output provided by DSolveValue.

sol - Sqrt[19]/24*Cos[8 Sqrt[3] t - ArcTan[-Sqrt[3]/4]] // Simplify


Out[293]= 0

Now my question. Is there a simple way in Mathematica to convert the expression

$$\frac16\cos 8\sqrt3 t-\frac{\sqrt3}{24}\sin 8\sqrt3 t$$

to

$$\frac{\sqrt{19}}{24}\cos(8\sqrt3 t-\tan^{-1}(-\sqrt3/4))$$

These are the hand calculations used to make the change.

• How did you come up with that little graph to make that simplification? It definitely works but I'm not sure what method you are using there? – user32882 Dec 12 '16 at 10:45
• @user32882 If you have an expression such as $a\cos\theta+b\sin\theta$, the first step is to factor out $\sqrt{a^2+b^2}$, giving $\sqrt{a^2+b^2}(\frac{a}{\sqrt{a^2+b^2}}\cos\theta+\frac{b}{\sqrt{a^2+b^2}}\sin\theta)$. Then, using my picture, you can replace $\frac{a}{\sqrt{a^2+b^2}}$ with $\cos\phi$ and $\frac{b}{\sqrt{a^2+b^2}}$ with $\sin\phi$. – David Dec 13 '16 at 17:06

Not sure this is what you need :

A = 1/6; B = -Sqrt[3]/24; ω = 8 Sqrt[3];
r = Sqrt[A^2 + B^2]
ClearAll[t];
sol = Solve[ A Cos[ω t] + B Sin[ω t] == r Cos[ω t - ϕ], ϕ];
t = 0;
ϕ /. sol[[1]]
(* ConditionalExpression[-ArcCos[4/Sqrt[19]] - 2 π C[1],  C[1] ∈ Integers] *)


Clear[TrigShrink];
TrigShrink[exp_, trgt_, lag_: ϕ] :=
Module[{sign, xExp, xRes, xTrig, cCos, cSin, tan, xlag, clag},
xExp = exp // TrigExpand // Collect[#, {Sin[trgt], Cos[trgt]}] &;
xRes = xExp /. {Sin[trgt] -> 0, Cos[trgt] -> 0};
xTrig = xExp - xRes;
cCos = xTrig /. {Sin[trgt] -> 0, Cos[trgt] -> 1};
cSin = xTrig /. {Sin[trgt] -> 1, Cos[trgt] -> 0};
sign = If[Sign[cSin] // NumberQ, Sign[cSin], 1];
xlag = ArcTan[cCos/cSin] // Simplify;
clag = sign Sqrt[cSin^2 + cCos^2] // Simplify;
{C[lag] Sin[trgt + lag] + xRes, lag -> xlag, C[lag] -> clag} //
Simplify // Flatten
]


Here are some test cases:

TrigShrink[2 Sin[x + α] + Sin[x + 8] + Cos[t], x, θ]
(*res: {Cos[t] + C[θ] Sin[x + θ], θ ->ArcTan[(Sin[8] + 2 Sin[α])/(Cos[8] + 2 Cos[α])], C[θ] -> Sqrt[5 + 4 Cos[8 - α]]}*)

TrigShrink[ Sin[x] + Cos[x], x, θ]
(*{C[θ] Sin[x + θ], θ -> π/4, C[θ] -> Sqrt[2]}*)


TrigShrink[1/24 (4 Cos[x] - Sqrt[3] Sin[x]), x, θ] /. x -> 8 Sqrt[3] t