-
Notifications
You must be signed in to change notification settings - Fork 0
/
solveMPC.m
43 lines (29 loc) · 933 Bytes
/
solveMPC.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function [ x, u ] = solveMPC(CSTR_linearplant, t, x_t, u_t, N, X0, U0, UBounds)
% Clear Yalmip Variables
yalmip('clear')
n = size(X0,1);
m = size(U0,1);
x = sdpvar(n*ones(1,N+1),ones(1,N+1));
u = sdpvar(m*ones(1,N),ones(1,N));
% Initial Condition
Constraints = [x_t == x{1}];
for i = 1:N
Uset = constraints(u_t, UBounds);
Hu = Uset.A; bu = Uset.b;
Constraints = [Constraints;
x{i+1} == CSTR_linearplant( t + i,x{i},u{i});
Hu * u{i} <= bu;];
end
% Running Cost
Cost = 0;
for i = 1:N
Cost = Cost + runningcost( t + i, x{i}, u{i});
end
% Solver setting
options = sdpsettings('solver','mosek','verbose',0);
% Solve the FTOCP
Problem = optimize(Constraints, Cost, options);
Objective = double(Cost);
cost = value(Cost);
% yalmip('clear')
end