-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaves2.py
executable file
·67 lines (58 loc) · 1.76 KB
/
waves2.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
import math
L = 20
offsetX = -10
offsetY = -10
s = 200
timesteps = 500
z = np.zeros((s, s, timesteps))
for t in range(timesteps):
for y in range(s):
for x in range(s):
realX = (x*L/s + offsetX)
realY = (y*L/s + offsetY)
# always zero at boundaries
if x == 0 or x == s - 1 or y == 0 or y == s - 1:
z[x, y, t] = 0
# base case: sine wave
elif t == 0:
z[x, y, t] = (
math.exp(-(realX ** 2 + realY ** 2) / 50) *
math.sin(0.2 * math.pi * realX) *
math.sin(0.2 * math.pi * realY)
)
# second base case: weird hack
elif t == 1:
z[x, y, t] = z[x, y, 0] + 0.5 * (
z[x + 1, y, 0] +
z[x - 1, y, 0] -
z[x, y + 1, 0] +
z[x, y - 1, 0] -
2 * z[x, y, 0]
)
# all other timesteps
else:
z[x, y, t] = (
2 * z[x, y, t - 1] -
z[x, y, t - 2] + (
z[x + 1, y, t - 1] +
z[x - 1, y, t - 1] -
z[x, y + 1, t - 1] +
z[x, y - 1, t - 1] -
2 * z[x, y, t - 1]
))
y = np.arange(s)
x = np.arange(s)
(x, y) = np.meshgrid(x, y)
# ax = plt.axes(projection="3d")
# ax.plot_surface(x, y, z[:, :, 10])
graphCount = 10
figure, ax = plt.subplots(
ncols = graphCount,
subplot_kw = dict(projection = "3d")
)
for t in range(graphCount):
ax[t].plot_surface(x, y, z[:, :, t])
plt.show()