Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import math
  4.  
  5. N = 129
  6. err = []
  7. def error(analytical,numerical):
  8.     return np.sum(abs((numerical[(N/2)-2]-analytical[(N/2)-2])))
  9.  
  10. def sigma(t,D):
  11.     return math.sqrt(2*D*t)
  12.          
  13. def analytical(t,x,D):
  14.     firstPart = 1/(sigma(t,D)*math.sqrt(2*math.pi))
  15.     secondPart = math.exp(-((x-L/2)**2)/(2*sigma(t,D)**2))
  16.     return firstPart*secondPart
  17.  
  18.  
  19. testNs = np.linspace(20,200,20)
  20. for i in testNs:
  21.     N = int(i)
  22.  
  23.      
  24.     L = 2*math.pi
  25.     D = 0.1
  26.     h = L / (N-1)
  27.      
  28.     dt = 0.97*((h**2)/(2*D))
  29.      
  30.     vonnewman = h**2/2*D
  31.     totaltime = 2
  32.      
  33.     U = np.zeros(((int(totaltime/dt)),N))
  34.      
  35.     U[0][len(U[0])/2] = 1/h
  36.      
  37.     U[:,0] = 0
  38.     U[:,-1] = 0
  39.      
  40.     for i in range(1,int(U.shape[0])):
  41.         for j in range(1,int(U.shape[1])-1):
  42.             U[i][j] = U[i-1][j]+((D*dt)/h**2)*(U[i-1][j-1]+U[i-1][j+1]-2*U[i-1][j])
  43.                  
  44.     A = np.zeros(((int(totaltime/dt)),N))  
  45.     for i in range(1,int(U.shape[0])):
  46.         iC = i * dt
  47.         for j in range(int(U.shape[1])-1):
  48.             jC = j * h
  49.             A[i][j] = analytical(iC,jC,D)
  50.      
  51.      
  52.     #plt.xlabel('Time')
  53.     #plt.ylabel('Concentration')
  54.     #plt.plot(A[-1])
  55.     #plt.plot(U[-1])
  56.     #plt.show()  
  57.      
  58.     err.append(error(A[-1],U[-1]))
  59.  
  60. plt.plot(testNs,err)
  61. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement