1 | function dplti_1 |
---|
2 | |
---|
3 | % Data |
---|
4 | A = [2 -1;1 0];nx = 2; |
---|
5 | B = [1;0];nu = 1; |
---|
6 | C = [0.5 0.5]; |
---|
7 | |
---|
8 | % Prediction horizon |
---|
9 | N = 3; |
---|
10 | |
---|
11 | % States x(k), ..., x(k+N) |
---|
12 | x = sdpvar(repmat(nx,1,N),repmat(1,1,N)); |
---|
13 | |
---|
14 | % Inputs u(k), ..., u(k+N) (last one not used) |
---|
15 | u = sdpvar(repmat(nu,1,N),repmat(1,1,N)); |
---|
16 | |
---|
17 | % Value functions |
---|
18 | J = cell(1,N); |
---|
19 | |
---|
20 | % Initialize value function at stage N |
---|
21 | J{N} = 0; |
---|
22 | U{N} = 0; |
---|
23 | k=N-1; |
---|
24 | |
---|
25 | for k = N-1:-1:1 |
---|
26 | |
---|
27 | % Feasible region |
---|
28 | bounds(x{k},-5,5); |
---|
29 | bounds(u{k},-1,1); |
---|
30 | bounds(x{k+1},-5,5); |
---|
31 | |
---|
32 | F = set(-1 < u{k} < 1); |
---|
33 | F = F + set(-1 < C*x{k} < 1); |
---|
34 | F = F + set(-5 < x{k} < 5); |
---|
35 | F = F + set(-1 < C*x{k+1} < 1); |
---|
36 | F = F + set(-5 < x{k+1} < 5); |
---|
37 | |
---|
38 | % LTI Dynamics |
---|
39 | F = F + set(x{k+1} == A*x{k}+B*u{k}); |
---|
40 | |
---|
41 | obj = norm([x{k};u{k}],1); |
---|
42 | |
---|
43 | % Compute value function for one step backwards |
---|
44 | [mpsol{k},sol{k},Uz{k},J{k}] = solvemp(F,obj + J{k+1},[],x{k},u{k}); |
---|
45 | end |
---|
46 | |
---|
47 | % MPT implementation |
---|
48 | sysStruct.A= A; |
---|
49 | sysStruct.B= B; |
---|
50 | sysStruct.C= C; |
---|
51 | sysStruct.D= [0]; |
---|
52 | |
---|
53 | %set constraints on output |
---|
54 | sysStruct.ymin = -1; |
---|
55 | sysStruct.ymax = 1; |
---|
56 | |
---|
57 | %set constraints on input |
---|
58 | sysStruct.umin = -1; |
---|
59 | sysStruct.umax = 1; |
---|
60 | |
---|
61 | sysStruct.xmin = [-5;-5]; |
---|
62 | sysStruct.xmax = [5;5]; |
---|
63 | |
---|
64 | probStruct.norm=1; |
---|
65 | probStruct.Q=eye(2); |
---|
66 | probStruct.R=1; |
---|
67 | probStruct.N=N-1; |
---|
68 | probStruct.P_N=zeros(2); |
---|
69 | probStruct.subopt_lev=0; |
---|
70 | probStruct.y0bounds=1; |
---|
71 | probStruct.Tconstraint=0; |
---|
72 | ctrl=mpt_control(sysStruct,probStruct) |
---|
73 | |
---|
74 | mbg_asserttolequal(mpt_isPWAbigger(mpsol{1}{1},ctrl),0); |
---|