financial_physician
financial_physician

Reputation: 1978

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.

Upvotes: 0

Views: 575

Answers (2)

Oscar Benjamin
Oscar Benjamin

Reputation: 14470

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)  ⎦

Upvotes: 1

Tarik
Tarik

Reputation: 11209

Well, you need to be aware of sympy limitations. It might not be able to tackle this type of equation. As far as being two sided, make it one sided by solving rhs - lhs = 0.

Plugging your equation into WolframAlpha did not solve it. As such, greatly doubt sympy will give you anything useful.

https://www.wolframalpha.com/input/?i=1%2Fg*%28w-a%29%5Eg+%3D+1%2F%282*g%29*%28w-x%29%5Eg+%2B+1%2F%282*g%29*%28w%2Bx%29%5Eg

Upvotes: 1

Related Questions