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[1] Cos[Sqrt[k] x] + C[2] Sin[Sqrt[k] x]}}

How can I get the general solution in the form

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

Just a simple trigonometric simplification.

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

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

I cheated a little here.

Btw, I do not think your formula is correct. It should be

Mathematica graphics

(page 167, Differential equations and their applications, 4th edition. By Braun).

Hence, you can do


Mathematica graphics


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

Reference: Combining cosine or sine terms into a single cosine or sine


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.