Advertisement
Guest User

kalmar

a guest
Apr 9th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. T = 1
  5.  
  6. p_x = []
  7. p_y = []
  8. s = []
  9. P = []
  10. z = []
  11.  
  12. z_p = [0]
  13. e = [0]
  14. S = [0]
  15. K = [0]
  16.  
  17. P_p = []
  18. s_p = []
  19.  
  20. w = 1
  21.  
  22. P.append(np.identity(4) * 5)
  23. Q = np.identity(2) * 0.25
  24. R = np.identity(2) * 2.0
  25. I = np.identity(4)
  26.  
  27. F = np.identity(4)
  28. F[0][2] = T
  29. F[1][3] = T
  30.  
  31. G = np.ndarray((4,2))
  32. G.fill(0)
  33. G[2][0] = 1.0
  34. G[3][1] = 1.0
  35.  
  36. H = np.ndarray((2,4))
  37. H.fill(0)
  38. H[0][0] = 1.0
  39. H[1][1] = 1.0
  40.  
  41. with open("measurements17.txt", "r") as f:
  42.     lines = f.read().splitlines()
  43.  
  44. for line in lines:
  45.     first, second = line.split()
  46.     p_x.append(np.float(first))
  47.     p_y.append(np.float(second))
  48.  
  49. s.append([p_x[0],p_y[0],0,0])
  50. z.append([p_x[0],p_y[0]])
  51.  
  52. for i in range(len(lines)-1):
  53.  
  54.     s.append(F@s[i])
  55.     P.append(F@P[i]@F.T + G@Q@G.T)
  56.     z_p.append(H@s[i+1])
  57.  
  58.     z.append([p_x[i+1],p_y[i+1]])
  59.     e.append(z[i+1] - z_p[i+1])
  60.     S.append(H@P[i+1]@H.T + R)
  61.     K.append(P[i+1]@H.T @ np.linalg.inv(S[i+1]))
  62.     s[i+1] = s[i+1] + K[i+1]@e[i+1]
  63.     P[i+1] = (I - K[i+1]@H)@P[i+1]
  64.  
  65. num = 5
  66. s_p.append(s[-1])
  67. P_p.append(P[-1])
  68.  
  69. new_z = [[s[-1][0],s[-1][1]]]
  70.  
  71. for k in range(num):
  72.     s_p.append(F@s_p[k])
  73.     P_p.append(F@P_p[k]@F.T + G@Q@G.T)
  74.     new_z.append(H@s_p[k+1])
  75.  
  76. draw_x = []
  77. draw_y = []
  78.  
  79. for pred_x,pred_y in new_z:
  80.     draw_x.append(pred_x)
  81.     draw_y.append(pred_y)
  82.  
  83. label = "Przewidziana trajektoria za " + np.str(num) + "s"
  84. fig, axs = plt.subplots()
  85. axs.plot(p_x,p_y, 'rx', label='Dane pomiarowe z pliku')
  86. axs.plot([i[0] for i in s],[i[1] for i in s], 'b', label='Trajektoria samolotu')
  87. axs.plot([i[0] for i in new_z],[i[1] for i in new_z], 'g', label=label)
  88. axs.plot(new_z[-1][0],new_z[-1][1], 'go')
  89. axs.axhline(y=0, color='k')
  90. axs.axvline(x=0, color='k')
  91. axs.legend()
  92. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement