0

I am looking for a programming language and a way to automatize the following problems. Given a formula connecting different variables, say g=GM/r^2, and values for all but one of the variables, (g=9.8,M=5E25,G=6.7E-11), how can I program a routine which: a) Identifies the unknown variable b) symbolically, solves the formula c) finally, substitutes values of known variables and solves the equation for the unknown. I am far from an expert in programming and the only thing it came to my mind was a slow process in which, one checks variable after variable which one has not been set to a value and according to that use the appropriate rearrangement of the formula to calculate the unknown. (eg. in our case, the program checks variable after variable until it find that r is the unknown. Then, it uses the same formula but ready to calculate r, i.e. r=sqrt(GM/g)) I am sure there is a fast an elegant language to do this but I cannot figure it out. Thanks in advance for your help.

2 Answers2

0

For such a simple case, the approach that you suggest is quite appropriate.

The "slow" process might take on the order of 10 nanoseconds to find the unknown variable (using a compiled language), so I wouldn't worry so much.

Indeed symbolic computation programs are able to derive the explicit formulas, that you can retranscript in most programming languages

g=GM/r²
G=gr²/M
M=gr²/G
r=√GM/g


// C code
if (g == 0) g= G * M / (r * r);
else if (G == 0) G= g * r * r / M;
else if (M == 0) M= g * r * r / G;
else r= Math.sqrt(G * M / g);

For instance, the free Microsoft Mathematics can do it. But in this particular case, just do it by hand.

For a completely integrated solution with built-in scripting, think of Mathematica, Mathcad, Maple and the like.

0

Well, here is one way to do it, using Maxima.

eq : g = G * M / r^2;
known_values : [g = 9.8, M = 5e25, G = 6.7e-11];
eq1 : subst (known_values, eq);
remaining_var : listofvars (eq1);
solve (eq1, remaining_var);
  => [r = -5000000*sqrt(670)/7, r = 5000000*sqrt(670)/7]

You can use the function float to get a floating point value from that.

You can probably also do it with Sympy or something else.

Robert Dodier
  • 16,905
  • 2
  • 31
  • 48