# Solve for missing variables (expressions/functions)

I’m hoping to get a script working that can solve for whichever variable is missing in an equation:
x + y = z
Given:
x = 5
z = 2
y = ?

Where if I unplug the given value for one variable and provide it into a new slot (x = ?, z = 2, y = 7), I can automatically output the missing x variable value.

I found this https://stackoverflow.com/questions/10274236/python-solving-equations-for-unknown-variable?lq=1 but believe that would require loading up SymPy, which is a process I’m not familiar with. I could dig into that, but am I missing something native to in Grasshopper that can do this?

I would say no. For a general equation, you may use the Galapagos as a heuristic solver, but the result is not guaranteed nor accurate.

well, test for which variable is missing, then feed it to the algebraic form of the equipment; i.e., if y is missing, then y=z-x. if z is missing, x=z-y ; etc

1 Like

You can do something like that

2 Likes

Thanks @seghierkhaled, this is more or less what I was looking for. Guess I’ll have to do the algebra on my end, but only once per variable as opposed to every time.

Solve transcendental equations `sin(x)+y = cos(x)`, `tan(x+y) = 1` over `[1, 2π]`, using simulated annealing:

20200106 Solver.gh (21.3 KB)

The accuracy is not very bad with a deviation of about 0.07%.

If you want solve complex equation use special softwares or websites
and you can with grasshopper but some equations need a long time with solvers