from gekko import GEKKO import numpy as np import matplotlib.pyplot as plt m = GEKKO() # initialize GEKKO nt = 501 m.time = np.linspace(0,1,nt) # Variables x1 = m.Var(value=np.pi/2.0) x2 = m.Var(value=4.0) x3 = m.Var(value=0.0) p = np.zeros(nt) # final time = 1 p[-1] = 1.0 final = m.Param(value=p) # optimize final time tf = m.FV(value=1.0,lb=0.1,ub=100.0) tf.STATUS = 1 # control changes every time period u = m.MV(value=0,lb=-2,ub=2) u.STATUS = 1 m.Equation(x1.dt()==u*tf) m.Equation(x2.dt()==m.cos(x1)*tf) m.Equation(x3.dt()==m.sin(x1)*tf) m.Equation(x2*final<=0) m.Equation(x3*final<=0) m.Obj(tf) m.options.IMODE = 6 m.solve(disp=False) print('Final Time: ' + str(tf.value[0])) tm = np.linspace(0,tf.value[0],nt) plt.figure(1) plt.plot(tm,x1.value,'k-',label=r'$x_1$') plt.plot(tm,x2.value,'b-',label=r'$x_2$') plt.plot(tm,x3.value,'g--',label=r'$x_3$') plt.plot(tm,u.value,'r--',label=r'$u$') plt.legend(loc='best') plt.xlabel('Time') plt.show()