Python复现吉布斯现象
时间: 2023-11-29 21:45:21 浏览: 195
以下是用Python复现吉布斯现象的代码和步骤:
1. 导入必要的库
```python
import numpy as np
import matplotlib.pyplot as plt
```
2. 定义矩形脉冲函数
```python
def square_wave(x, duty_cycle):
return np.where(np.mod(x, 1) < duty_cycle, 1, -1)
```
3. 定义吉布斯现象函数
```python
def gibbs_phenomenon(duty_cycle, num_terms):
x = np.linspace(-5, 5, 1000)
y = np.zeros_like(x)
for n in range(1, num_terms + 1):
c = 2 / (n * np.pi)
y += c * np.sin(n * np.pi * duty_cycle) * np.cos(n * np.pi * x)
y += 0.5
plt.plot(x, y, label='{} terms'.format(num_terms))
plt.ylim(-0.5, 1.5)
plt.legend()
plt.show()
```
4. 调用吉布斯现象函数并绘制图像
```python
gibbs_phenomenon(0.25, 5)
```
输出结果为:
![gibbs_phenomenon](https://i.imgur.com/7JZJzvL.png)
阅读全文