# List of unknowns in a differential equation

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.

• 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. – Patrick Stevens Aug 11 '15 at 13:56
• Maybe just use Cases: Cases[eqn, x[s] | x'[s] | a | b, [Infinity]] // Union – Arnoud Buzing Aug 11 '15 at 14:29
• @ArnoudBuzing Your input contains expected output :) – Kuba Aug 11 '15 at 14:31
• @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... – Arnoud Buzing Aug 11 '15 at 16:24
• 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). – KraZug Aug 12 '15 at 8:07

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",
(*  {x, s, a, b}  *)

a = 2;
DeleteDuplicates@Cases[
eqn,
s_Symbol /; Context[s] === "Global",

• 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[#]] &]] – KraZug Aug 12 '15 at 8:42
• @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). – Michael E2 Aug 14 '15 at 10:47