From 40f52ba78fb265a22743163831aaa2fd2fa37145 Mon Sep 17 00:00:00 2001 From: mrshnra Date: Sun, 22 Nov 2015 07:54:27 -0500 Subject: [PATCH] added the spolution for the wave equation via leap frog --- PDE.ipynb | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/PDE.ipynb b/PDE.ipynb index b1665e7..2bb9ba8 100644 --- a/PDE.ipynb +++ b/PDE.ipynb @@ -335,3 +335,48 @@ "nbformat": 4, "nbformat_minor": 0 } + +import numpy as np +import matplotlib.pyplot as plt +%matplotlib inline + +Nx, Nt = 200, 5000 + +v = 1 +dt = 0.04 +dx = 0.1 +delta = v**2 * dt**2 / dx**2 + +u = np.zeros((Nt,Nx)) + +#u[0,:] = np.sin(2*np.pi*np.arange(Nx)/Nx) +u[0,:] = np.exp(-(np.arange(Nx)-100)**2/1000) +#u[0,100]=1 +#u[0,101]=1 + +#u[1,100]=1 +#u[1,101]=1 + +#u[0,80:110] =1 + +#u[1,:] = np.exp(-(np.arange(Nx)-100)**2/500) +for j in range(Nx): + u[1,j]=2*u[0,j-1] - (u[0,(j+1)%Nx] - u[0,j-1]) * delta + #u[1,j]=u[0,j-1] + +for n in range(2,Nt): + for j in range(Nx): + u[n,j] = - u[n-2,j] + 2*u[n-1,j] + (u[n-1,(j+1)%Nx] + u[n-1,j-1] - 2*u[n-1,j]) * delta + +plt.imshow(u) +plt.show() +plt.plot(u[0,:]) +plt.show() +plt.plot(u[200,:]) +plt.show() +plt.plot(u[800,:]) +plt.show() +plt.plot(u[1000,:]) +plt.show() +plt.plot(u[1200,:]) +plt.show()