I have problems by solving a polynomial function with sympy. The following example shows a case which gives an error message that I cannot manage. If the polynomial gets simpler the solver works properly. Please copy and paste the code to check the error on your system as well.
import sympy
from sympy import I
omega = sympy.symbols('omega')
def function(omega):
return - 0.34*omega**4 \
+ 7.44*omega**3 \
+ 4.51*I*omega**3 \
+ 87705.64*omega**2 \
- 53.08*I*omega**2 \
- 144140.03*omega \
- 22959.95*I*omega \
+ 42357.18 + 50317.77*I
sympy.solve(function(omega), omega)
Do you know how I can achieve a result? Thank you for your help.
EDIT:
This is the error message:
TypeError Traceback (most recent call last)
<ipython-input-7-512446a62fa9> in <module>()
1 def function(omega):
2 return - 0.34*omega**4 + 7.44*omega**3 + 4.51*I*omega**3 + 87705.64*omega**2 - 53.08*I*omega**2 - 144140.03*omega - 22959.95*I*omega + 42357.18 + 50317.77*I
----> 3 sympy.solve(function(omega), omega)
C:\Anaconda\lib\site-packages\sympy\solvers\solvers.pyc in solve(f, *symbols, **flags)
1123 # restore floats
1124 if floats and solution and flags.get('rational', None) is None:
-> 1125 solution = nfloat(solution, exponent=False)
1126
1127 if check and solution: # assumption checking
C:\Anaconda\lib\site-packages\sympy\core\function.pyc in nfloat(expr, n, exponent)
2463 return type(expr)([(k, nfloat(v, n, exponent)) for k, v in
2464 list(expr.items())])
-> 2465 return type(expr)([nfloat(a, n, exponent) for a in expr])
2466 rv = sympify(expr)
2467
C:\Anaconda\lib\site-packages\sympy\core\function.pyc in nfloat(expr, n, exponent)
2497 return rv.xreplace(Transform(
2498 lambda x: x.func(*nfloat(x.args, n, exponent)),
-> 2499 lambda x: isinstance(x, Function)))
2500
2501
C:\Anaconda\lib\site-packages\sympy\core\basic.pyc in xreplace(self, rule)
1085
1086 """
-> 1087 value, _ = self._xreplace(rule)
1088 return value
1089
C:\Anaconda\lib\site-packages\sympy\core\basic.pyc in _xreplace(self, rule)
1093 """
1094 if self in rule:
-> 1095 return rule[self], True
1096 elif rule:
1097 args = []
C:\Anaconda\lib\site-packages\sympy\core\rules.pyc in __getitem__(self, key)
57 def __getitem__(self, key):
58 if self._filter(key):
---> 59 return self._transform(key)
60 else:
61 raise KeyError(key)
C:\Anaconda\lib\site-packages\sympy\core\function.pyc in <lambda>(x)
2496
2497 return rv.xreplace(Transform(
-> 2498 lambda x: x.func(*nfloat(x.args, n, exponent)),
2499 lambda x: isinstance(x, Function)))
2500
C:\Anaconda\lib\site-packages\sympy\core\function.pyc in nfloat(expr, n, exponent)
2463 return type(expr)([(k, nfloat(v, n, exponent)) for k, v in
2464 list(expr.items())])
-> 2465 return type(expr)([nfloat(a, n, exponent) for a in expr])
2466 rv = sympify(expr)
2467
C:\Anaconda\lib\site-packages\sympy\core\function.pyc in nfloat(expr, n, exponent)
2463 return type(expr)([(k, nfloat(v, n, exponent)) for k, v in
2464 list(expr.items())])
-> 2465 return type(expr)([nfloat(a, n, exponent) for a in expr])
2466 rv = sympify(expr)
2467
TypeError: __new__() takes exactly 3 arguments (2 given)
mpmath.findroot
is ~= 1.35717989161653180236360985534 - 0.202974596285109153971324419197i – Grondinmpmath.findroot
? – Nunhoodsolver=Secant
, with 1 as the starting approximation. But I converted all of the float constants to strings before passing them tomp.mpf()
. I can post my code as an answer if you like. – Grondin