当我试图解一个非线性方程时出错了

2024-03-28 12:01:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图解一个方程,由下式给出:

enter image description here

我将上述等式转换为python代码,如下所示:

from scipy.optimize import fsolve
import numpy as np

u = lambda b : ((1 - b)(7.864 - 5.336*b + 25.864*np.power(b,2) - 11.935*np.power(b,3) - 0.336*np.power(b,4))) - 6.164

fsolve(u,np.linspace(0,1,10))

但我得到一个错误,说:

enter image description here

这可能是什么原因?我做错了什么


Tags: lambda代码fromimportnumpyas错误np
1条回答
网友
1楼 · 发布于 2024-03-28 12:01:24

在(1-b)和(7.864…)之间缺少a*:

In [11]: from scipy.optimize import fsolve
    ...: import numpy as np
    ...:
    ...: u = lambda b : ((1 - b) * (7.864 - 5.336*b + 25.864*np.power(b,2) - 11.935*np.power(b,3) - 0.336*np.power(b,4))) - 6.164
    ...:                       # ^ MISSING HERE
    ...: fsolve(u,np.linspace(0,1,10))
    ...:
Out[11]:
array([0.20503009, 0.20503009, 0.20503009, 0.20503009, 0.20503009,
       0.20503009, 0.20503009, 0.20503009, 0.20503009, 0.20503009])

因此出现错误TypeError: 'numpy.ndarray' object is not callable,这与尝试执行以下操作相同:

In [12]: a
Out[12]: array(42)

In [13]: a()
TypeError: 'numpy.ndarray' object is not callable

In [14]: a(1)
TypeError: 'numpy.ndarray' object is not callable

相关问题 更多 >