In [2]:
from ipywidgets import interact
import matplotlib.pyplot as plt
import numpy as np

Sine functions sampled on grid

In [3]:
M = 10
L = 1
cx    = np.linspace(0,L,201) # quasi-continuum
gridx = np.linspace(0,L,M+1) # grid
cpixoverl = np.pi/L*cx
gridpixoverl = np.pi/L*gridx
def plot(p):
    plt.plot(cx,np.sin(p*cpixoverl),alpha=0.5)
    plt.plot(gridx,np.sin(p*gridpixoverl),'o-')
    plt.xlim(0,L)
    plt.ylim(-1,1)
interact(plot, p=(0,25));

Comparison of eigenvalues for PDE and 3 finite difference methods

In [13]:
sigma = .350
alpha = np.linspace(0,2*np.pi,201)
bcalpha = np.linspace(0,2*np.pi,2*M+1)
def felambda(alpha): return 1 + 2*sigma*(np.cos(alpha)-1)
def belambda(alpha): return 1/(1 + 2*sigma*(1-np.cos(alpha)))
def cnlambda(alpha): return (1 - sigma*(1 - np.cos(alpha)))/(1 + sigma*(1 - np.cos(alpha)))
truelambda = np.exp(-sigma*alpha**2)
plt.plot(alpha,truelambda,label='PDE')
plt.plot(alpha,felambda(alpha),label='forward Euler')
plt.plot(alpha,belambda(alpha),label='backward Euler')
plt.plot(alpha,cnlambda(alpha),label='Crank-Nicolson')
for y in [-1,0,1]: plt.axhline(y,color='k',alpha=0.25)
for x in [0,np.pi,2*np.pi]: plt.axvline(x,color='k',alpha=0.25)
plt.legend();
In [ ]: