import numpy as np import matplotlib.pyplot as plt from gekko import GEKKO m = GEKKO() nt = 101 m.time = np.linspace(0,12,nt) # Parameters u = m.MV(value=1,ub=1,lb=0) u.STATUS = 1 u.DCOST = 0 # Variables x1 = m.Var(value=1) x2 = m.Var(value=0) p = np.zeros(nt) p[-1] = 1.0 final = m.Param(value=p) # Equations m.Equation(x1.dt()==u*(10*x2-x1)) m.Equation(x2.dt()==-u*(10*x2-x1)-(1-u)*x2) # Objective Function m.Obj(-final*(1-x1-x2)) m.options.IMODE = 6 m.solve() print('Objective: ' + str(1-x1[-1]-x2[-1])) plt.figure(1) plt.subplot(2,1,1) plt.plot(m.time,x1.value,'k:',lw=2,label=r'$x_1$') plt.plot(m.time,x2.value,'b-',lw=2,label=r'$x_2$') plt.ylabel('Value') plt.legend(loc='best') plt.subplot(2,1,2) plt.plot(m.time,u.value,'r-',lw=2,label=r'$u$') plt.legend(loc='best') plt.xlabel('Time') plt.ylabel('Value') plt.show()