import numpy as np from random import random from gekko import GEKKO import matplotlib.pyplot as plt # initialize GEKKO model m = GEKKO() # time m.time = np.linspace(0,20,41) # constants mass = 500 # Parameters b = m.Param(value=50) K = m.Param(value=0.8) # Manipulated variable p = m.MV(value=0, lb=0, ub=100) # Controlled Variable v = m.CV(value=0,name='v') # Equations m.Equation(mass*v.dt() == -v*b + K*b*p) m.options.IMODE = 6 # control # MV tuning p.STATUS = 1 #allow optimizer to change p.DCOST = 0.1 #smooth out MV p.DMAX = 20 #slow down change of MV # CV tuning v.STATUS = 1 #add the CV to the objective m.options.CV_TYPE = 1 #Dead-band v.SPHI = 42 #set point v.SPLO = 38 #set point v.TR_INIT = 1 #setpoint trajectory v.TAU = 5 #time constant of setpoint trajectory # Solve m.solve() # get additional solution information import json with open(m.path+'//results.json') as f: results = json.load(f) # Plot solution plt.figure() plt.subplot(2,1,1) plt.plot(m.time,p.value,'b-',lw=2,label='MV') plt.legend(loc='best') plt.ylabel('gas') plt.subplot(2,1,2) plt.plot(m.time,results['v.tr_lo'],'k-',label='SPHI') plt.plot(m.time,results['v.tr_hi'],'k-',label='SPLO') plt.plot(m.time,v.value,'r--',lw=2,label='CV') plt.legend(loc='best') plt.ylabel('velocity') plt.xlabel('time') plt.show()