-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwave_eq_on_R.py
67 lines (48 loc) · 1.54 KB
/
wave_eq_on_R.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
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import matplotlib.animation as animation
c = 1
startt = -3
endt = 3
n = 200
dt = (endt-startt)/n
times = np.linspace(startt,endt,n)
startx = -10
endx = 10
n = 1000
dx = (endx-startx)/n
xs = np.linspace(startx,endx,n)
# Write f such that it takes a numpy array of x-values and returns a numpy array
# OBS! If f returns a regular python list, things will end badly
# See below for the interpretation of f
# Gauss
#f = lambda xs: np.exp(-xs**2)
# Two Gausses
#f = lambda xs: np.exp(-(xs-5)**2) + np.exp(-(xs+5)**2)
# Heaviside
#f = lambda xs: np.array([int(x < 0) for x in xs])
# Spikes
#f = lambda xs: np.where(np.abs(np.mod(xs,4)-2)-1.5 > 0, np.abs(np.mod(xs,4)-2)-1.5, np.zeros(xs.shape))
# Photon
f = lambda xs: 0.5*np.exp(-xs**2)*np.cos(10*xs)
# Define ufcn based on the initial condition
# Initial condition , u(x,0) = f(x), d/dt u(x,0) = 0
ufcn = lambda t: f(xs-c*t) + f(xs+c*t)
# Initial condition, u(x,0) = 0, d/dt u(x,0) = f'(x)
#ufcn = lambda t: 0.5*(f(xs+c*t) - f(xs-c*t))
# Create an animation
us = ufcn(0)
fig,ax = plt.subplots()
line, = ax.plot(xs,us,'w')
ax.set_ylim([-1,1])
def animate(t):
us = ufcn(t)
line.set_ydata(us)
line.set_color('r')
return line,
anim = FuncAnimation(fig,animate,init_func=None,frames = times, interval=50,blit=True,repeat=True, repeat_delay=0)
Writer = animation.writers['imagemagick']
writer = Writer(fps=30, bitrate=1800)
anim.save('unboundedwave.gif',writer=writer)
plt.show()