# Parabolic PDE¶

MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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)$$

In [2]:
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import math
import sympy as sym


## Solution¶

$$u(t,x) = e^{-\pi^2t}sin(\pi x)$$

Check symbolically.

In [3]:
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)

Out[3]:
$\displaystyle 0$

## Set Up the Grid¶

• dx: grid spacing in the $x$-direction
• x: grid coordinates
In [4]:
nx = 200
x = np.linspace(0, 1, nx, endpoint=False)
dx = x[1] - x[0]


## Set Time Parameters¶

In [5]:
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


## Set Initial Condition¶

In [6]:
PI = math.pi
def g(x):
return np.sin(PI*x)


## Plot Solution¶

In [7]:
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

Out[7]: