N_SE = 150; % estiamtion window size
% dimension
nu_SE = 1;
nw_SE = 1; % dimension of disturabances
nx_SE = 6; % dimension of states
% The model will be composed of two variables. The first is a binary variable which
% controls if a disturbance is taken at time k, and the second variable is a continuous
% variable representing the disturbance amount taken at time k.
onoff = binvar(N_SE, nw_SE);
w_SE = sdpvar(N_SE,nw_SE); % Disturbance w(0),...,w(N_SE - 1)
% variables, now the decision variables are the states and the disturbances
x_SE = sdpvar(nx_SE,1); % Simulated initial States x(0), this is a decision variable
u_SE = sdpvar(nu_SE, N_SE); % insulin input u(0),...,u(N_SE - 1). here they are known values.
x_hat_SE = sdpvar(nx_SE,1); % Estimated initial states x_hat(0)
y_SE = sdpvar( 1, N_SE); % measurements y(0),...,y(N_SE - 1)
% system dynamics
E = [1 0 0 0 0 0];%times xk to get the first parameter
Y_SE = [];% Y: system output % simulated output H(x(t))
V_SE = [];% V: discrepency between measurements and simulated outputs
xk_SE = x_SE;
for k = 1:N_SE
xk_SE = A*xk_SE + b + C*u_SE(k) + D*w_SE(k);
Y_SE = [Y_SE; E*xk_SE];
V_SE = [V_SE; y_SE(k) - E*xk_SE];
end
% The amount of meals are either between the ub and lb (when onoff key is on)
% , or being 0 (when onoff key is off).
upperbound = sdpvar(N_SE,nw_SE);
lowerbound = sdpvar(N_SE,nw_SE);
Constraints =[];
for k = 1:N_SE
Constraints = [Constraints, onoff(k)*lowerbound(k) <= w_SE(k)<= onoff(k)*upperbound(k)];
end
% Constraints that limit the number of disturbance in a window
limit = 3;
indicator = zeros(N_SE-1,1);
for k = 2:N_SE
% indicator will be 1 only when switched on
indicator(k-1) = onoff(k)-onoff(k-1);
end
% At most limit number of 1's
Constraints = [Constraints, nnz(indicator == 1) <= meal_limit, nnz(indicator == 1) == nnz(indicator == -1) ];
% disturbances are square, so all the values between a (1, -1) pair are the same
meal_number = nnz(indicator == 1);
[s, location] = sort(indicator);
if meal_number > 0
for k = 1:meal_number
range = location(N_SE - meal_number -1 + k):location(k)-1;
Constraints = [Constraints, w_SE(range) == w_SE(range(1))];
end
end
objective = norm(x_SE - x_hat_SE, 1) + norm(V_SE, 1);
ops = sdpsettings('verbose',2,'debug',1);
% use optimizer instead of optimize
estimator = optimizer(Constraints, objective, ops, {u_SE, y_SE, x_hat_SE,upperbound,lowerbound}, {xk_SE, w_SE, onoff,objective} );
My assumption is in a moving horizon estimation window, there could be a limited number of square-wave disturbances. However, I keep getting 0.5 for all onoff values? What is the problem of my model? How can I fix it with the assumption? And what is the best solver for this?
Thank you very much!