Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3.  
  4.  
  5. def createArray(x0, shape):
  6. arr = np.zeros(shape)
  7. arr[0] = x0
  8. return arr
  9.  
  10.  
  11. def yPrim(y, u):
  12. return a * y + u
  13.  
  14.  
  15. def error(y):
  16. return y_target - y
  17.  
  18.  
  19. def reg(e):
  20. global state
  21. if e > maxError:
  22. state = True
  23. if e < -maxError:
  24. state = False
  25. return u_on if state else u_off
  26.  
  27.  
  28. h = 0.01
  29. t0 = 0
  30. T = 100
  31. a = -0.025
  32. u_on = 2
  33. u_off = 0
  34. state = False
  35. maxError = 5
  36. y_target = 50
  37. y0 = 75
  38.  
  39. tTab = np.arange(t0, T, h)
  40. yTab = createArray(y0, np.shape(tTab))
  41. eTab = createArray(error(yTab[0]), np.shape(tTab))
  42. uTab = createArray(reg(eTab[0]), np.shape(tTab))
  43.  
  44. for i in range(tTab.shape[0] - 1):
  45. yTab[i + 1] = yTab[i] + h * yPrim(yTab[i], uTab[i])
  46. eTab[i + 1] = error(yTab[i + 1])
  47. uTab[i + 1] = reg(eTab[i + 1])
  48.  
  49.  
  50. f, axes = plt.subplots(3, sharex='all')
  51.  
  52. axes[0].plot(tTab, yTab, label='y', color='orange')
  53. axes[0].plot(tTab, np.ones(tTab.shape)*y_target, label='y target', color='red')
  54. axes[1].plot(tTab, eTab, label='error')
  55. axes[2].plot(tTab, uTab, label='U')
  56.  
  57. for ax in axes:
  58. ax.grid(True)
  59. ax.legend(loc='upper right')
  60. ax.set_xlabel('Czas')
  61. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement