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) $$
numpy
is used for vectors and linear algebrascipy
is unused here, but is common for scientific computing applicationsmatpllotlib.pyplot
is used for plotting%matplotlib inline
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import math
dx: grid spacing in the $x$-direction
x: grid coordinates
xx: really fine 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)
PI = math.pi
def g(x):
return np.sin(PI*x)
import time
plotit = True
u = g(x)
if plotit:
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111)
ax.set_title('u vs x')
line1, = ax.plot(x, u, lw=3, clip_on=False)
def init():
line1.set_data([], [])
def timestepper(n):
uex = np.exp(-1*PI**2*(n+1)*dt)*g(x)
line1.set_data(x, uex)
return line1
from JSAnimation import IPython_display
from matplotlib import animation
animation.FuncAnimation(fig, timestepper, init_func=init, frames=nt, interval=10, blit=True)