Python Python equation solver, I'm getting a floating point error

AI Thread Summary
The discussion revolves around solving a complex equation in Python using the SymPy library, where users encounter a floating point error due to the use of the math library instead of SymPy's functions. It is noted that the `solve` function can return results that are not compatible with the print function, and using `math.sqrt` and `math.exp` leads to errors. The solution involves importing `sqrt` and `exp` from SymPy and using `.evalf()` to obtain floating point results. Users also explore setting conditions for the variable `x` to ensure it remains real, with some success in finding solutions through methods like binary search. The conversation highlights the importance of using SymPy's symbolic capabilities for accurate mathematical computations.
Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
I am trying to solve the equation like this,

Python:
from sympy.solvers import solve
from sympy import Symbol
import math

x = Symbol('x')

A, B, C, D = 0.59912051, 0.64030348, 263.33721367, 387.92069617

print(solve((A * x) + (B * math.sqrt(x**3)) - (C * math.exp(-x / 50)) - D, x , numerical = True))
Code:
Traceback (most recent call last):
  File "c:/Users/***/Desktop/Python Stuff/***", line 9, in <module>
    solve((A * x) + (B * math.sqrt(x**3)) - (C * math.exp(-x / 50)) - D, x , numerical = True)
  File "C:\Users\***\AppData\Local\Programs\Python\Python37\lib\site-packages\sympy\core\expr.py", line 280, in __float__
    raise TypeError("can't convert expression to float")
TypeError: can't convert expression to float

Why this is happening ?Thanks
 
Technology news on Phys.org
It's possible that what solve returns isn't something that print can handle. I would try this:
Python:
solve((A * x) + (B * math.sqrt(x**3)) - (C * math.exp(-x / 50)) - D, x)
Also, from my reading of the docs for solve, you don't need to include the numerical = True part, since that is the default.
 
I tried that however its still giving me the same error.
 
Error goes away if I remove 'math.sqrt' and 'math.exp'

I find one reference saying you shouldn't use 'math' in sympy, but it doesn't give a workaround.
 
  • Like
Likes Arman777
Sympy is there for symbolic computations. The math library is there for numerics (typically bad ones, basically anything you'd want in there is in numpy)

so import sqrt and exp from sympy, get your final symbolic expression, then use .evalf() at the end to get it as floating point.
 
  • Like
Likes BvU and Arman777
I tried and it worked. However the python couldn't solve the equation..
 
I still get an error
Python:
from sympy.solvers import solve
from sympy import Symbol
from sympy import sqrt
from sympy import exp

x = Symbol('x')

A, B, C, D = 0.59912051, 0.64030348, 263.33721367, 387.92069617

print(solve((A * x) + (B * sqrt(x**3)) - C*exp(-0.02*x) - D, x , numerical = True))

Code:
line 10:
    print(solve((A * x) + (B * sqrt(x**3)) - C*exp(-0.02*x) - D, x , numerical = True))
  File "H:\Program Files\Python36\lib\site-packages\sympy\solvers\solvers.py", line 1171, in solve
    solution = _solve(f[0], *symbols, **flags)
  File "H:\Program Files\Python36\lib\site-packages\sympy\solvers\solvers.py", line 1742, in _solve
    raise NotImplementedError('\n'.join([msg, not_impl_msg % f]))
NotImplementedError: multiple generators [x, exp(x/50), sqrt(x**3)]
No algorithms are implemented to solve equation 59912051*x/100000000 + 16007587*sqrt(x**3)/25000000 - 38792069617/100000000 - 26333721367*exp(-x/50)/100000000
If I leave out the exp, there is a result (66.6 and two complex numbers).

Is there a way to force x real ?
 
BvU said:
Is there a way to force x real ?

Python:
x = Symbol('x', real = True)

you can also do things like postive = True, if so inclined
 
I kind of find the condition set however I am not sure how that can help
Python:
import sympy as sym
x = sym.Symbol('x')

A, B, C, D = 0.59912051, 0.64030348, 263.33721367, 387.92069617
r = sym.solveset((A * x) + (B * sym.sqrt(x**3)) - (C * sym.exp(-x / 50)) - D, x)
print(r)

ConditionSet(x, Eq(0.40998854650011*x**3*exp(x/25) - 0.35894538550266*x**2*exp(x/25) + 315.541451511899*x*exp(x/50) + 464.822490657851*x*exp(x/25) - 204307.910508669*exp(x/50) - 150482.466517017*exp(x/25) - 69346.4881034792, 0), Complexes(Reals x Reals, False))

or in pprint form

Code:
⎧                                 x                         x
⎪                                 ──                        ──
⎨                              3  25                     2  25
⎪x | x ∊ ℂ ∧ 0.40998854650011⋅x ⋅ℯ   - 0.35894538550266⋅x ⋅ℯ   + 315.541451511
⎩

       x                        x                      x
       ──                       ──                     ──
       50                       25                     50
899⋅x⋅ℯ   + 464.822490657851⋅x⋅ℯ   - 204307.910508669⋅ℯ   - 150482.466517017⋅ℯx                        ⎫
──                       ⎪
25                       ⎬
   - 69346.4881034792 = 0⎪
                         ⎭
 
  • #10
The problem can be solved by using binary search. I solved it
 

Similar threads

Replies
4
Views
5K
Replies
2
Views
2K
Replies
3
Views
3K
Replies
4
Views
7K
Replies
11
Views
2K
Replies
17
Views
3K
Replies
2
Views
2K
Back
Top