import numpy as np import matplotlib.pyplot as plt from gekko import GEKKO m = GEKKO() nt = 101 m.time = np.linspace(0,1,nt) # Variables x1 = m.Var(value=1) x2 = m.Var(value=0) u = m.Var(value=-0.48) p = np.zeros(nt) p[-1] = 1.0 final = m.Param(value=p) # Equations m.Equation(x1.dt()==u) m.Equation(x2.dt()==x1**2 + u**2) m.Equation(final*(x1-1)==0) # Objective Function m.Obj(x2*final) m.options.IMODE = 6 m.solve() plt.figure(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.plot(m.time,u.value,'r--',lw=2,label=r'$u$') plt.legend(loc='best') plt.xlabel('Time') plt.ylabel('Value') plt.show()