From eb8e0cc09c7e493a9b2e4383765027fddb7d0a65 Mon Sep 17 00:00:00 2001 From: Shiva Heidari Date: Sat, 21 Nov 2015 11:57:32 +0330 Subject: [PATCH] Solving Diffusion Equation --- shiva-heidari | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 shiva-heidari diff --git a/shiva-heidari b/shiva-heidari new file mode 100644 index 0000000..02e26b6 --- /dev/null +++ b/shiva-heidari @@ -0,0 +1,26 @@ + +# solving diffusion equation +import numpy as np +import matplotlib.pyplot as plt +%matplotlib inline +D=1 +dt=0.02 +dx=0.1 +c=(D*dt)/2*(dx*dx) +Nx=2000 +Nt=10000 +u=np.zeros((Nt,Nx)) +#u[:,0]=np.exp(-(np.arange(N)-500)**2/100) +u[0,:] = np.exp(-(np.arange(Nx)-1000)**2/10000) +for n in range (Nt-1): + for j in range (Nx-1): + u[n+1,j]=u[n,j]+c*((u[n+1,(j+1)%Nx]-2*u[n+1,j]+u[n+1,(j-1)%Nx])+(u[n,(j+1)%Nx]-2*u[n,j]+u[n,(j-1)%Nx])) + +plt.imshow(u) +plt.show() +plt.plot(u[0,:]) +plt.show() +plt.plot(u[Nt//2,:]) +plt.show() +plt.plot(u[-1,:]) +plt.show()