Is there any way to model a variable which is an average value of the previous time step (t-1)'s value and the current time step (t)'s value (e.g., eq1) x(t) = (some equation), eq2) a(t) = (x(t-1) + x(t))/2
) in IMODE=6
in Gekko?
The simplified code below shows what I asked above (The code itself doesn't (physically) make sense, but the code is simplified from the original to ask the question in a more succinct manner).
import numpy as np
from gekko import GEKKO
# Create GEKKO model
m = GEKKO(remote=False)
print(m.path)
m.time = np.linspace(0,5,6)
# Variables
T_air_set_prev = m.Var(value=10, name="T_air_set_prev") # previous -> previous time step (t-1)
T_m0 = m.Var(value=15, name="T_m0")
T_m_ac_t = m.Var(value=25, name="T_m_ac_t") # t -> current time step (t)
T_m_ac = m.Var(value=25, name="T_m_ac")
Q_heating = m.Var(value=0, name="Q_heating")
actuator = m.MV(value=6, lb=3, ub=9, name="actuator")
actuator.STATUS = 1
actuator.DMAX = 1.5
# Equations
m.Equations([T_m0 == 0.5*T_air_set_prev + 1,\
T_m_ac_t == 0.7* T_m0 + 10,\
T_m_ac == (T_air_set_prev + T_m_ac_t)/2,\
Q_heating == 5*T_m_ac+20])
# This line is meant for saving the previous time step's value, but I don't know the correct way to implement this.
T_air_set_prev = m.Intermediate(T_m_ac_t)
m.Obj(0.5*Q_heating + actuator)
# Solve
m.options.IMODE = 6
m.options.DIAGLEVEL = 4
m.options.MAX_ITER = 1000
m.options.SOLVER = 3
m.solve(disp=True)