如何在python中用pyqtgraph找函数e^(-cos9x/x^2)在区间[1,5]的驻点
时间: 2024-04-29 19:24:48 浏览: 111
可以使用numpy和scipy库中的optimize函数来实现,代码如下:
```python
import numpy as np
from scipy import optimize
import pyqtgraph as pg
x = np.linspace(1, 5, 1000)
y = np.exp(-np.cos(9*x) / x**2)
# 找到函数的最小值
min_x = optimize.fmin(lambda x: np.exp(-np.cos(9*x) / x**2), 3)
min_y = np.exp(-np.cos(9*min_x) / min_x**2)
# 绘制函数及其驻点
plot = pg.plot()
curve = plot.plot(x, y)
min_curve = plot.plot([min_x], [min_y], pen=None, symbol='o')
```
运行这段代码,就能够绘制出函数及其驻点。
相关问题
如何在python中用pyqtgraph找函数e^(-cosqx/x^2)的驻点
您可以使用sympy库中的solve函数进行求解。以下是代码示例:
```python
import numpy as np
import pyqtgraph as pg
from sympy import *
# 定义函数
x, q = symbols('x q')
f = exp(-cos(q*x) / x**2)
# 求导数
df = diff(f, x)
# 求解方程 df = 0
res = solve(df, x)
# 转化为numpy数组
res = np.array(res)
# 绘制函数和驻点
x_vals = np.linspace(-10, 10, 500)
y_vals = lambdify(x, f)(x_vals)
pg.plot(x_vals, y_vals, pen='b')
pg.plot(res, [lambdify(x, f)(res)], pen=None, symbol='o', symbolBrush='r', symbolSize=10)
```
这样就能找到函数e^(-cosqx/x^2)的驻点了。
阅读全文