Search code examples
pythonequation

Solve equations with multiple variables (Python)


I have an equation, say x = .5 (b + c) - d. I would like to solve this equation for all of the variables. For example, d = … or b =. This would mean that the variables are equal to other variables.

Another example is this simple equation: x = b/c. C is equal to b/x and b = c * x (please correct my math if it’s wrong). Is this possible?

Thanks


Solution

  • E.g. :

    from sympy import symbols, solve, Eq 
    
    x = symbols('x')
    b = symbols('b')
    c = symbols('c')
    d = symbols('d')
    
    eq1 = Eq(x, 0.5*(b+c)-d)
    
    sol_d = solve(eq1, d)
    sol_b = solve(eq1, b)
    
    print(sol_d, sol_b)
    

    Output: [0.5*b + 0.5*c - x] [-c + 2.0*d + 2.0*x]

    Here is a video and explanation of the solve() command