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

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
9 replies · 10K views
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
 
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.
 
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   Reactions: 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 ?
 
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⎪
                         ⎭