我需要通过solveset(或一些替代工具)来评估函数的最大值。
目前我的代码是:
import numpy as np
from scipy.optimize import curve_fit as cf
from sympy import symbols, Interval, S, Float, Rational
from sympy.logic.boolalg import Or
from sympy import solveset
x = symbols('x', real=True)
"""
Fitting raw data via:
1. np.genfromtxt(...)
2. fitParams, fitCovariances = cf(fitFunc, x_values, y_values)
gives the foolowing parameters:
"""
p1 = -0.351519665611521
p2 = -0.025153022792473
p3 = 0.005404053033914
p4 = -0.000875763032580
p5 = 0.000070700828439
p6 = -0.000002224914318
p7 = 0.871513745522901
fitParams = np.array([p1, p2, p3, p4, p5, p6, p7])
def function(x, p1, p2, p3, p4, p5, p6, p7):
return p7*x**(S(2)/3) + p6*x**6 + p5*x**5 + p4*x**4 + p3*x**3 + p2*x**2 + p1*x
其中参数'p *'来自先前的原始数据拟合。
我需要提取最大值:
energy = function(x, *fitParams) + var1*x
作为变量'var1'的函数。
所以我试图找到它的衍生物的根源:
energy_dx = energy.diff(x)
通过“solveset”:
solutions = solveset(energy_dx, x, domain=S.Reals)
碰巧对于“var1”的某些值,solveset能够评估解决方案,但不能评估其他值。 在后面这些情况下,代码给出:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/sympy/polys/polytools.py", line 3544, in nroots
cleanup=cleanup, error=False, extraprec=f.degree()*10)
File "/usr/lib/python3/dist-packages/mpmath/calculus/polynomials.py", line 196, in polyroots
% maxsteps)
mpmath.libmp.libhyper.NoConvergence: Didn't converge in maxsteps=50 steps.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./myscript.py", line 146, in <module>
solutions = solveset(energy_dx, x, S.Reals)
File "/usr/lib/python3/dist-packages/sympy/solvers/solveset.py", line 921, in solveset
return _solveset(f, symbol, domain, _check=True)
File "/usr/lib/python3/dist-packages/sympy/solvers/solveset.py", line 734, in _solveset
solver)
File "/usr/lib/python3/dist-packages/sympy/solvers/solveset.py", line 535, in _solve_radical
f_y_sols = solveset_solver(eq, y)
File "/usr/lib/python3/dist-packages/sympy/solvers/solveset.py", line 674, in <lambda>
solver = lambda f, x, domain=domain: _solveset(f, x, domain)
File "/usr/lib/python3/dist-packages/sympy/solvers/solveset.py", line 738, in _solveset
result += _solve_as_rational(equation, symbol, domain)
File "/usr/lib/python3/dist-packages/sympy/solvers/solveset.py", line 385, in _solve_as_rational
return _solve_as_poly(g, symbol, domain)
File "/usr/lib/python3/dist-packages/sympy/solvers/solveset.py", line 430, in _solve_as_poly
quintics=True, domain='EX')
File "/usr/lib/python3/dist-packages/sympy/polys/polyroots.py", line 989, in roots
for r in f.nroots():
File "/usr/lib/python3/dist-packages/sympy/polys/polytools.py", line 3553, in nroots
n, maxsteps))
mpmath.libmp.libhyper.NoConvergence: convergence to root failed; try n < 15 or maxsteps > 50
在这里,我无法弄清楚如何修改'n'或'maxsteps'值。
我正在通过Python进行编码,所以我非常感谢您的任何建议。 Python是3.6.5版本,所有模块都是最新的。
6月02日:目前我无法重现问题