Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving a simple symbolic equation in python

Tags:

python

sympy

I am using sympy to solve a very simple equation symbolically, but the solution I get for the variable is an empty matrix! Here is the code:

from sympy import *

x = Symbol('x')
l_x = Symbol('l_x')

x_min = -6
x_max = 6

precision_x = 10**8 

solve(((x_max-x_min)/((2**l_x)-1))/precision_x, l_x)

print(l_x)

I tried some other simple equations such as:

solve(x**2 = 4, x)

And the later works perfectly; I just do not understand why the former one does not work!

like image 409
RezAm Avatar asked Dec 17 '22 23:12

RezAm


1 Answers

The expression given to solve has an assumed rhs of 0 which no value of l_x can satisfy. Try something like this instead:

from sympy import *
q, r, s, t = symbols("q r s t")
eq = (q-r)/(2**s-1)/t
solve(eq-1,s)

The output is:

[log((q - r + t)/t)/log(2)]

to explicitly create an equation object with a non-zero rhs you can do something like:

solve(Eq(eq,1),s)
like image 145
ptb Avatar answered Jan 07 '23 16:01

ptb