Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. def func(t,y,a,A,w):  # тут твоя функция идет
  2.     return np.array( [a*(-((y[0]**3)/3 - y[0]) + y[1]),
  3.                       -1*y[0] + A*cos(w*t)])
  4.  
  5. def nordsieck_func(z0,z1,t,h,a,A,w): #эта хуита идет в fsolve
  6.    
  7.     M = np.array([[1, 1-2/3, 1-4/3],
  8.                   [0,     0,     0],
  9.                   [0,  -1/3, 1-2/3]])
  10.     l = np.array([[2/3,1,1/3]]).transpose()
  11.     z0 = z0.reshape((3,2))
  12.     z1 = z1.reshape((3,2))
  13.    
  14.     return (h*l*np.array([[a*(-((z0[0][0]**3)/3 - z0[0][0]) + z0[0][1]), -1*z0[0][0] + A*cos(w*t)]]) + np.dot(M,z1) - z0).flatten()
  15.  
  16. def nordsieck(a,A,w,T, d = (0.01,0.001), falloff = 0.01, graphs = {1,2,3,4,5}, dim = 5, ignore_hcalc = False):
  17.     y = np.array([[2,0]])
  18.     t = 0
  19.     h = 0.001
  20.     t_arr = [0]
  21.     h_arr = []
  22.     z = np.array([y[-1], h*func(t,y[-1],a,A,w), np.array([0,0])])
  23.    
  24.     calculation = True
  25.     while calculation:
  26.            
  27.         dmax_buffer = 0
  28.         h_calculation = True
  29.         while h_calculation:
  30.    
  31.             flat_z = z.flatten()
  32.             z_new_flat = fsolve(nordsieck_func,flat_z,(flat_z,t,h,a,A,w))
  33.             z_new = z_new_flat.reshape((3,2))
  34.             if ignore_hcalc:
  35.                 break
  36.            
  37.             delta = z_new[0] - z[0]
  38.             d_max = np.amax(np.absolute(delta))
  39.            
  40.             if abs(d_max - dmax_buffer) > falloff: # на быстрых участках нужно экспоненциально менять шаг для
  41.                 dmax_buffer = d_max                # линейного уменьшения прироста
  42.             else:                                  # если находим такой участок, перестаем менять шаг
  43.                 h_calculation = False
  44.            
  45.             if d_max > d[0]:
  46.                 h -= h*0.1
  47.             elif d_max < d[1]:
  48.                 h += h*0.1
  49.             else:
  50.                 h_calculation = False
  51.        
  52.         z = z_new
  53.         t += h            
  54.         h_arr.append(h)
  55.         y = np.append(y, [z_new[0]], axis = 0)
  56.         t_arr.append(t)
  57.  
  58.         if t > T:
  59.             calculation = False
  60.        
  61.     if graphs:
  62.         n = 1
  63.         m1 = ceil(np.sqrt(len(graphs)))
  64.         m2 = ceil(len(graphs)/m1)
  65.         fig = plt.figure(figsize = (m2*dim,m1*dim))
  66.  
  67.         if 1 in graphs:
  68.             ax = fig.add_subplot(m1, m2, n)
  69.             ax.plot(t_arr[1:],h_arr)
  70.             n+=1
  71.  
  72.         if 2 in graphs:
  73.             ax = fig.add_subplot(m1, m2, n)
  74.             ax.plot(y.transpose()[0],y.transpose()[1])
  75.             n+=1
  76.         if 3 in graphs:
  77.             ax = fig.add_subplot(m1, m2, n)
  78.             ax.plot(t_arr,y.transpose()[0])
  79.             n+=1
  80.         if 4 in graphs:
  81.             ax = fig.add_subplot(m1, m2, n)
  82.             ax.plot(t_arr,y.transpose()[1])
  83.             n+=1
  84.  
  85.         if 5 in graphs:
  86.             ax = fig.add_subplot(m1, m2, n, projection='3d')
  87.             ax.plot(y.transpose()[0],y.transpose()[1],t_arr)
  88.             n+=1
  89.  
  90.         fig.show()
  91.     return [y,t_arr]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement