0
$\begingroup$

I have some equation

eqn = (A + B).x''[t] + Transpose[x'[t]].(2 A - 3 B + 1).x'[t] +(СС - 5).y'[t]+ Sin[x[t]+y[t]]

I need to collect all the coefficients at x''[t],x'[t],y'[t] and free term Sin[x[t]+y[t]]

That is, to get something like:

X = A + B
Y = Transpose[x'[t]].(2 A - 3 B + 1)
Z = CC - 5
F = Sin[x[t]+y[t]]

How to do it with in Mathematica automatically?

$\endgroup$
3
  • 1
    $\begingroup$ Perhaps this can be of use for the non-constant terms: Cases[eqn, Dot[a_, Derivative[i_][z_][t_]] :> a]. $\endgroup$
    – Domen
    Sep 29 '21 at 19:02
  • 2
    $\begingroup$ Or CoefficientList[eqn /. Dot[a_, Derivative[n_][z_][t]] :> a z^n, {x, y}] to get all of them. However, you have given just one example so I am not sure whether this is general enough for all your cases. $\endgroup$
    – Domen
    Sep 29 '21 at 19:14
  • $\begingroup$ @Domen can you form your comment, as answer ? $\endgroup$
    – dtn
    Sep 29 '21 at 19:18
2
$\begingroup$

You can use Replace to convert derivatives to powers, wrapped in some arbitrary head, and then use any of the existing functions to get the coefficients (e.g. CoefficientList or CoefficientRules).

derivativeCoefficients[eqn_] := 
 CoefficientRules[
   eqn /. (a_) . Derivative[n_][z_][t] | a_*Derivative[n_][z_][t] :> 
     a d[z]^n, {d[x], d[y]}] /. d[z_]^n_ :> Derivative[n][z][t]

eqn = (A + B) . x''[t] + 
  Transpose[x'[t]] . (2 A - 3 B + 1) . x'[t] + (CC - 5) . y'[t] + 
  Sin[x[t] + y[t]]

derivativeCoefficients[eqn]

(* {{2, 0} -> A + B, 
    {1, 0} -> Transpose[x'[t]] . (1 + 2 A - 3 B), 
    {0, 1} -> -5 + CC, 
    {0, 0} -> Sin[x[t] + y[t]]} *)

{X, Y, Z, F} = {{2,0}, {1,0}, {0,1}, {0,0}} /. %

This should now also work for simple multiplicative coefficients.

eqn = a x''[t] + 5 x'[t] - 2 y'[t] + x[t] y[t]
derivativeCoefficients[eqn]

(* {{2, 0} -> a, 
    {1, 0} -> 5, 
    {0, 1} -> -2, 
    {0, 0} -> x[t] y[t]} *)
$\endgroup$
3
  • $\begingroup$ Thanks for the answer. And what is this replacement for numbers? Is it possible to get the coefficients in the form in which they stand for the corresponding derivatives? $\endgroup$
    – dtn
    Sep 29 '21 at 19:50
  • 1
    $\begingroup$ @dtn, I have edited my answer to include this. $\endgroup$
    – Domen
    Sep 29 '21 at 22:29
  • $\begingroup$ Yes, it works. I will try your approach on other equations. $\endgroup$
    – dtn
    Sep 30 '21 at 4:08

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.