In [15]:
%pylab inline
import numpy as np
import matplotlib.pyplot as plt
Populating the interactive namespace from numpy and matplotlib
In [16]:
x = np.linspace(0.,1)
v = 0*x
u = np.exp(-100*(x-0.5)**2)
plt.plot(x,u)
Out[16]:
[<matplotlib.lines.Line2D at 0x1083ce790>]
In [17]:
tfinal = .2
dx = x[1]-x[0]
dt = 0.5*dx
nsteps = int(round(tfinal/dt))
for i in range(nsteps):
    # Diffusion
    v[1:-1] = v[1:-1] + dt/dx**2 * (u[2:] - 2*u[1:-1] + u[:-2])
    # Reaction
    u = u + dt*v
In [18]:
plt.plot(x,u)
Out[18]:
[<matplotlib.lines.Line2D at 0x1083f8850>]
In [ ]: