# Change general solution form of the simple harmonic oscillator

When I try to solve the simple harmonic oscillator equation

DSolve[y''[x] + k*y[x] == 0, y[x], x]


the solution is given by:

{{y[x] -> C Cos[Sqrt[k] x] + C Sin[Sqrt[k] x]}}


How can I get the general solution in the form

{{y[x] -> C Cos[Sqrt[k] x + C]}}


Just a simple trigonometric simplification.

soly = DSolve[{y''[x] + k y[x] == 0, y == c0 Cos[phi0], y' == c0 Sqrt[k] Sin[phi0]}, y, x][] /. {c0 -> C, phi0 -> -C}
yx = y[x] /. soly // TrigReduce

(* C Cos[Sqrt[k] x + C] *)


I cheated a little here.

Btw, I do not think your formula is correct. It should be (page 167, Differential equations and their applications, 4th edition. By Braun).

Hence, you can do

ClearAll[x,y,k];
sol=y[x]/.First@DSolve[y''[x]+k*y[x]==0,y[x],x];
convert[sol,k,x] Where

convert[sol_, k_, x_] := Module[{a, b, expr},
expr = TrigExpand[sol];
b = CoefficientList[expr, Cos[Sqrt[k] x], 2][];
a = CoefficientList[expr, Sin[Sqrt[k] x], 2][];
Sqrt[a^2 + b^2] Cos[Sqrt[k] x - ArcTan[b, a]]
]