# Extract coefficients from ODE

I'm trying to extract the coefficients from an ODE $$y''''+y''+x^2 y'-6y+ 8\ cos(x)$$
and hoping the code will give a list with all the coefficients in, ranging from lower order to higher order, i.e. $$\{8\ cos(x)\,,\,-6y\,,\,x^2\,,\, 1\,,\,1\}$$

So far I can only extract the $$x^2$$ and $$-6$$ in $$x^2 y'$$ and $$-6y$$ using

In : Cases[ y'''' + y'' + x^2 y' - 6 y + 8 Cos[x],
Times[x_, Derivative[_][_]] :> x, Infinity]
Out: {x^2}

In : Cases[ y'''' + y' + x^2 y' - 6 y + 8 Cos[x], x__ y :> x, Infinity]
Out: {-6}


How can I deal with the $$y'''$$ and $$y''$$ which do not have the Times header before them? I've tried this one which just return me all the $$y^{(n)}$$s.

In: Cases[ y'''' + y'' + x^2 y' - 6 y + 8 Cos[x],
Derivative[_][y], Infinity]
Out: {y',y'',y''''}

• not very clean but it works: CoefficientList[expression /. y -> (Exp[α #] &) /. (q_. Exp[α #] &) -> q, α] or CoefficientList[expression /. y -> (Exp[α #] &) /. Function[q_] -> q Exp[-α] /. Slot[_] -> 1, α]. I'm sure there are better ways though. Nov 24, 2019 at 2:52

Given that it's linear, I would probably do it this way:

CoefficientList[
y'''' + y'' + x^2 y' - 6 y + 8 Cos[x] /.
Derivative[n_][y] :> y^(n + 1), {y}]
(*  {8 Cos[x], -6, x^2, 1, 0, 1}  *)


Note that the output here has the coefficient of y''', which the desired output in the OP omits. I see every reason to include it, though.

More straightforward:

Flatten@CoefficientArrays[y'''' + y'' + x^2 y' - 6 y + 8 Cos[x],
Table[Derivative[n][y], {n, 0, 4}]]
(*  {8 Cos[x], -6, x^2, 1, 0, 1}  *)

• But the code will confuse between y and y'.... Nov 24, 2019 at 5:12
• @WeiShanNg Oops. make it n+1. Nov 24, 2019 at 13:50

The solution is two step:

expression==y'''' + y'' + x^2 y' - 6 y + 8 Cos[x]


For the coeffecients of the function and the derivatives:

If[Length[#] > 1, #[],
0] & /@ (CoefficientList[expression, #] & /@ {y, y', y'', y''',
y''''})


For the inhomogeneity:

expression - (If[Length[#] > 1, #[],
0] & /@ (CoefficientList[expression, #] & /@ {y, y', y'', y''',
y''''}).{y, y', y'', y''', y''''})


There remains the problem whether the the highest order should be detect programmatical or not. This can be done by using the first solution of @Michael-e2.

Length@CoefficientList[
y'''' + y'' + x^2 y' - 6 y + 8 Cos[x] /.
Derivative[n_][y] :> y^(n + 1), {y}]-2
(*4*)


With that

   dlist=Table[Derivative[n_][y],{n,0,Length@CoefficientList[
y'''' + y'' + x^2 y' - 6 y + 8 Cos[x] /.
Derivative[n_][y] :> y^(n + 1), {y}]-2}]
(*{y, Derivative[y], y^\[Prime]\[Prime],
\!$$\*SuperscriptBox[\(y$$,
TagBox[
RowBox[{"(", "3", ")"}],
Derivative],
MultilineFunction->None]\),
\!$$\*SuperscriptBox[\(y$$,
TagBox[
RowBox[{"(", "4", ")"}],
Derivative],
MultilineFunction->None]\)}*)


The two inputs are for the coefficients of the function and the derivative:

coeffs = If[Length[#] > 1, #[],
0] & /@ (CoefficientList[expression, #] & /@
Table[Derivative[n][y], {n, 0,
Length@CoefficientList[
expression /. Derivative[n_][y] :> y^(n + 1), {y}] - 2}])


and for the arbitrary not on y dependent part:

expression - (If[Length[#] > 1, #[],
0] & /@ (CoefficientList[expression, #] & /@
Table[Derivative[n][y], {n, 0,
Length@CoefficientList[
expression /. Derivative[n_][y] :> y^(n + 1), {y}] -
2}]).Table[
Derivative[n][y], {n, 0,
Length@CoefficientList[
expression /. Derivative[n_][y] :> y^(n + 1), {y}] - 2}])


An intermediate coefficient function for a derivative order is allowed to be zero and the highest order of the derivative is set arbitrary. The type of the inhomogeneity can be arbitrary too. It is not investigated and derived but substraction only.

The premises are linearity and ODE and there is not functional dependences for the function or the derivatives in the suppose left or right hand side of the ODE or in the expression, term.