5
$\begingroup$

I often find myself wanting to see what variables/constants are undefined in a large set of differential equations, is there a function that will do this for me?

As an example, for the following ODE (as a list, as I usually have more than one),

eqn={Cos[x'[s]] + x[s] == a + b Sin[Log[x[s]]]}

I'd like to return {x[s],x'[s],a,b}, as they are the unknowns. If the constants have defined values then they'll drop out.

I can use the following hack with Variables to do part of it:

Variables[eqn[[All, 1]] - eqn[[All, 2]]]
{a, b, Cos[x'[s]], Sin[Log[x[s]]], x[s]}

but anything wrapped inside a Mathematica function is not extracted, plus I have to extract the two parts of equation list separately. If there are a lot of Trig functions this gets very messy.

Edit: Alternatively, a function that gives just the unknown variables without derivatives would also be helpful, so returning {a,b,s,x} from the above example (FullForm of the Variables hack above may help towards this). Both would be useful in different ways.

$\endgroup$
  • $\begingroup$ In general, it seems that what you want to do is impossible: consider the scenario that f is defined for every real input except 1, and f[1] is a variable of some kind. Then f[1] is an unknown but most f[x_] are not. $\endgroup$ – Patrick Stevens Aug 11 '15 at 13:56
  • $\begingroup$ Maybe just use Cases: Cases[eqn, x[s] | x'[s] | a | b, [Infinity]] // Union $\endgroup$ – Arnoud Buzing Aug 11 '15 at 14:29
  • 1
    $\begingroup$ @ArnoudBuzing Your input contains expected output :) $\endgroup$ – Kuba Aug 11 '15 at 14:31
  • $\begingroup$ @Kuba it's not clear to me from the question how unknown the unknowns are. In full generality this seems to be a very complicated question to answer, unless you can make certain assumptions on what you are looking for... $\endgroup$ – Arnoud Buzing Aug 11 '15 at 16:24
  • $\begingroup$ I often have a lot of variables/constants which are defined in terms of other variables/constants, so I end up wanting to know if my resulting system is well defined (and what the order of the system is). $\endgroup$ – KraZug Aug 12 '15 at 8:07
4
$\begingroup$

Not precisely what you asked for, but I usually do something like this and interpret the results:

a =.;
DeleteDuplicates@Cases[
  eqn,
  s_Symbol /; Context[s] === "Global`",
  Infinity, Heads -> True]
(*  {x, s, a, b}  *)

a = 2;
DeleteDuplicates@Cases[
  eqn,
  s_Symbol /; Context[s] === "Global`",
  Infinity, Heads -> True]
(*  {x, s, b}  *)
$\endgroup$
  • $\begingroup$ Thanks, that answers the second part of my question nicely. $\endgroup$ – KraZug Aug 12 '15 at 8:03
  • $\begingroup$ Right, so I'm currently at a working answer to the first part, but I get (ignorable) errors from Context::ssle as it tries to apply Context inappropriately: Sort@DeleteDuplicates[ Select[(eqn /. s_ /; (Context[s] === "System`" && ! (s === List) && ! (s === Derivative)) -> Sequence), Not[NumericQ[#]] &]] $\endgroup$ – KraZug Aug 12 '15 at 8:42
  • $\begingroup$ @KraZug Thanks for the accept. Your code for part 1 didn't work on eqn = x'[t]^2 == (a + x[t])^2. Try this instead: Union@ Cases[eqn, s_Symbol | (s_Symbol)[_] | Derivative[_][s_][_] /; Context[s] === "Global`", Infinity]. In my mind, I'm not sure what the advantage is of getting rid of the independent variable, because it should be known and if necessary, you can get rid of it by applying DeleteCases[..., s] to the result (sub. t for s for the example in this comment). $\endgroup$ – Michael E2 Aug 14 '15 at 10:47
  • $\begingroup$ I'm happy to keep the independent variable, that's fine, my code wasn't returning it unless it was being used in a non-autonomous manner. My code wasn't working on your eqn there because it required a list round the outside, it worked on {eqn}. But your code you just posted seems good (and works for lists of equations or single ones, and doesn't give Context errors), can you edit that into the answer? Thanks for your help! $\endgroup$ – KraZug Aug 17 '15 at 8:31
  • $\begingroup$ I'll note here that in order to extend this to PDEs, I'm now using: Union@ Cases[eqn, s_Symbol | (s_Symbol)[] | Derivative[][s_][___] /; Context[s] === "Global`", Infinity] . $\endgroup$ – KraZug Sep 11 '15 at 10:09

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.