296
9 Solving Partial Differential Equations
Parameters can be set as
L = 0.5
beta = 8.2E-5
N = 40
x = np.linspace(0, L, N+1)
dx = x[1] - x[0]
u = np.zeros(N+1)
U_0 = np.zeros(N+1)
U_0[0] = s(0)
U_0[1:] = 283
Let us use Δt = 1.0. We can now call ode_FE and then make an animation on the
screen to see how u(x, t) develops in time:
from ode_system_FE import ode_FE
u, t = ode_FE(rhs, U_0, dt, T=1*60*60)
# Make movie
import os
os.system(’rm tmp_*.png’)
import matplotlib.pyplot as plt
plt.ion()
y = u[0,:]
lines = plt.plot(x, y)
plt.axis([x[0], x[-1], 273, s(0)+10])
plt.xlabel(’x’)
plt.ylabel(’u(x,t)’)
counter = 0
# Plot each of the first 100 frames, then increase speed by 10x
change_speed = 100
for i in range(0, u.shape[0]):
print(t[i])
plot = True if i <= change_speed else i % 10 == 0
lines[0].set_ydata(u[i,:])
if i > change_speed:
plt.legend([’t={:.0f} 10x’.format(t[i])])
else:
plt.legend([’t={:.0f}’.format(t[i])])
plt.draw()
if plot:
plt.savefig(’tmp_{:04d}.png’.format(counter))
counter += 1
#time.sleep(0.2)
The plotting statements update the u(x, t) curve on the screen. In addition,
we save a fraction of the plots to files tmp_0000.png, tmp_0001.png,
tmp_0002.png, and so on. These plots can be combined to ordinary video files. A
common tool is ffmpeg or its sister avconv.
These programs take the same type of command-line options. To make a Flash
video movie.flv, run 1
Terminal
Terminal> ffmpeg -i tmp_%04d.png -r 4 -vcodec flv movie.flv
1 You may read about using a terminal in Appendix A.
9 Solving Partial Differential Equations
Parameters can be set as
L = 0.5
beta = 8.2E-5
N = 40
x = np.linspace(0, L, N+1)
dx = x[1] - x[0]
u = np.zeros(N+1)
U_0 = np.zeros(N+1)
U_0[0] = s(0)
U_0[1:] = 283
Let us use Δt = 1.0. We can now call ode_FE and then make an animation on the
screen to see how u(x, t) develops in time:
from ode_system_FE import ode_FE
u, t = ode_FE(rhs, U_0, dt, T=1*60*60)
# Make movie
import os
os.system(’rm tmp_*.png’)
import matplotlib.pyplot as plt
plt.ion()
y = u[0,:]
lines = plt.plot(x, y)
plt.axis([x[0], x[-1], 273, s(0)+10])
plt.xlabel(’x’)
plt.ylabel(’u(x,t)’)
counter = 0
# Plot each of the first 100 frames, then increase speed by 10x
change_speed = 100
for i in range(0, u.shape[0]):
print(t[i])
plot = True if i <= change_speed else i % 10 == 0
lines[0].set_ydata(u[i,:])
if i > change_speed:
plt.legend([’t={:.0f} 10x’.format(t[i])])
else:
plt.legend([’t={:.0f}’.format(t[i])])
plt.draw()
if plot:
plt.savefig(’tmp_{:04d}.png’.format(counter))
counter += 1
#time.sleep(0.2)
The plotting statements update the u(x, t) curve on the screen. In addition,
we save a fraction of the plots to files tmp_0000.png, tmp_0001.png,
tmp_0002.png, and so on. These plots can be combined to ordinary video files. A
common tool is ffmpeg or its sister avconv.
These programs take the same type of command-line options. To make a Flash
video movie.flv, run 1
Terminal
Terminal> ffmpeg -i tmp_%04d.png -r 4 -vcodec flv movie.flv
1 You may read about using a terminal in Appendix A.
