I'm attempting to implement the variation of parameters method of solving a linear nonhomogeneous ODE (as a coding problem to help learn Mathematica). As a first step, I would like to form the characteristic equation of my ODE function.
My test ODE is:
f[x_, y_] := y'' + 3 y' - 2 y - Sin[E^-x]
Which has the characteristic equation:
s^2 + 3s - 2 = 0
My naive attempt at getting the characteristic equation is:
variationOfParameters[fun_] :=
Plus @@ Cases[fun[x, y], _.*y | _.*_Derivative[y]] /. {yPrime :_Derivative[y] ->
s^Extract[FullForm[yPrime],
Position[FullForm[yPrime], _Integer]], y -> 1}
The Cases part works, the ReplaceAll however does not (output is an empty list). But when I test the rule manually it produces the correct answer, e.g.
Extract[FullForm[y''],
Position[FullForm[y''], _Integer]]
(*{2}*)
What am I doing wrong? Additionally if you have any advice on making this function better, I would be happy to hear it.
Thank you.
