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

In summary: Symbol('x')A, B, C, D = 0.59912051, 0.64030348, 263.33721367, 387.92069617r = 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.54145
  • #1
Arman777
Insights Author
Gold Member
2,168
193
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
  • #2
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.
 
  • #3
I tried that however its still giving me the same error.
 
  • #4
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
  • #5
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
  • #6
I tried and it worked. However the python couldn't solve the equation..
 
  • #7
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 ?
 
  • #8
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
 
  • #9
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
 

1. What is a floating point error in Python equation solver?

A floating point error occurs when a number in a calculation cannot be represented accurately in binary, resulting in a small error in the final result. This is a common issue in all programming languages, including Python.

2. Why am I getting a floating point error in my Python equation solver?

There are several reasons why you may be getting a floating point error in your Python equation solver. One possible reason is that your calculation involves division by zero, which is undefined. Another reason could be that your calculation is too complex and involves a very small or very large number, which cannot be accurately represented in binary.

3. How can I fix a floating point error in my Python equation solver?

One way to fix a floating point error in your Python equation solver is to use a different data type, such as the decimal data type, which allows for more precise calculations. Another solution is to use the "round" function to round your result to a certain number of decimal places, which can help reduce the error.

4. Are there any best practices for avoiding floating point errors in Python equation solver?

Yes, there are a few best practices that can help you avoid floating point errors in your Python equation solver. One is to use appropriate data types for your calculations, as mentioned earlier. Another is to break down complex calculations into smaller, simpler steps to reduce the chances of error. You can also use libraries or modules that are specifically designed for accurate mathematical operations.

5. Can a floating point error affect the accuracy of my Python equation solver?

Yes, a floating point error can affect the accuracy of your Python equation solver. In some cases, the error may be small enough to not make a significant impact, but in other cases, it can lead to a significant difference in the result. It is important to be aware of floating point errors and understand how to handle them to ensure the accuracy of your calculations.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
8
Views
881
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
921
  • Programming and Computer Science
Replies
3
Views
895
  • Programming and Computer Science
Replies
5
Views
9K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
Back
Top