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

Click For Summary

Discussion Overview

The discussion revolves around solving a specific equation using Python's SymPy library. Participants are addressing issues related to floating point errors, the use of mathematical functions from different libraries, and the challenges of obtaining real solutions from the equation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant encounters a TypeError when attempting to solve an equation using both SymPy and the math library.
  • Another participant suggests that the error may arise from using the math library instead of SymPy's functions, recommending the use of SymPy's sqrt and exp.
  • A participant notes that removing the math functions resolves the error but leads to difficulties in solving the equation.
  • It is mentioned that the equation cannot be solved when certain functions are included, and a NotImplementedError is raised due to multiple generators in the equation.
  • One participant inquires about forcing the variable x to be real, suggesting the use of the real=True argument when defining x.
  • Another participant shares a ConditionSet output indicating that the solution may involve complex numbers, questioning the utility of the condition set.
  • A final participant claims to have solved the problem using a binary search method, although details of this solution are not provided.

Areas of Agreement / Disagreement

Participants express differing views on the appropriate use of libraries and functions for solving the equation. There is no consensus on a definitive solution to the original problem, as multiple approaches and unresolved issues remain.

Contextual Notes

Participants highlight limitations related to the compatibility of mathematical functions from different libraries and the complexity of the equation being solved. The discussion also touches on the potential for complex solutions and the implications of setting conditions on the variable.

Who May Find This Useful

Readers interested in computational methods for solving equations, particularly those using Python and SymPy, may find the discussion relevant. It may also be useful for those encountering similar floating point errors or seeking to understand the implications of using different mathematical libraries.

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   Reactions: 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   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 ?
 
BvU said:
Is there a way to force x real ?

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

you can also do things like positive = 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 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
5
Views
16K