Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import math
  4. import random
  5. T = 30
  6. h = 0.01
  7. size = int(T / h)
  8.  
  9. kp = 5 # im większe tym dokładniej pracuje układ regulacji
  10. ki = 2 #czym wieksze tym czestsze falki
  11. y_set = 50
  12. y_0 = 40
  13. a = -0.05
  14.  
  15.  
  16. y = np.zeros(size)
  17. e = np.zeros(size)
  18. u = np.zeros(size)
  19. t = np.zeros(size)
  20. y_need_arr = np.array(size)
  21. x1 = np.zeros(size)
  22. x2 = np.zeros(size)
  23. x3 = np.zeros(size)
  24.  
  25. # y[0] = y_0
  26. e[0] = y_set - y[0]
  27. u[0] = kp*e[0]
  28. e_sum = e[0]*h
  29. x1[0] = 4
  30. x2[0] = 7
  31. x3[0] = 5
  32. y[0] = x3[0]
  33.  
  34. for i in range(size - 1):
  35.  
  36. # x[i+1] = x[i] + h * (y[i])
  37. # y[i+1] = y[i] + h * (-a * x[i] - b * y[i] + u[i] + r * np.random.rand())
  38.  
  39. x1[i+1] = x1[i] + h * (-x1[i] + x2[i] + u[i])
  40. x2[i+1] = x2[i] + h *(x1[i] -2*x2[i] + x3[i])
  41. x3[i + 1] = x3[i] + h * (x2[i] - 2*y[i])
  42. y[i+1] = x3[i+1]
  43.  
  44. # y[i + 1] = y[i] + h * (a * y[i] + u[i])
  45. e[i + 1] = y_set - y[i + 1]
  46. e_sum = e_sum + e[i + 1] * h
  47. u[i+1] = kp*e[i+1] + ki *e_sum
  48. t[i + 1] = t[i] + h
  49.  
  50. # if(i == 3000):
  51. # y_set = 80
  52.  
  53. # i = t/h
  54. # print(random.uniform(0, 1))
  55.  
  56. plt.subplot(311)
  57. # plt.hlines(y_set, 0, T, 'b', 'dashed')
  58. # plt.hlines(50, 0, 30, 'b', 'dashed')
  59. # plt.plot(t[3000:], np.ones(3000) * y_set, 'b', markersize=1)
  60. # plt.plot(t[3000:], np.ones(size-3000) * 80, 'b', label='y*', markersize=1)
  61. # plt.plot(t[:3000], np.ones(3000) * 50, 'b', label='y*', markersize=1)
  62. # plt.plot(t[:3000], np.ones(3000) * y_set, 'b', label='y*', markersize=1)
  63. # plt.hlines(80, 30, T, 'b', 'dashed')
  64. plt.plot(t, x3, 'r', label='y', markersize=1)
  65. plt.plot(t, x1, 'b', label='y', markersize=1)
  66. plt.plot(t, x2, 'y', label='y', markersize=1)
  67. # plt.legend(bbox_to_anchor=(1.005, 1), loc=2, borderaxespad=0.)
  68. plt.ylabel('y(t)')
  69. plt.xlabel('t')
  70.  
  71.  
  72. plt.subplot(312)
  73. plt.plot(t, e, 'r', markersize=1)
  74. plt.hlines(0,0, T,'b','dashed')
  75. plt.ylabel('e(t)')
  76. plt.xlabel('t')
  77.  
  78. plt.subplot(313)
  79. plt.plot(t, u, 'r', markersize=1)
  80. plt.ylabel('u(t)')
  81. plt.xlabel('t')
  82.  
  83. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement