3
$\begingroup$

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

So, I want to get the following: enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ You can collect terms with Collect[eq, {U[_], Derivative[_][U][_]}] where eq is your equation. Sorting the terms is a bit tricky, because Plus is inherently Orderless. Do you want to have the sorted output just for the displaying purposes? $\endgroup$
    – Domen
    Commented Mar 24, 2022 at 15:45
  • $\begingroup$ Thank you for your interest. I see. If what I said is not possible, yes we can sort it for displaying purposes as the last possibility. $\endgroup$ Commented Mar 24, 2022 at 16:06

1 Answer 1

6
$\begingroup$
Clear["Global`*"]

eqn = (ω - Subscript[a, 6])*U[η] - Subscript[a, 2]*U[η] + 
    5*k^2*Subscript[a, 6]*Derivative[2][U][η] + 
    Subscript[b, 1]*U[η]^3 + Subscript[b, 2]*U[η]^3 + 
    Subscript[b, 2]*U[η]^5 + Subscript[b, 3]*U[η]^7 + 
    Subscript[a, 2]*Derivative[2][U][η] + 
    6*k^2*Subscript[a, 4]*Derivative[2][U][η] + 
    30*k^3*Subscript[a, 5]*Derivative[2][U][η] + 
    Subscript[a, 4]*Derivative[4][U][η] + 
    2*k^2*Subscript[a, 6]*Derivative[4][U][η] + 
    Subscript[a, 6]*Derivative[6][U][η] + 
    Subscript[a, 4]*Derivative[6][U][η] == 0;

format = Inactive[Plus] @@ 
    Reverse[List @@ (Collect[#, {U[η], Derivative[_][U][η]}])] &;

Displayed in the requested order

format@eqn[[1]] == 0

enter image description here

In canonical order

% // Activate

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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