I'm trying to solve these three equations:
a1 + a2 = b1
a2 + a3 = b2
b1 + b2 = c1
I generate values for three variables that are chosen randomly (disallowing the combination b1, b2, and c1), so I might have a1 = 5, a3 = 10, and c1 = 100, so I can solve that equation with sympy.
My problem is that I can't seem to transfer the random input to the sympy part of the code and loop over it.
a1, a2, a3, b1, b2, c1 = symbols('a1 a2 a3 b1 b2 c1')
solve([a1.subs(a1, 5) + a2 - b1, a2 + a3.subs (a3, 10) - b2, b1 + b2 - c1.subs (c1, 100)], (a1, a2, a3, b1, b2, c1))
This works when I assume a1, a3 and c1 as I mentioned in my example, but I choose those variables randomly beforehand. I've tried to create for-loops depending on which variables were chosen, but there are too many possible combinations of three variables, so I gave up.
If I've correctly understood what you're asking, you can use Sympy's
linsolve
function to solve the equations symbolically first, then substitute in numbers afterwards. The key step is to telllinsolve
which variables you want to solve for. I suggest usingset
s to separate the variables you're solving for from the ones you want to plug in values for. You might declare your variables like this:Then you can define your equations
and pass them and the chosen variables to
linsolve
Then you can plug in the chosen values.
Of course
value(variable)
is a proxy for whatever you do to determine the numeric values you want to plug in.