如何在python中用pyqtgraph找函数e^(-cos9x/x^2)的驻点
时间: 2024-05-05 13:18:38 浏览: 143
你可以使用以下代码在python中使用pyqtgraph找到函数e^(-cos9x/x^2)的驻点:
```python
import numpy as np
import pyqtgraph as pg
x = np.arange(-10, 10, 0.1)
y = np.exp(-np.cos(9*x) / x**2)
plt = pg.plot()
plt.plot(x, y)
# Find local maxima/minima
min_ind = pg.findMin(y)
max_ind = pg.findMax(y)
mins = [x[i] for i in min_ind]
maxs = [x[i] for i in max_ind]
print("Local minima:", mins)
print("Local maxima:", maxs)
```
这段代码使用numpy生成自变量x和因变量y的值,然后使用pyqtgraph绘制出函数图像。接着使用pg.findMin和pg.findMax来找到函数的驻点,将所有极小值的x坐标存储在mins数组中,将所有极大值的x坐标存储在maxs数组中。最后输出驻点的坐标。
相关问题
如何在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)的驻点了。
阅读全文