Search code examples
symbolic-mathequation-solving

Symbolic computation with formulas and solver


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.


Solution

  • 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.