Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import scipy.optimize as scp
  4.  
  5.  
  6.  
  7. def myFunc(K):
  8.     T = 30
  9.     h = 0.01
  10.     y = 0
  11.     x = 1
  12.     xThres = 0
  13.     error=0
  14.  
  15.     a = 0.7
  16.     b = 0.7
  17.     c = 3
  18.     z = -0.4
  19.     columns = (T // h) + 1
  20.     table = np.zeros(shape=(3, int(columns)))
  21.     tableU = np.zeros(shape=(2, int(columns)))
  22.     tableE = np.zeros(shape=(2, int(columns)))
  23.     table[0][0] = y
  24.     table[1][0] = x
  25.     tableE[0][0] = xThres - x
  26.     e_sum = tableE[0, 0] * h
  27.     tableU[0, 0] = K[0] * tableE[0, 0] + K[1] * e_sum
  28.     if tableU[0, 0] > 2:
  29.         tableU[0, 0] = 2
  30.     elif tableU[0, 0] < -2:
  31.         tableU[0, 0] = -2
  32.     nextX = x
  33.     nextY = y
  34.  
  35.     for j in range(1, int(columns)):
  36.         table[0, j] = nextY + (-(1 / c) * (nextX - a + b * nextY)) * h
  37.         nextY = table[0, j]
  38.         table[1, j] = nextX + (c * (nextY - ((nextX * nextX * nextX) / 3) + nextX + z) + tableU[0, j - 1]) * h
  39.         nextX = table[1, j]
  40.         table[2, j] = table[2, j - 1] + h
  41.         e_pop = tableE[0, j - 1]
  42.         tableE[0, j] = xThres - nextX
  43.         e_sum += tableE[0, j] * h
  44.         tableU[0, j] = K[0] * tableE[0, j] + K[1] * e_sum + K[2] * ((-e_pop + tableE[0, j]) / h)
  45.         if tableU[0, j] > 2:
  46.             tableU[0, j] = 2
  47.         elif tableU[0, j] < -2:
  48.             tableU[0, j] = -2
  49.         tableU[1, j] = tableU[1, j - 1] + h
  50.         tableE[1, j] = tableE[1, j - 1] + h
  51.         error += abs(tableE[0, j]) * tableE[1, j]
  52.     return error
  53.  
  54.  
  55. print(scp.fmin_bfgs(myFunc,[1,1,1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement