I have some higher-order ordinary differential equations.
Firstly, I want to collect the terms with common factors together. Besides, I want to re-sort the ODE with the highest order term first.
How can we write general code/module? I think it would be also useful for other Mathematica users.
For example, let's have a sixth-order ODE as follows:
(\[Omega] - Subscript[a, 6])*U[\[Eta]]- Subscript[a, 2]*U[\[Eta]] + 5*k^2*Subscript[a, 6]*Derivative[2][U][\[Eta]] + Subscript[b, 1]*U[\[Eta]]^3 + Subscript[b, 2]*U[\[Eta]]^3 + Subscript[b, 2]*U[\[Eta]]^5 + Subscript[b, 3]*U[\[Eta]]^7 + Subscript[a, 2]*Derivative[2][U][\[Eta]] + 6*k^2*Subscript[a, 4]*Derivative[2][U][\[Eta]] +
30*k^3*Subscript[a, 5]*Derivative[2][U][\[Eta]] + Subscript[a, 4]*Derivative[4][U][\[Eta]] + 2*k^2*Subscript[a, 6]*Derivative[4][U][\[Eta]] + Subscript[a, 6]*Derivative[6][U][\[Eta]] + Subscript[a, 4]*Derivative[6][U][\[Eta]]==0
Collect[eq, {U[_], Derivative[_][U][_]}]
whereeq
is your equation. Sorting the terms is a bit tricky, becausePlus
is inherentlyOrderless
. Do you want to have the sorted output just for the displaying purposes? $\endgroup$