scipy 如何在Python中求解一个未知数的多项式方程?

62lalag4  于 2023-10-20  发布在  Python
关注(0)|答案(1)|浏览(161)

我一直在尝试使用scipy在python中解决R1的以下等式,但我得到了错误:

result = root_scalar(equation, bracket=[0, 8])
Traceback (most recent call last):

  Cell In[1638], line 1
    result = root_scalar(equation, bracket=[0, 8])

  File ~\anaconda3\lib\site-packages\scipy\optimize\_root_scalar.py:275 in root_scalar
    r, sol = methodc(f, a, b, args=args, **kwargs)

  File ~\anaconda3\lib\site-packages\scipy\optimize\_zeros_py.py:784 in brentq
    r = _zeros._brentq(f, a, b, xtol, rtol, maxiter, args, full_output, disp)

ValueError: f(a) and f(b) must have different signs

方程为:

K= 10**(a0 + (a1 * np.log10(R1/R2)) + (a2 * (np.log10(R1/R2))**2) + (a3 * (np.log10(R1/R2))**3) + (a4 * (np.log10(R1/R2))**4)) + 0.0166

我使用的代码如下:

R2 = 0.002294000005349517
K = 0.09539999812841415

a0 = -1.1358
a1 = -2.1146
a2 = 1.6474
a3 = -1.1428
a4 = -0.6190

def equation(log_R1_R2):
    x = log_R1_R2
    return 10**(a0 + a1*x + a2*x**2 + a3*x**3 + a4*x**4) - (K - 0.0166)

result = root_scalar(equation, bracket=[0, 8])
log_R1_R2 = result.root
R1 = R2 * 10**log_R1_R2
eqoofvh9

eqoofvh91#

这个问题似乎更适合多项式根求解器。使用numpy.polynomial.Polynomial类,我们可以创建所讨论的多项式,获得根,并使用这些根找到R1。至于你想用哪个根,那是你自己决定的,因为我不知道它的应用。

import numpy as np
from numpy.polynomial import Polynomial

R2 = 0.002294000005349517
K = 0.09539999812841415

a0 = -1.1358
a1 = -2.1146
a2 = 1.6474
a3 = -1.1428
a4 = -0.6190

p = Polynomial([-np.log10(K - 1/60) + a0, a1, a2, a3, a4])
roots = p.roots()
print(roots)
R1 = R2*10**roots

输出(即根):

[-3.07249423+0.j, -0.0149377 +0.j, 0.62061419-0.86009349j, 0.62061419+0.86009349j]

相关问题