Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. import random
  2.  
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5.  
  6.  
  7. def error(y):
  8. return Y_expected - y
  9.  
  10.  
  11. a = -0.02
  12. b = 0.7
  13. c = 0.3
  14. T = 100
  15. h = 0.1
  16. Y_expected = 50
  17. Y_0 = 0
  18. X_0 = 0
  19. iterations = (T / h).__int__()
  20. hysteresis = 5
  21. K_P = 0.1
  22. K_I = 0.1
  23.  
  24. X_results = np.zeros(shape=iterations)
  25. Y_results = np.zeros(shape=iterations)
  26. Y_exp_results = np.zeros(shape=iterations)
  27. U_results = np.zeros(shape=iterations)
  28. E_results = np.zeros(shape=iterations)
  29. E_sum_results = np.zeros(shape=iterations)
  30. T_results = np.zeros(shape=iterations)
  31.  
  32. X_results[0] = X_0
  33. Y_results[0] = Y_0
  34. E_results[0] = error(Y_0)
  35. E_sum_results[0] = error(Y_0)
  36. T_results[0] = 0
  37. U_results[0] = 0
  38.  
  39. r = 0
  40.  
  41. for itr in range(0, iterations - 1):
  42.  
  43. print("U")
  44. print(U_results[itr])
  45. print("AKTUALNE X")
  46. print(X_results[itr])
  47. print("NASTEPNE X")
  48. print(X_results[itr] + (a * X_results[itr] + U_results[itr]) * h)
  49. print("Y")
  50. print( Y_results[itr])
  51. print('')
  52.  
  53.  
  54. U_results[itr+1] = K_P * error(Y_results[itr]) + K_I * E_sum_results[itr]
  55.  
  56. Y_exp_results[itr] = Y_expected
  57.  
  58. X_results[itr + 1] = X_results[itr] + (U_results[itr]+r*np.random.randn(1)-b*X_results[itr]-c*Y_results[itr]) * h
  59.  
  60. Y_results[itr + 1] = Y_results[itr] + (X_results[itr]) * h
  61.  
  62. E_results[itr + 1] = error(Y_results[itr])
  63.  
  64. E_sum_results[itr+1] = E_sum_results[itr] + E_results[itr]*h
  65.  
  66. T_results[itr + 1] = T_results[itr] + h
  67.  
  68. plt.figure()
  69. plt.plot(T_results, X_results)
  70. plt.title("X to T")
  71. plt.show()
  72.  
  73. plt.figure()
  74. plt.plot(T_results, Y_results)
  75. plt.title("Y to T")
  76. plt.show()
  77.  
  78. plt.figure()
  79. plt.subplot(3, 1, 1)
  80. plt.plot(T_results, Y_results, label='y')
  81. plt.plot(T_results,Y_exp_results, label='y expected', color='red')
  82. #plt.plot(T_results, np.ones(T_results.shape) * Y_expected, label='y expected', color='red')
  83. # plt.axis([1,5,0,100])
  84. plt.legend()
  85. plt.grid(True)
  86. plt.title("Y to T")
  87.  
  88. plt.subplot(3, 1, 2)
  89. plt.plot(T_results, E_results)
  90. # plt.axis([1,5,0,-50])
  91. plt.title("E to T")
  92.  
  93. plt.subplot(3, 1, 3)
  94. plt.plot(T_results, U_results)
  95. # plt.axis([1,5,0,50])
  96. plt.grid(True, color='red')
  97. plt.title("U to T")
  98. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement