Page - 169 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 169 -
Text of the Page - 169 -
5.1 FiniteDifferenceMethods 169
def dsdt(t):
return 0
def g(x, t):
return 0
Parameterscanbeset as
L = 0.5
beta = 8.2E-5
N = 40
x = linspace(0, L, N+1)
dx = x[1] - x[0]
u = zeros(N+1)
U_0 = zeros(N+1)
U_0[0] = s(0)
U_0[1:] = 283
Letususe t D0:00034375.Wecannowcallode_FEandthenmakeananimation
on the screen toseehowu.x;t/develops in time:
t0 = time.clock()
from ode_system_FE import ode_FE
u, t = ode_FE(rhs, U_0, dt, T=1*60*60)
t1 = time.clock()
print βCPU time: %.1fsβ % (t1 - t0)
# 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β % t[i]])
else:
plt.legend([βt=%.0fβ % t[i]])
plt.draw()
if plot:
plt.savefig(βtmp_%04d.pngβ % counter)
counter += 1
#time.sleep(0.2)
back to the
book Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2016
- Language
- English
- License
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 248
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik