from gekko import GEKKO import numpy as np # True system: dx/dt = -k_true * x, with k_true = 0.4 k_true = 0.4 t_points = np.linspace(0, 5, 51) x_true = np.exp(-k_true * t_points) x_meas = x_true[-1] # measurement at t=5 # GEKKO model for parameter estimation (MHE) m = GEKKO(remote=False) m.time = np.linspace(0, 5, 51) k = m.FV(value=1.0, lb=0, ub=2) k.STATUS = 1 # allow optimization to adjust k x = m.Var(value=1.0) m.Equation(x.dt() == -k * x) # Define controlled variable for measurements y = m.CV(value=np.nan * np.ones(len(m.time))) y.FSTATUS = 0 y.MEAS = np.nan * np.ones(len(m.time)) y.FSTATUS[-1] = 1 # provide measurement feedback at final time y.MEAS[-1] = x_meas m.Equation(y == x) m.options.IMODE = 5 # MHE mode m.options.EV_TYPE = 1 # L2 norm in objective m.solve(disp=False) print("Estimated k:", k.value[0]) print("Predicted x(5):", x.value[-1], "Measured x(5):", x_meas)