-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaves.py
executable file
·45 lines (41 loc) · 1.02 KB
/
waves.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
import math
import numpy as np
import matplotlib.pyplot as plt
L = 1
s = 500
# c = 20
# deltax = L / s
# deltat = deltax / c
y = np.zeros((s, s))
for t in range(s):
for x in range(s):
# always zero at boundaries
if x == 0 or x == s - 1:
y[x, t] = 0
# base case: sine wave
elif t == 0:
y[x, t] = 5 * math.sin(4 * math.pi * x / s)
# second base case: weird hack
elif t == 1:
y[x, t] = y[x, 0] + 0.5 * (
y[x + 1, 0] +
y[x - 1, 0] -
2 * y[x, 0]
)
# all other timesteps
else:
y[x, t] = (
2 * y[x, t - 1] -
y[x, t - 2] + (
y[x + 1, t - 1] +
y[x - 1, t - 1] -
2 * y[x, t - 1]
))
# graph that shit
ax = plt.axes(projection="3d")
t = np.arange(len(y))
x = np.arange(len(y[0]))
(x, t) = np.meshgrid(x, t)
ax.plot_surface(x, t, y)
plt.show()