简体   繁体   中英

Solving a simple symbolic equation in python

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!

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)

It is simple: your equation has no result.

The equation is 12/((2**l_x)-1))/1e8 = 0 and that has no solution.

See what y = 12/((2**x)-1))/1e8 looks like ( copied from wolframalpha ):

y = 12 /((2 ** x)-1))/ 1e8

To compare, try solving eg 12/((2**l_x)-1))/1e8 = 1 instead:

>>> solve(((x_max-x_min)/((2**l_x)-1))/precision_x - 1, l_x)
[(-log(25000000) + log(25000003))/log(2)]

Works like a charm!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM