Skip to content
Advertisement

How to use sympy to algebraically solve a two sided equation

I’d like to use sympy to solve he following equation in terms of x, g, and w.

enter image description here

Here’s what I thought I should code

from sympy import *

w, a, x, g = symbols('w a x, g', real=True)
lhs = 1/g*(w-a)**g
rhs = 1/(2*g)*(w-x)**g + 1/(2*g)*(w+x)**g
solve((lhs,rhs), (x,g,w))

But this seems to be trying to solve for a numerical answer.

Advertisement

Answer

You can create a 2-sided equation with Eq:

In [52]: Eq(lhs, rhs)                                                                                                                          
Out[52]: 
        g          g          g
(-a + w)    (w - x)    (w + x) 
───────── = ──────── + ────────
    g         2⋅g        2⋅g 

When you say that you want to solve “in terms of x, g and w” I’m not sure I understand what you mean. Do you mean that you want to solve for a in terms of the others? If so then you just have to ask to solve for a:

In [53]: solve(Eq(lhs, rhs), a)                                                                                                                
Out[53]: 
⎡     -1                          ⎤
⎢     ───    _____________________⎥
⎢      g  g ╱        g          g ⎥
⎣w - 2   ⋅╲╱  (w - x)  + (w + x)  ⎦
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement