Copyright (C) 2010-2020 Luke Olson
Copyright (C) 2020 Andreas Kloeckner
Heat equation:
$$ u_t - c u_{xx} = 0\quad \text{in}\, \Omega\, \times\, [0,T] $$
$$ u(0,x) = g(x)\quad \text{in}\, \Omega\, $$
$$ u(t,0) = u(t,1) = 0 $$
Let:
$$ g(x) = sin(\pi x) $$
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import math
import sympy as sym
Check symbolically.
sx = sym.Symbol("x")
st = sym.Symbol("t")
su = sym.exp(-sym.pi**2*st) * sym.sin(sym.pi * sx)
sym.diff(su, st) - sym.diff(su, sx, 2)
dx
: grid spacing in the $x$-direction x
: grid coordinates nx = 200
x = np.linspace(0, 1, nx, endpoint=False)
dx = x[1] - x[0]
T = 1.0 # end time
dt = 0.009
nt = int(T/dt)
print('T = %g' % T)
print('tsteps = %d' % nt)
print(' dx = %g' % dx)
print(' dt = %g' % dt)
T = 1 tsteps = 111 dx = 0.005 dt = 0.009
PI = math.pi
def g(x):
return np.sin(PI*x)
import time
plotit = True
u = g(x)
fig = plt.figure(figsize=(8,8))
plt.title('u vs x')
line1, = plt.plot(x, u, lw=3, clip_on=False)
def timestepper(n):
uex = np.exp(-1*PI**2*(n+1)*dt)*g(x)
line1.set_data(x, uex)
return line1
from matplotlib import animation
from IPython.display import HTML
ani = animation.FuncAnimation(fig, timestepper, frames=nt, interval=20)
html = HTML(ani.to_jshtml())
plt.clf()
html