Advertisement
Guest User

Untitled

a guest
May 21st, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1.  
  2. # coding: utf-8
  3.  
  4. # In[21]:
  5.  
  6. # 3 Set 3
  7. # 3.1 Eigenmodes of drums or membranes of dierent shapes
  8.  
  9.  
  10. # In[45]:
  11.  
  12. import numpy as np
  13. import scipy.sparse.linalg as sc
  14. import scipy.linalg as scnorm
  15. import matplotlib.pyplot as plt
  16. size = 100
  17.  
  18. #1. create the matrix a
  19. def matrix(size):
  20. dx = size / (size - 1)
  21. B = np.identity(size ) * (-4)
  22. counter = 0
  23. sizeCounter = size
  24. for i in range(1, size):
  25. B[i, counter] = 1
  26. B[counter, i] = 1
  27. counter += 1
  28. B[0,:]=0
  29. B[:,0]=0
  30. B[size-1,:]=0
  31. B[:,size-1]=0
  32.  
  33. I=np.identity(size)
  34. I[0,:]=0
  35. I[:,0]=0
  36. I[size-1,:]=0
  37. I[:,size-1]=0
  38.  
  39.  
  40. return B,I
  41.  
  42. #used to find the middle of the matrix
  43.  
  44. # def middle(B, dx):
  45. # B = B / dx**2
  46. # horizontal = np.floor(len(B) / 2)
  47. # vertical = horizontal
  48. # for i in range(len(B)):
  49. # for j in range(len(B)):
  50. # if( np.sqrt((i - horizontal)**2 + (j - vertical)**2) > horizontal): B[i, j] = 0
  51. # if(i == j and B[i, j] == 0): B[i, j] = 1
  52.  
  53. # return B, vertical, horizontal
  54.  
  55. #2. create the vector b with starting conditions
  56. def vector(size):
  57. b = np.zeros([size, size])
  58. vertical = np.floor(len(b) / 2)
  59. point06 = np.floor((len(b) - vertical) * 0.3)
  60. point12 = np.floor((len(b) - vertical) * 0.6)
  61. b[int(point06), int(point12)] = 1
  62. bcolumn = b.T.reshape(size**2, 1)
  63.  
  64. return bcolumn
  65. #
  66. # #3. solve the problem
  67. # solution = sc.spsolve(matrix(size), vector(size))
  68. # solution.reshape(size, size)
  69.  
  70.  
  71. B, I = matrix(size)
  72. A = np.zeros((size, size))
  73.  
  74. M = []
  75. for i in range(size):
  76. if i == 0:
  77. M.append(np.concatenate([ B, I, np.tile(A, (1, size-2)) ], axis = -1 ))
  78. elif i == size-1:
  79. M.append(np.concatenate([ np.tile(A, (1, size-2)), I, B ], axis = -1))
  80. else:
  81. M.append(np.concatenate([ np.tile(A, (1, i-1)), I, B, I, np.tile(A, (1, size-i-2))], axis = -1))
  82. M = np.concatenate(M, axis = 0)
  83.  
  84.  
  85. # make the matrix not singular
  86. for i in range(size**2):
  87.  
  88. for j in range(size**2):
  89.  
  90. if i==j:
  91. if M[i,j]==0 :
  92.  
  93. M[i,j]=1
  94.  
  95. M = M / (1/size)**2
  96.  
  97. #print(M)
  98. # plt.imshow(M,interpolation='none')
  99. # plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement