# How can Mathematica simplify this trigonometric function?

This question must be too simple, but it confused me for several days. I want to know how can I do the following simplification using Mathematica:

For example, convert m Sin[x] + n Cos[x] + p to a Sin[w x + b] + c.

Note: I've tried some built-in functions such as Simplify,FullSimplify,TrigReduce but none of those worked for me. Can anyone give a solution?

• – eyorble Mar 12 '18 at 9:38
• This is fairly simple. Based on your question, the frequency is the same, so yea this should be possible. You could do the following: ExpToTrig[FullSimplify[TrigToExp[<Your Expression Here>]]] – Brett Haupt Mar 12 '18 at 10:12
• @BrettHaupt your example does not work with OP's code. – Kuba Mar 12 '18 at 12:07
• @HenrikSchumacher You are right. Thank you for point out that error! I've changed my question. – WW00WW Mar 12 '18 at 12:15
• @BrettHaupt Thank you for your answer! I replaced m nand pwith real number and implemented your code. Then I got my origin expression yet. So is there anything wrong? – WW00WW Mar 12 '18 at 12:22

Use

replacerule = m_ Sin[x_] + n_ Cos[x_] :> Sqrt[m^2 + n^2] Sin[x + ArcTan[m, n]];


With this,

m Sin[x] + n Cos[x] + p /. replacerule
(* p + Sqrt[m^2 + n^2] Sin[x + ArcTan[m, n]] *)


For a more general problem, you can use

trigTogether[exp_] := FullSimplify[exp,
TransformationFunctions -> {Automatic, # /. replacerule &},
ComplexityFunction -> ((LeafCount[#] + 100 Count[#, (_Sin | _Cos | E^_), {0, Infinity}]) &)
]


so that

trigTogether[m Sin[x] + n Cos[x] + c + Log[a Sin[y^2] + b Cos[y^2]]]
(* c + Log[Sqrt[a^2 + b^2] Sin[y^2 + ArcTan[a, b]]] + Sqrt[m^2 + n^2] Sin[x + ArcTan[m, n]] *)


There are two or three tricks that we can use to solve for {a, b, c, ω}. First, of course, we write our equation. But, since that is only one equation, we will take one or more derivatives to generate additional equations. Then, we will substitute certain values for $x$. The result will be a set of equations that Mathematica can handle.

Specifically, we write our equation, take the second derivative and evaluate both equatinos at $x = 0$ and at $x=\pi$. This brings $\omega$ out into the open. We also use TrigExpand.

lhs = m Sin[x] + n Cos[x] + p ;
rhs = a Sin[ω x + b] + c;

eqn = {lhs == rhs, D[lhs, x, x] == D[rhs, x, x]};
eqs = {eqn /. x -> 0, eqn /. x -> π} // Flatten // TrigExpand;
eqs // Column

(*  {
n + p == c + a Sin[b],
-n     == -a ω^2 Sin[b],
-n + p == c + a Cos[π ω] Sin[b] + a Cos[b] Sin[π ω],
n     == -a ω^2 Cos[π ω] Sin[b] - a ω^2 Cos[b] Sin[π ω]
}  *)


Now, we eliminate Sin[b] and Cos[b] and solve for $\omega$, like this

eq2 = List @@
Eliminate[eqs /. Cos[b] -> ρ /. Sin[b] -> σ, {ρ, σ}];
eq2 // Column
Reduce[eq2, ω]

(*  {
c n   == n p,
c ω^2 == p ω^2,
n (-1 + ω^2) == 0
}

(c == p && ω == -1) || (c == p && ω == 1) ||
(n == 0 && c == p)  || (n == 0 && ω == 0)
*)


So, if $n\neq 0$, we must have $c = p$ and $\omega = 1$ or $\omega=-1$. We note that if $n=0$ then we don't have a solution for $\omega$. We also note that when we used Eliminate we introduced the tacit assumptions that $\sin b\neq 0$ and $\cos b\neq 0$.

All of that just to get $\omega = \pm 1$. Now, we can substitute $\omega \rightarrow 1$ into our equation, remind Mathematica to consider what happens at $x=0$ and at $x=\pi$, and then solve for variables $a, b, c$. We get two families of solutions, so we pick the one that is simplest.

eqs = eqn /. ω -> 1;
eq2 = {eqs, eqs /. x -> 0, eqs /. x -> π} // Flatten // TrigExpand;
soln = Solve[eq2, {a, b, c}];
Simplify[soln // Last, Assumptions -> C[1] == 0]

(* {
a -> Sqrt[m^2 + n^2], c -> p,
b -> ArcTan[m/Sqrt[m^2 + n^2], n/Sqrt[m^2 + n^2]]
} *)

• Thank you very much! This method through solving equation also inspired me too much. – WW00WW Mar 13 '18 at 14:13