daily pastebin goal
23%
SHARE
TWEET

Untitled

a guest Mar 26th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import random
  4.  
  5. a = 1.5
  6. u = 85
  7. e_upper_value = 4
  8. e_lower_value = -2
  9. y_min = 10
  10. y_set = 22
  11. y_max = 70
  12.  
  13. t0 = 0
  14. t_max = 150
  15. h = 0.01
  16.  
  17. t = np.arange(0, t_max, h)
  18. y = np.zeros(len(t))
  19. y[0] = 22
  20. u_results = np.zeros(len(t))
  21. e_results = np.zeros(len(t))
  22. e = y_set - y[0]
  23.  
  24. for i in range(0, len(t)-1):
  25.     e = y_set - y[i]
  26.     if e > e_upper_value:
  27.         u = 85
  28.     else:
  29.         if e < e_lower_value:
  30.             u = 0
  31.  
  32.     y[i+1] = y[i] + h*(a*y[i]+u)
  33.     u_results[i] = u
  34.     e_results[i] = e
  35.  
  36.  
  37. def bez_histerezy():
  38.     t = np.arange(0, t_max, h)
  39.     y = np.zeros(len(t))
  40.     y[0] = 0
  41.     u_results = np.zeros(len(t))
  42.     e_results = np.zeros(len(t))
  43.     e = y_set - y[0]
  44.  
  45.     for i in range(0, len(t)-1):
  46.         e = y_set - y[i]
  47.         if e > 0:
  48.             u = y_max
  49.         else:
  50.             u = y_min
  51.  
  52.         if y[i] + h*(u-y[i])/a < y_min:
  53.             y[i+1] = y_min
  54.         elif y[i] + h*(u-y[i])/a > y_max:
  55.             y[i+1] = y_max
  56.         else:
  57.             y[i+1] = y[i] + h*(u-y[i])/a
  58.         u_results[i] = u
  59.         e_results[i] = e
  60.     return y
  61.  
  62. def dwupolozeniowy_z(d):
  63.     t = np.arange(0, t_max, h)
  64.     y = np.zeros(len(t))
  65.     y[0] = 0
  66.     u_results = np.zeros(len(t))
  67.     e_results = np.zeros(len(t))
  68.     e = y_set - y[0]
  69.  
  70.     for i in range(0, len(t)-1):
  71.         e = y_set - y[i]
  72.         if e > d:
  73.             u = y_max
  74.         elif e < -d:
  75.             u = y_min
  76.  
  77.         if y[i] + h*(u-y[i])/a < y_min:
  78.             y[i+1] = y_min
  79.         elif y[i] + h*(u-y[i])/a > y_max:
  80.             y[i+1] = y_max
  81.         else:
  82.             y[i+1] = y[i] + h*(u-y[i])/a
  83.         u_results[i] = u
  84.         e_results[i] = e
  85.     return y
  86.  
  87. def trojpolozeniowy_bez(M):
  88.     t = np.arange(0, t_max, h)
  89.     y = np.zeros(len(t))
  90.     y[0] = 0
  91.     u_results = np.zeros(len(t))
  92.     e_results = np.zeros(len(t))
  93.     e = y_set - y[0]
  94.  
  95.     for i in range(0, len(t)-1):
  96.         e = y_set - y[i]
  97.         if e >= M:
  98.             u = y_max
  99.         elif e < -M:
  100.             u = y_min
  101.         else:
  102.             u = y_set
  103.  
  104.         if y[i] + h*(u-y[i])/a < y_min:
  105.             y[i+1] = y_min
  106.         elif y[i] + h*(u-y[i])/a > y_max:
  107.             y[i+1] = y_max
  108.         else:
  109.             y[i+1] = y[i] + h*(u-y[i])/a
  110.         u_results[i] = u
  111.         e_results[i] = e
  112.     return y
  113.  
  114. def trojpolozeniowy_z(M, d):
  115.     t = np.arange(0, t_max, h)
  116.     y = np.zeros(len(t))
  117.     y[0] = 0
  118.     u_results = np.zeros(len(t))
  119.     e_results = np.zeros(len(t))
  120.     e = y_set - y[0]
  121.  
  122.     for i in range(0, len(t)-1):
  123.         e = y_set - y[i]
  124.         if e > d:
  125.             if e >= M:
  126.                 u = y_max
  127.             else:
  128.                 u = y_set
  129.         elif e < -d:
  130.             if e < -M:
  131.                 u = y_set
  132.             else:
  133.                 u = y_min
  134.  
  135.         if y[i] + h*(u-y[i])/a < y_min:
  136.             y[i+1] = y_min
  137.         elif y[i] + h*(u-y[i])/a > y_max:
  138.             y[i+1] = y_max
  139.         else:
  140.             y[i+1] = y[i] + h*(u-y[i])/a
  141.         u_results[i] = u
  142.         e_results[i] = e
  143.     return y
  144.  
  145.  
  146. if __name__ == '__main__':
  147.     #bez_histerezy(t)
  148.     plt.subplot(411)
  149.     plt.plot(t, bez_histerezy())
  150.     plt.xlabel('t')
  151.     plt.ylabel('y(t)')
  152.     plt.plot(t, np.full(t.shape, [y_set]))
  153.     plt.xlim(0, 10)
  154.     plt.ylim(0, 30)
  155.  
  156.     plt.subplot(412)
  157.     plt.plot(t, dwupolozeniowy_z(5))
  158.     plt.xlabel('t')
  159.     plt.ylabel('y(t)')
  160.     plt.plot(t, np.full(t.shape, [y_set]))
  161.     plt.xlim(0, 10)
  162.     plt.ylim(0, 30)
  163.  
  164.     plt.subplot(413)
  165.     plt.plot(t, trojpolozeniowy_bez(0.2))
  166.     plt.xlabel('t')
  167.     plt.ylabel('y(t)')
  168.     plt.plot(t, np.full(t.shape, [y_set]))
  169.     plt.xlim(0, 150)
  170.     plt.ylim(0, 30)
  171.  
  172.     plt.subplot(414)
  173.     plt.plot(t, trojpolozeniowy_z(0.2, 0.3))
  174.     plt.xlabel('t')
  175.     plt.ylabel('y(t)')
  176.     plt.plot(t, np.full(t.shape, [y_set]))
  177.     plt.xlim(0, 10)
  178.     plt.ylim(0, 40)
  179.  
  180.     # plt.plot(t, bez_histerezy())
  181.     # plt.xlabel('t')
  182.     # plt.ylabel('y(t)')
  183.     # plt.plot(t, np.full(t.shape, [y_set]))
  184.     # plt.xlim(0, 10)
  185.     # plt.ylim(10, 70)
  186.     #
  187.     # #e(t)
  188.     # plt.subplot(312)
  189.     # plt.plot(t, e_results)
  190.     # plt.xlabel('t')
  191.     # plt.ylabel('e(t)')
  192.     # plt.xlim(2, 4)
  193.     # plt.ylim(-5, 20)
  194.     #
  195.     # #u(t)
  196.     # plt.subplot(313)
  197.     # plt.plot(t, u_results)
  198.     # plt.xlabel('t')
  199.     # plt.ylabel('u(t)')
  200.     # plt.xlim(2, 4)
  201.     # plt.ylim(0, 90)
  202.     # y_t.plot(t[220:350], y[220:350])
  203.     # y_t.plot(t[220:350], np.full([130], y_set), 'r')
  204.     # y_t.plot(t[220:350], np.full([130], y_set-e_lower_value), 'r--')
  205.     # y_t.plot(t[220:350], np.full([130], y_set-e_upper_value), 'r--')
  206.     # y_t.set(xlabel = 't', ylabel = 'y(t)')
  207.     # e_t = figure.add_subplot(312)
  208.     # e_t.plot(t[220:350], e_results[220:350])
  209.     # e_t.set(xlabel = 't', ylabel = 'e(t)')
  210.     # u_t = figure.add_subplot(313)
  211.     # u_t.plot(t[220:350], u_resutls[220:350], 'r.')
  212.     # u_t.set(xlabel = 't', ylabel = 'u(t)')
  213.     plt.show()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top