Advertisement
Guest User

3d kalmar

a guest
Apr 9th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from mpl_toolkits.mplot3d import Axes3D
  4.  
  5. T = 1
  6.  
  7. p_x = []
  8. p_y = []
  9. s = []
  10. P = []
  11. z = []
  12.  
  13. z_p = [0]
  14. e = [0]
  15. S = [0]
  16. K = [0]
  17.  
  18. P_p = []
  19. s_p = []
  20.  
  21. w = 1
  22.  
  23. P.append(np.identity(4) * 5)
  24. Q = np.identity(2) * 0.25
  25. R = np.identity(2) * 2.0
  26. I = np.identity(4)
  27.  
  28. F = np.identity(4)
  29. F[0][2] = T
  30. F[1][3] = T
  31.  
  32. G = np.ndarray((4,2))
  33. G.fill(0)
  34. G[2][0] = 1.0
  35. G[3][1] = 1.0
  36.  
  37. H = np.ndarray((2,4))
  38. H.fill(0)
  39. H[0][0] = 1.0
  40. H[1][1] = 1.0
  41.  
  42. with open("measurements17.txt", "r") as f:
  43.     lines = f.read().splitlines()
  44.  
  45. for line in lines:
  46.     first, second = line.split()
  47.     p_x.append(np.float(first))
  48.     p_y.append(np.float(second))
  49.  
  50. s.append([p_x[0],p_y[0],0,0])
  51. z.append([p_x[0],p_y[0]])
  52.  
  53. for i in range(len(lines)-1):
  54.  
  55.     s.append(F@s[i])
  56.     P.append(F@P[i]@F.T + G@Q@G.T)
  57.     z_p.append(H@s[i+1])
  58.  
  59.     z.append([p_x[i+1],p_y[i+1]])
  60.     e.append(z[i+1] - z_p[i+1])
  61.     S.append(H@P[i+1]@H.T + R)
  62.     K.append(P[i+1]@H.T @ np.linalg.inv(S[i+1]))
  63.     s[i+1] = s[i+1] + K[i+1]@e[i+1]
  64.     P[i+1] = (I - K[i+1]@H)@P[i+1]
  65.  
  66. num = 5
  67. s_p.append(s[-1])
  68. P_p.append(P[-1])
  69.  
  70. new_z = [[s[-1][0],s[-1][1]]]
  71.  
  72. for k in range(num):
  73.     s_p.append(F@s_p[k])
  74.     P_p.append(F@P_p[k]@F.T + G@Q@G.T)
  75.     new_z.append(H@s_p[k+1])
  76.  
  77. draw_x = []
  78. draw_y = []
  79.  
  80. for pred_x,pred_y in new_z:
  81.     draw_x.append(pred_x)
  82.     draw_y.append(pred_y)
  83.  
  84. t = np.arange(0,len(s),1)
  85. t_2 = np.arange(len(s)-1,len(s)+num,1)
  86.  
  87. label = "Przewidziana trajektoria za " + np.str(num) + "s"
  88. fig, axs = plt.subplots(2,2)
  89. gs = axs[0, 0].get_gridspec()
  90.  
  91. for ax in axs[:, 0]:
  92.     ax.remove()
  93. axbig = fig.add_subplot(gs[:, 0], projection='3d')
  94. fig.tight_layout()
  95.  
  96. axbig.plot3D([i[0] for i in s], [i[1] for i in s],t)
  97.  
  98. axbig.plot3D([i[0] for i in s],[i[1] for i in s],t, 'b', label='Trajektoria samolotu')
  99. axbig.plot3D(p_x,p_y,t, 'rx', label='Dane pomiarowe z pliku')
  100. axbig.plot3D([i[0] for i in new_z],[i[1] for i in new_z],t_2, 'g', label=label)
  101. axbig.plot3D([new_z[-1][0]],new_z[-1][1],[t_2[-1]], 'go')
  102. axbig.set_xlabel("Położenie X")
  103. axbig.set_ylabel("Położenie Y")
  104. axbig.set_zlabel("Czas")
  105. axbig.legend()
  106.  
  107. axs[0][1].plot(t,[i[2] for i in s], 'b', label='Prędkość w osi X')
  108. axs[1][1].plot(t,[i[3] for i in s], 'b', label='Prędkość w osi Y')
  109.  
  110. axs[0][1].plot(t_2,[i[2] for i in s_p], 'g', label='Prędkość w osi X przewidziana jest stała')
  111. axs[1][1].plot(t_2,[i[3] for i in s_p], 'g', label='Prędkość w osi Y przewidziana jest stała)')
  112.  
  113. for ax in axs:
  114.      ax[1].grid(True, which='both')
  115.      ax[1].axhline(y=0, color='k')
  116.      ax[1].axvline(x=0, color='k')
  117.      ax[1].set_ylabel("V [m/s]")
  118.      ax[1].set_xlabel("Czas [s]")
  119.      ax[1].legend()
  120.  
  121. fig.set_size_inches(14,5)
  122. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement