SymPy when trying to solve equations, you get an infinite loop
Question:
I’m trying to solve the equation
15*exp(ln(0.5)/4 * x) = 1
and I get into an infinity loop
x = symbols("x")
fun = 15 * exp(log(0.5)/4 * x)
eq = Eq(fun, 1)
ans = solve(eq, x) #infinity loop
SymPy version 1.11.1
python version 3.10
Answers:
When working with powers raised to a floating exponent, either use nsolve or convert the floating number — especially a simple one like 0.5 — to a Rational:
>>> from sympy import *
>>> from sympy.abc import x
>>> solve(15 * exp(log(S.Half)/4 * x)-1)
[4*log(15)/log(2)]
15.6275623824341
>>> nsolve(15 * exp(log(.5)/4 * x)-1, 1)
15.6275623824341
This is because of the behaviour of solve
under the default rational=True
setting. Pass rational=False:
In [2]: eq
Out[2]:
-0.173286795139986⋅x
15⋅ℯ = 1
In [3]: solve(eq, x, rational=False)
Out[3]: [15.6275623824341]
I’m trying to solve the equation
15*exp(ln(0.5)/4 * x) = 1
and I get into an infinity loop
x = symbols("x")
fun = 15 * exp(log(0.5)/4 * x)
eq = Eq(fun, 1)
ans = solve(eq, x) #infinity loop
SymPy version 1.11.1
python version 3.10
When working with powers raised to a floating exponent, either use nsolve or convert the floating number — especially a simple one like 0.5 — to a Rational:
>>> from sympy import *
>>> from sympy.abc import x
>>> solve(15 * exp(log(S.Half)/4 * x)-1)
[4*log(15)/log(2)]
15.6275623824341
>>> nsolve(15 * exp(log(.5)/4 * x)-1, 1)
15.6275623824341
This is because of the behaviour of solve
under the default rational=True
setting. Pass rational=False:
In [2]: eq
Out[2]:
-0.173286795139986⋅x
15⋅ℯ = 1
In [3]: solve(eq, x, rational=False)
Out[3]: [15.6275623824341]