Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3.  
  4. X_0=93
  5. a=-0.5
  6. T=40
  7. h=0.1
  8. Y_expected=50
  9. Y_0=100
  10. iterations=(T/h).__int__()
  11. hysteresis = 5
  12.  
  13. X_results=np.zeros(shape=iterations)
  14. Y_results=np.zeros(shape=iterations)
  15. U_results=np.zeros(shape=iterations)
  16. T_results=np.zeros(shape=iterations)
  17.  
  18. X_results[0]=X_0
  19. Y_results[0]=Y_0
  20. T_results[0]=0
  21.  
  22.  
  23. U_prev = 0
  24.  
  25. for itr in range(0,iterations-1):
  26.  
  27.     error = Y_expected - Y_results[itr]
  28.  
  29.     U = U_prev
  30.  
  31.     if (error > 0 and Y_results[itr] < Y_expected - hysteresis):
  32.         U = 30
  33.     elif(error <= 0 and Y_results[itr] > Y_expected + hysteresis):
  34.         U = 0
  35.  
  36.     U_results[itr] = U
  37.  
  38.     U_prev = U
  39.  
  40.     X_results[itr+1] = X_results[itr] + (a*X_results[itr] + U_results[itr]) * h
  41.     Y_results[itr+1] = Y_results[itr] + (a*Y_results[itr] + U_results[itr]) * h
  42.  
  43.     print("Y: ")
  44.     print(Y_results[itr])
  45.     print("Error: ")
  46.     print(error)
  47.     print('')
  48.  
  49.     T_results[itr + 1] = T_results[itr] + h
  50.  
  51. plt.figure()
  52. plt.plot(T_results,X_results)
  53. plt.title("X to T")
  54. plt.show()
  55.  
  56. plt.figure()
  57. plt.plot(T_results,Y_results)
  58. plt.title("Y to T")
  59. plt.show()
  60.  
  61. plt.figure()
  62. plt.subplot(2,1,1)
  63. plt.plot(T_results,Y_results)
  64. plt.axis([2,8,40,100])
  65. plt.grid(True)
  66. plt.title("Y to T")
  67. plt.subplot(2,1,2)
  68. plt.plot(T_results,U_results,'bo')
  69. plt.axis([2,8,0,100])
  70. plt.grid(True,color='red')
  71. plt.title("U to T")
  72. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement