2
$\begingroup$

This question came up in the Maple forum: Given a differential equation, how do we move all terms with the dependent variable and all its derivative to the left side of the equation and all terms with the independent variable to the right side?

Given a differential equation, typically the dependent variable is $y(x)$ and the independent variable is $x$.

Maple is not as strong as Mathematica in Pattern matching. So solutions are shown there using Maple's type system which is strong.

How would this be done in Mathematica? Below is my attempt.

Here is the function I wrote.

convert[odeIn_Equal, y_[x_]] := 
 Module[{ode = odeIn, n, yd, yy, deps, indeps},

  ode = ode[[1]] - ode[[2]];

  If[Or[FreeQ[ode, y], Not[Head[ode] === Plus]], Return[odeIn, Module]];

  ode  = ode /. {Derivative[n_][y][x] :> yd^n, y[x] :> yy};
  deps = Select[ode, Or[Not[FreeQ[#, yd]], Not[FreeQ[#, yy]]] &];
  deps = deps /. {yd^n_. :> Derivative[n][y][x], yy^n_. :> y[x]^n};
  indeps = Select[ode, (FreeQ[#, yd] && FreeQ[#, yy]) &];
  deps   == -indeps]

Here are tests cases and the output

testCases = {
   y[x] + y'[x] + Cos[x] + g[y[x]] + f'[x] + 1/x == Sin[x],
   f'[x] + 1/x == Sin[x],
   y[x] + x == 0,
   x*y'[x] + x == y[x] + y'''[x],
   x^2 + 1/y[x] + y'[x] + Sin[x] == y[x]^2,
   1 + x == 0,
   y'[x] == 0,
   y''[x] == y[x] - x};

Call it as

Map[{#, convert[#, y[x]]} &, testCases]
Grid[%, Frame -> All]

enter image description here

This is only for ordinary differential equation. Not partial differential equation. Separate function will have to be written for that.

Is there a better or more canonical way to do this? Does Mathematica have builtin function to do this?

V 14

$\endgroup$
0

1 Answer 1

1
$\begingroup$

This method just searches for terms that lack an instance of the dependent variable name and moves them to one side. It should work for PDEs as well.

In condensed form:

convert[odeIn_Equal, depVar_] :=
  (# - Select[#, FreeQ[#, depVar] &] == -Select[#, FreeQ[#, depVar] &]) &@First@Expand@SubtractSides@odeIn

In more readable form:

convert[odeIn_Equal, depVar_] := Module[
    {rhs, lhs = First@Expand@SubtractSides@odeIn},
    rhs = -Select[lhs, FreeQ[#, depVar] &];
    rhs + lhs == rhs
  ]

Result:

enter image description here

Only possible issue is for the simplest case of y'[x] == 0: it moves the dependent term to the right. This can be fixed with a simple kluge to detect cases in which there are no terms without the dependent variable.

This method also works for PDEs automatically:

convert[3 D[u[x, y], x] + Sin[x] + Cos[u[x, y]] - y^2 + x + 5 D[u[x, y], y] == 0, u]
(* Cos[u[x, y]]+5 (u^(0, 1))[x, y]+3 (u^(1,0))[x, y] == -x + y^2 - Sin[x] *)
$\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.