Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.animation import FuncAnimation
  4.  
  5. L = 10
  6. delta_u = 0.1
  7. E = 110e9
  8. rho = 4300
  9. N_x = 100
  10. h = L / N_x
  11. tau = h / np.sqrt(E/rho)
  12. T = 1
  13. N_t = int(T/tau)
  14. first = 1
  15. second = 1
  16.  
  17.  
  18. # u(x,0)
  19. def p(x):
  20.     return (10*x - 100) * x
  21.  
  22.  
  23. def f(x):
  24.     return 0
  25.  
  26.  
  27. # u'(x,0)
  28. def q(x):
  29.     return 0
  30.  
  31.  
  32. def solver():
  33.     x_s = np.linspace(0, L, N_x)
  34.     a = E/rho * tau**2 / h**2
  35.     U = np.zeros((N_t, N_x), dtype=np.float)
  36.     U[:][0] = 0
  37.     U[:][-1] = 0
  38.     U[0][1:-1] = p(x_s[1:-1])
  39.     U[1][1:-1] = p(x_s[1:-1]) + tau * q(x_s[1:-1]) + tau**2 / 2 * (f(x_s[1:-1]) + p(x_s[2:]) - 2 * p(x_s[1:-1]) + p(x_s[2:])/h**2)
  40.     for i in range(2, N_t):
  41.         U[i][1:-1] = -U[i-2][1:-1] + a * (U[i-1][2:] + U[i-1][:-2]) + U[i-1][1:-1] * (2 - 2*a)
  42.     return U, x_s
  43.  
  44.  
  45. def solution_drawer(u, x):
  46.     u_lines = [u[:][i] for i in range(u.shape[1])]
  47.     for u_x in u_lines:
  48.         plt.plot(x, u_x)
  49.     plt.show()
  50.  
  51.  
  52. U, X = solver()
  53. # solution_drawer(U, X)
  54.  
  55. fig = plt.figure()
  56. ax = plt.axes(xlim=(0, 10), ylim=(-300, 300))
  57. line, = ax.plot([], [], lw=3)
  58. ax.grid()
  59.  
  60.  
  61.  
  62. def init():
  63.     line.set_data([], [])
  64.     return line,
  65.  
  66.  
  67. def animate(i):
  68.     line.set_data(X, U[i])
  69.     return line,
  70.  
  71.  
  72. anim = FuncAnimation(fig, animate, init_func=init,
  73.                      frames=len(U), interval=20, blit=False)
  74.  
  75. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement