Skip to content

Commit 28a281b

Browse files
authored
Create Gibbs.py
1 parent 2a94436 commit 28a281b

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Gibbs.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
x = np.linspace(0,2*np.pi,5000)
5+
6+
square_wave = np.ones_like(x)
7+
square_wave[int(x.size/2):]=-1
8+
9+
N = 30
10+
fsq = np.zeros_like(x)
11+
for i in range(N):
12+
n = 2*i + 1
13+
fsq += np.sin(n *x) / n
14+
fsq *= 4 / np.pi
15+
16+
fig, ax = plt.subplots()
17+
ax.plot(x, square_wave, lw=5, alpha=0.5)
18+
ax.plot(x, fsq, 'r')
19+
ax.set_ylim(-1.2,1.2)
20+
plt.title('N='+str(N))
21+
22+
ax.set_xticks([0,1,2,3,4,5,6,7])
23+
ax.set_xticks([0.5,1.5,2.5,3.5,4.5,5.5,6.5], minor=True)
24+
ax.set_yticks([-1, 0, 1])
25+
ax.set_yticks(np.arange(-1.2,1.2,0.2), minor=True)
26+
27+
ax.grid(b=True, c='k', lw=1, ls='--', which='major')
28+
ax.grid(b=True, c='0.4', lw=0.5, ls=':', which='minor')
29+
30+
plt.show()
31+

0 commit comments

Comments
 (0)