1
$\begingroup$

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]]}}
$\endgroup$
0
$\begingroup$

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]] *)
$\endgroup$
2
$\begingroup$

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

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

Mathematica graphics

Where

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

$\endgroup$

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.