Search code examples
pythongekko

Data arrays must have the same length, and match time discretization in dynamic problems


I am working on a variation of the Luss problem. I wrote out this code and it seems right, I am looking to minimize the integral and set the endpoints to their final conditions. Unfortunately, the variable p is causing this error and I need it to help my solution converge. p and m.time are the same size but I still get the error. Any help would be appreciated, thanks in advance

import numpy as np

from gekko import GEKKO
import matplotlib.pyplot as plt

m = GEKKO()
#n = 1000
m.time = np.linspace(0,1.5)

x1 = m.Var(value=0.5)
x2 = m.Var(value=0.0)

u = m.MV(lb=-1,ub=1)
u.STATUS = 1

p = np.zeros_like(m.time)
p[-1] = 1
final = m.Param(p)

m.Equation(x1.dt() == u + x2)
m.Equation(x2.dt() == -u)
m.Equation(final*x1 == 0)
m.Equation(final*x2 == 0)


m.Minimize(0.5*m.integral(x1**2) * final)
m.IMODE = 6
m.solve(disp=False)

plt.plot(m.time,x1.value,label = "x1")
plt.plot(m.time,x2.value,label = "x2")
plt.plot(m.time,u.value,label = "u")
plt.legend()
plt.show()

Solution

  • So the problem lies within the IMODE declaration, the command should have been m.options.IMODE = 6, then the problem works fine