Professional Documents
Culture Documents
Summary
This thesis deals with linear Model Predictive Control, MPC, with the goal of
making a controller for an articial pancreas. A diabetic is simulated by a mathematical model, and based on this model the MPC will compute the optimal
insulin input, taking constraints, disturbances and noise into account. Below is
a brief description of each chapter.
Chapter 2 describes linearization of dierential equations in continuous time.
These are converted into a linear state-space model with discrete time representation, which is a requirement for linear MPC.
In Chapter 3, the basic idea for MPC is reviewed. By starting of with MPC on
basic form, the control model is extended step by step. Constraints are imposed
on the input and on the input rate of movement, which makes sure the input
appears in a controlled volume and speed.
Soft constraints are used for the output, to ensure the output are held inside
the wanted boundaries, but, if needed, the boundaries can be violated. In some
cases it would be impossible to stay within the constraints, and this would make
the problem infeasible, if soft constraints wasn't used.
Feedforward and feedback is also described.
the system more robust, and gives a better reaction speed on changes in the
process, such as disturbances and noise.
Chapter 4 is the implementation of the MPC problem in
divided into three dierent phases:
ATLAB. This is
The
This includes
properties such as constraints and meal disturbances. After all the needed parameters are specied, the MPC controller is designed, and the simulation is
completed. Finally the simulation is evalutated by various plots.
This implementation is used on a modied version of Bergmans minimal model
in Chapter 5. This model consists of ve dierential equations, which simulates
a type 1 diabetic patient. The knowledge from the previous chapters is used to
transform the model into a linear state space model with discrete time representation, and then use this with MPC. This chapter also discuss the selection
ii
of noise and weight matrices. Furthermore simulations are done, to nally test
the functionality of the controller.
The thesis concludes that, it is possible that MPC can be used for the purpose
of an insulin pump, but severe testing, and a better model would be needed.
Resum
Formlet med dette projekt er at undersge om Model Predictive Control, MPC,
kan bruges som kontrolanordning til en insulinpumpe, hvilket kan blive brugt
til at udvikle en kunstig bugspytkirtel til mennesker som ikke producerer insulin
selv.
De basale ider for liner MPC vil blive gennemget og implementeret, og
anvendt p en matematisk model for en type 1 diabetiker.
Dette inkluderer
feedforward og feedback.
den hndterer de este scnearier godt, dog skal det haves i mente at den ktive
patient er baseret p en noget mangelfuld model.
iv
Contents
Summary
Resum
iii
Introduction
Linearization
2.1
. . . . . . . . . . . . . . .
2.2
Summary
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.1
. . . . . . . . . . . . . . . . . . . . . . . . .
3.2
Unconstrained MPC
3.1.1
Regularization
. . . . . . . . . . . . . . . . . . . . . . . .
12
3.1.2
Disturbance . . . . . . . . . . . . . . . . . . . . . . . . . .
14
3.1.3
Feed-forward/feedback . . . . . . . . . . . . . . . . . . . .
17
Constrained MPC
. . . . . . . . . . . . . . . . . . . . . . . . . .
19
3.2.1
Input constraints . . . . . . . . . . . . . . . . . . . . . . .
19
3.2.2
20
3.2.3
Output constraints . . . . . . . . . . . . . . . . . . . . . .
22
vi
CONTENTS
3.2.4
. . . . . . . . . . . . . . . . . . .
23
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
26
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
3.3
Kalman lter
3.4
Summary
Implementation
29
4.1
ScenaX.m
4.2
MPCControl.m .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.2.1
MPCDesign.m
4.2.2
MPCSimulate.m
4.2.3
MPCPlot
. . . . . . . . . . . . . . . . . . . . . . . . .
30
30
30
. . . . . . . . . . . . . . . . . . . . . . . .
31
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
34
35
5.1
Linearization
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
38
5.2
MPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
39
5.2.1
Weight matrices
39
5.2.2
5.2.3
Noise
5.3
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
42
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
43
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
44
5.3.1
Scenario I . . . . . . . . . . . . . . . . . . . . . . . . . . .
44
5.3.2
Scenario II
. . . . . . . . . . . . . . . . . . . . . . . . . .
54
5.3.3
Scenario III . . . . . . . . . . . . . . . . . . . . . . . . . .
56
Simulations
Conclusion
59
CONTENTS
vii
A Impulse-response method
61
B Matlab programs
63
B.1
Scena1.m
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
63
B.2
Scena2.m
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
64
B.3
Scena3.m
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
65
B.4
MPCControl.m . . . . . . . . . . . . . . . . . . . . . . . . . . . .
66
B.5
MPCDesign.m
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
66
B.6
DesignKalman.m . . . . . . . . . . . . . . . . . . . . . . . . . . .
68
B.7
DesignDiscreteMatrices.m . . . . . . . . . . . . . . . . . . . . . .
68
B.8
DesignParameters.m
. . . . . . . . . . . . . . . . . . . . . . . . .
69
B.9
DesignConstraints.m . . . . . . . . . . . . . . . . . . . . . . . . .
69
B.10 DesignMPCMatrices.m . . . . . . . . . . . . . . . . . . . . . . . .
70
B.11 MPCSimulate.m
. . . . . . . . . . . . . . . . . . . . . . . . . . .
72
B.12 MPCCompute.m
. . . . . . . . . . . . . . . . . . . . . . . . . . .
73
B.13 BergmanMinimalModel.m . . . . . . . . . . . . . . . . . . . . . .
74
B.14 MPCPlot.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
75
B.15 InvestSampling.m . . . . . . . . . . . . . . . . . . . . . . . . . . .
77
B.16 InvestWeights.m
78
Bibliography
. . . . . . . . . . . . . . . . . . . . . . . . . . .
81
viii
CONTENTS
Chapter
1
Introduction
WHO has estimated that this number will be 366 mio. in 2030, i.e. diabetes is
a rising problem.
In the USA alone, there is used 130 billion USD a year on diabetes, which is
10% of the health care budget.
There are two types of diabetes, type 1 and type 2. Type 1 is called the insulin
dependent diabetes and occur when the cells
in the pancreas, which producess insulin, are
destroyed.
The human body needs insulin to move glucose from the food into cells,
throughout
Therefore peo-
1.1:
The
diabetes-
the veins.
glucose-regulation
These injections are done with a needle several times at day, with the correct proportioning of insulin for the individual.
insulin he can go into hypoglycemia, and too little can cause hyperglycemia.
Hyperglycemia can lead to blindness, kidney failure and other long terms complication, while hypoglycemia can lead to loss of consciousness and coma.
device which could automate the injection of insulin would obviously be a big
advantage for the patient.
Introduction
A comparatively new device for type 1 diabetes is an insulin pump, which makes
the injection for the diseased person.
The pump consists essentially of three components: A sensor, which measures
the blood glucose concentration in the body, a controller that estimates the
needed insulin quantity, and a pump which makes the injection. See Figure 1.2.
This project will deal with a control algorithm for an insulin pump, for which
linear Model Predictive Control will be used.
MPC is a controller build on a model for the specic case.
The controller
Chapter
2
Linearization
This chapter will show how to convert a model consisting of rst order dierential equations, into a linear model with discrete time representation, and thereby
making it possible to use this model with linear MPC.
Such a system of dierential equations in continuous time is denoted by;
dx(t)
dt
= x(t)
(2.1)
f (xs , us , ds ) = 0.
(xs , us , ds ),
x(t)
,
A=
x (xs ,us ,ds )
x(t)
B=
,
u (xs ,us ,ds )
is transcribed so it containes
x(t)
dx(t)
dt
d (x(t)
(2.3)
xs :
xs )
dt
E=
,
d (xs ,us ,ds )
dx(t)
dt
d xs
dt
(2.4)
Linearization
D(t) = d(t) ds ,
and
gives;
xs )
(u(t) us ) + E
(d(t) ds )
= A (x(t) xs ) + B
dt
dX(t)
(t) + ED(t)
= AX(t)
+ BU
dt
(t) + ED(t)
X(t)
= AX(t)
+ BU
d (x(t)
(2.5)
(s) + ED(s)
ds
eA(ts) BU
(2.6)
t0
Where
xk , uk
and
dk
still be
deviation variables;
xk+1 = e
k+1 tk )
A(t
tk+1
x(tk ) +
+ Ed(s)
eA(tk+1 s) Bu(s)
ds
tk
eATs x(tk ) +
Ts
eA(tk+1 s) Bu(s)
+ Ed(s)
ds
Ts = tk+1 tk
eATs x(tk ) +
Ts
eA Bu(
) + Ed(
)
= tk+1 s
eATs xk +
Ts
eA B
d uk +
Ts
eA E
d dk
A xk + B uk + E dk
where the matrices
A = eATs ,
A, B
and
Z
B=
0
Ts
(2.7)
are;
d,
eA B
Z
E=
0
Ts
d
eA E
(2.8)
Ts
A
0
B
I
A, B
and
E,
E
= eM Ts ,
I
M=
A B
0 0
E
0
(2.9)
Let
and
Then the evolution of the states is governed by the dierence equation, as above;
k,
the values of
are;
x1 = A xs + B us + E ds
x2 = A x1 + B u1 + E d1
= A (A xs + B us + E ds ) + B u1 + E d1
= A2 xs + ABus + AEds + B u1 + E d1
x3 = A x2 + B u2 + E d2
= A A2 xs + ABus + AEds + B u1 + E d1 + B u2 + E d2
= A3 xs + A2 Bus + A2 Eds + ABu1 + AEd1 + Bu2 + Ed2
.
.
.
k1
X
k1
X
Ak1j B uj +
Ak1j E dj
j=0
(2.11)
j=0
k -time
is;
zk = C xk
= C A k xs +
k1
X
k1
X
C Ak1j B uj +
Ak1j E dj
j=0
= C A k xs +
k1
X
j=0
Hkj uj +
j=0
Hk
is the
k 'th
Hk =
Hkj,d dj ,
(2.12)
j=0
Markov parameter.
0,
CAk1 B,
k1
X
k=0
,
k1
Hk
and
Hk,d
Hk,d =
0,
k=0
CAk1 E, k 1
(2.13)
Linearization
xk+1 = A xk + B uk + E dk
zk = C xk
(2.14)
2.2 Summary
2.2 Summary
In this chapter it has been shown how to convert a system of rst order dierential equations in continuous time, into a linear model in discrete time. This
process is summarized below.
Linearization of model,
- Given a system of dierential equations,
x(t)
(xs , us , ds ).
= f
, B
u
= f
E
d
A =
f
x
and
(t) + ED(t)
X(t)
= AX(t)
+ BU
,
A = eATs ,
B=
R Ts
0
d,
eA B
and
E=
xk+1 = A xk + B uk + E dk .,
R Ts
0
d .
eA E
where
x, u and
- The output is
zk = C xk ,
with
Linearization
Chapter
u,
The controller
r,
zk ,
rk ,
as small as possible.
1X
||zk rk ||2Qy
2
k=0
(3.1)
10
Since
z0
1
2
2 ||z0 r0 ||Qz is discarded. This gives the
(3.2)
min z =
s.t.
1
2
N
X
||zk rk ||2Qz
k=1
k = 0, 1, . . . , N 1
k = 0, 1, . . . , N
zk = CAk x0 +
k1
X
Hkj uj +
k1
X
Hkj,d dj
j=0
j=0
zdj
(3.3)
k1
X
Hkj uj
(3.4)
j=0
This can be written as;
z0 = Cx0
CA
H1
z1
H
z CA2
2
z3 = CA3 x0 + H3
.
. .
.
. .
.
. .
CAN
HN
zN
|
| {z } | {z }
Z
(3.5)
0
H1
H2
0
0
H1
.
.
.
.
.
.
..
HN 1
HN 2
{z
Z = x0 + U
R
By introducing
r1
r2
R= .
..
rN
0
0
0
u0
u
1
u2
.
.
.
.
.
.
uN 1
H1
} | {z }
(3.6)
(3.7)
(3.8)
11
z =
1X
1
2
||zk rk ||2Qz = ||Z R||Qz
2
2
(3.9)
k=1
Qz
Qz
0
Qz = .
..
0
Qz
.
.
.
..
is given by;
0
0
.
.
.
(3.10)
Qz
1
2
||Z R||Qz
2
1
2
= ||x0 + U R||Qz
2
1
2
= ||U (R x0 )||Qz
2
1
2
= ||U b||Qz ,
b = R x0
2
z =
(3.11)
(3.12)
(3.13)
(3.14)
1
2
||U b||Qz
2
1
= (U b)T Qz (U b),
by denition
2
1
1
= U T T Qz U (T Qz b)T U + bT Qz b
2
2
1
= U T Hz U + gzT U + z
2
z =
where
Hz , gz
and
of weighted norm
(3.15)
Hz = T Qz
(3.16)
gz = T Qz b
= T Qz (R x0 )
= T Qz x0 T Qz R
= Mx 0 x 0 + MR R
1
z = bT Qz b
2
(3.17)
(3.18)
12
(3.19)
min z =
U
1 T
U Hz U + gzT U
2
Hz = T Qz
gz = Mx0 x0 + MR R
is discarded since it doesn't inuence the solution to the problem. Note that
the gradient is dynamic and needs to be updated for every timestep, as opposed
to the Hessian, which is static.
3.1.1
Regularization
uk = uk uk1 ;
(3.20)
min = z + u =
s.t.
1
2
N
X
||zk rk ||2Qz +
k=1
1
2
N
1
X
||uk ||2S
k=0
k = 0, 1, . . . , N 1
k = 0, 1, . . . , N
This new term minimizes the dierence between two consecutive steps in
which gives more smooth input, ie. tries to ensure that steps in
u,
are either
13
u =
N 1
1 X
||uk ||2S
2
k=0
1
2
1
2
N
1
X
k=0
N
1
X
k=0
u0
u1
u2
2 .
..
uN 1
2S
S
..
..
S
0
T
0
+
. u1
.
.
0
| {z }
..
S
2S
2S
S
{z
HS
u0
u1
u2
u0
u1
u2
.
.
S .
S
uN 1
}
. + 2 u1 Su1
.
.
uN 1
Mu1
This
1
1
T
= U T HS U + (Mu1 u1 ) U + u1 Su1
2
2
shows, that introducing u extends the QP problem
(3.21)
by the following
terms;
Hu = HS
(3.22)
gu = Mu1 u1
(3.23)
1
2 u1 Su1 is discarded, because of the lack of inuence
on the solution to the problem. The new QP problem is;
Like with
z ,
the term
min =
U
1 T
U HU + g T U
2
H = Hz + Hu = T Qz + HS
g = gz + gu = Mx0 x0 + MR R + Mu1 u1
(3.24)
14
3.1.2
Disturbance
Virtually
every system has some disturbance. Figure 3.2 illustrates how the disturbance
The disturbance in this model is relatively easily handled by including the last
term,
zdj ,
(3.25)
min = z + u =
N
N 1
1X
1 X
||zk rk ||2Qz +
||uk ||2S
2
2
k=1
s.t.
k=0
zuj .
Hi
k = 0, 1, . . . , N 1
k = 0, 1, . . . , N
is analogous to
Hi,d ,
so
zdj
must then be
U ,
Z = x0 + U + d D
in
Z.
zdj
yields a
d D ;
(3.26)
15
Where;
H1,d
H
2,d
H
d =
.3,d
.
.
HN,d
0
H1,d
H2,d
0
0
H1,d
0
0
0
.
.
.
.
.
.
..
.
.
.
HN 1,d
HN 2,d
d0
d1
D= .
..
dN 1
H1,d
means, that
(3.27)
has to be rewritten as a
QP;
z =
1X
||zk rk ||2Qz
2
k=1
=
=
=
=
=
=
=
1
||Z R||2Qz
2
1
||U (R x0 d D)||2Qz ,
by using (3.26)
2
1
||U b||2Qz ,
b = R x0 d D
2
1 T T
(U bT )Qz (U b)
2
1
U T T Qz U bT Qz U U T T Qz b + bT Qz b
2
1 T T
1
U Qz U (T Qz b)T U + bT Qz b
2
2
1 T
U Hz U + gzT U + z
(3.28)
2
Where;
Hz = T Qz
(3.29)
gz = Qz b
= T Qz (R x0 d D)
= Mx 0 x 0 + MR R + MD D
1
z = bT Qz b
2
(3.30)
(3.31)
16
min =
U
1 T
U HU + g T U
2
H = T Qz + HS
g = Mx0 x0 + MR R + MD D + Mu1 u1
(3.32)
3.1.3
17
Feed-forward/feedback
y,
r,
back with feedback is slow reaction speed, since output has to be measured
before it can be used.
The feedforward approach is illustrated on Figure 3.4.
Feedforward is a loop
in the system, which takes some known disturbance and forward it to the controller.
18
But meanwhile there is a relatively big chance that the output isn't comparable
to the wanted values, the references,
r,
disturbance.
When combining these two appoaches, the system will be aware of both the
known and the unmeasured disturbance, so the system in theory will be trustworthy and fast. Figur 3.5 illustrates a controller where both these approaches
are used.
19
3.2.1
Input constraints
The input constraints are limitations to the maximum and minimum input volume.
(3.33)
min =
N
N 1
1X
1 X
||zk rk ||2Qz +
||uk ||2S
2
2
k=1
s.t.
k=0
k = 0, 1, . . . , N 1
k = 0, 1, . . . , N
umin uk umax
k = 0, 1, . . . , N 1
umin uk umax
umin
u0
umin u1
. .
.. ..
umin
| {z }
uN 1
min
U
umax
umax
.
..
(3.34)
umax
| {z }
max
U
(3.35)
1 T
U HU + g T U
2
min U U
max
U
min =
U
s.t.
The Hessian and gradient for this problem are the same as in problem (3.32).
20
3.2.2
It is also possible to have constraints on how fast the input constraints can
change.
therefore called
uk .
to
k+1
and is
(3.36)
min =
s.t.
1
2
N
X
||zk rk ||2Qz +
k=1
1
2
N
1
X
||uk ||2S
k=0
k = 0, 1, . . . , N 1
k = 0, 1, . . . , N
umin uk umax ,
umin uk umax ,
k = 0, 1, . . . , N 1
k = 0, 1, . . . , N 1
uk
umin uk umax
umin
u0 u1
umax
umin u1 u0 umax
.
.
.
.
.
..
.
.
umin
uN uN 1
umin + u1
I
umin I I
.
..
.
.
.
umax
..
u0
umax + u1
u1 umax
.
.
.
.
umin
Since the rst row contains
u1
umin
{z }
Umin
I
|
{z
umax
umin + u1 u0 umax + u1
I I
umin
umin
I I
.
..
..
..
.
.
|
uN 1
u1
u2
.
..
(3.37)
umax
umax
.
.
.
uN 1
I
}
umax
{z }
Umax
(3.38)
21
u0
gives;
u0 umax + u1
u0 umax
u0 umin + u1
u0 umin
u0 min(umax , umax + u1 )
u0 max(umin , umin + u1 )
(3.39)
u0
max(umin , umin + u1 )
u1
u
min
.
.
.
..
.
uN 1
umin
{z
min(umax , umax + u1 )
umax
.
.
Umin
umax
{z
Umax
}
(3.40)
min =
U
s.t.
(3.41)
1 T
U HU + g T U
2
Umin U Umax
Umin U Umax
Note that
timestep.
Umin
and
Umax
22
3.2.3
Output constraints
limitations to
min =
s.t.
1
2
N
X
||zk rk ||2Qz +
k=0
1
2
N
1
X
(3.42)
||uk ||2S
k=0
k
k
k
k
k
= 0, 1, . . . , N 1
= 0, 1, . . . , N
= 0, 1, . . . , N 1
= 0, 1, . . . , N 1
= 1, 2, . . . , N
zmin zk zmax
zmin
zmax
z1
zmin z2 zmax
. . .
.. .. ..
zmin
| {z }
Zmin
Using
zN
| {z }
Z
(3.43)
zmax
| {z }
Zmax
Z = x0 + U + d D
Zmin Z Zmax
Zmin x0 + U + d D Zmax
Zmin x0 d D U Zmax x0 d D
|
{z
}
|
{z
}
min
Z
max
Z
(3.44)
23
(3.45)
min =
U
s.t.
3.2.4
1 T
U HU + g T U
2
Umin U Umax
Umin U Umax
Zmin U Zmax
It can occour that the control problem will be infeasible. This is mainly due to
the use of constraints greatly complicating the problem. In some cases it can
simply be impossible to stay within the boundaries.
A solution to this problem is to soften the constraints, meaning the boundaries
can be violated occasionally, if needed. In this case, soft output constraints are
used.
slack variable,
(3.46)
min = z + + u
=
k=1
s.t.
N 1
1 X
1
+
||zk rk ||2Qz + ||k ||2S + sT
||uk ||2S
k
2
2
2
N
X
1
k=0
k
k
k
k
k
k
k
= 0, 1, . . . , N 1
= 0, 1, . . . , N
= 0, 1, . . . , N 1
= 0, 1, . . . , N 1
= 1, 2, . . . , N
= 1, 2, . . . , N
= 1, 2, . . . , N
If it's not neccesary to violate output constraints, the solution to the problem
24
= 0,
is selected as small as
is introduced;
1
2
= .
..
(3.47)
N
The new term in the objective function,
is formulated as QP;
1 T
1
||k ||2S + sT
T
k = S + s
2
2
(3.48)
where;
s
..
s = .
S =
..
(3.49)
S
For this problem the Hessian is
,
U
s .
The QP problem
U
U=
(3.50)
1 T
2 U HU
.
+ gT U
The
= H
H
0
0
,
S
g =
g
s
U and should
Umin
U
Umax
| {z } |{z} | {z }
min
U
(3.51)
(3.52)
max
U
Umin U Umax
Formulated in terms of
yields;
Umin
U
Umax
0
(3.53)
zk
25
needs to be transcribed;
< zk k zmax
< Z Zmax
< x0 + D D + U Zmax ,
using (3.26)
< U Zmax ( x0 + D D)
< U Zmax
U
< I
Zmax
(3.54)
Equivalently, this can be done for the lower bound, which gives;
U
Zmin I
<
(3.55)
0
Umin
Umax
I U Zmax
I
Zmin
| {z } | {z }
| {z }
bmin
(3.56)
bmax
1 T
U H U + gT U
2
min U
U
max
U
bmin AU bmax
min =
U
s.t.
(3.57)
26
Both
wk
and
vk ,
respectively;
(3.58)
yk = Cxk + vk
(3.59)
wk
and
vk
Qw
and
Rv ,
respectively.
wk N (0, Qw )
(3.60)
vk N (0, Rv )
(3.61)
An estimate of
is given by:
x
k+1|k = E {xk+1 |yk , yk1 , . . . , y0 }
= E {xk+1 |Y}
= E {Axk + Buk + wk |Y}
= A E {xk |Y} + Buk + E {wk |Y}
= A
xk|k + Buk + w
k|k
Since the mean of
wk
(3.62)
x
k+1|k = A
xk|k + Buk
This means that
Pk|k ,
xk|k
(3.63)
x
k|k
i.e:
xk|k N x
k|k , Pk|k
(3.64)
k ,
to
(3.65)
k+1 = Ik +
(3.66)
yk = Cxk + vk
(3.67)
xk+1
A
=
k+1
0
y= C
xk
B
w
+
uk + k
k
0
xk
+ vk
0
k
B
I
(3.68)
(3.69)
where
27
Q,k
and
is
N (0, Q ).
x
k+1|k .
First phase of the Kalman lter is the time update. It is used to produce an
estimate of the current state, using the estimate of the previous state;
x
k|k1 = A
xk1|k1 + Buk
(3.70)
Pk|k1 = APk1|k1 A + Qw
(3.71)
The second phase of the lter, the measurement update, renes measurement
information from the current timestep to get a new, more accurate, estimate;
ek = yk C x
k|k1
(3.72)
Sk = CPk|k1 C + Rv
Kk Sk = Pk|k1 C T ,
(3.73)
(optimal Kalman gain, solve for
Kk )
(3.74)
x
k|k = x
k|k1 + Kk ek
(3.75)
Pk|k = (I Kk C) Pk|k1
(3.76)
28
3.4 Summary
This chapter derived the MPC problem (3.46) shown on Figure 3.7.
r,
time-step.
output,
bances,
z,
y.
d.
u,
for each
The described MPC algorithm also takes care of input constraints, constraints
on input rate of movement, and soft output constraints. The soft output constraints keeps us from ending up with an infeasible problem, but can also violate
the physical limitations for the output, which may cause the system to malfunction and should therefore be used with concern.
Also the use of the Kalman Filter is described. The lter makes an estimate of
x,
process.
Formulating the control problem as a QP problem makes it relatively easy solvable.
Chapter
4
Implementation
This chapter will describe the implementation of the MPC problem (3.46) in
ATLAB.
The individual
typewriter font
nominates
ATLAB entries.
Names in
30
Implementation
4.1 ScenaX.m
SceneX.m
The
the scene. The idea is that, all parameters the user should have inuence on,
can be adjusted in this le. Table 4.1 shows the basic variables which this le
initializes.
Parameters(s)
N
umin , umax
umin , umax
zmin , zmax
Q, S , S , s
ATLAB name(s)
umin, umax
udmin, udmax
zmin, zmax
Q, S, Spsi, spsi
Describtion
Size of prediction horizon.
Bounds on
u.
u.
z.
Weight matrices.
SceneX.m
The timevector
t,
A vector
Ts,
tf.
t0,
sigmaw and sigmav, which is used to scale the simulated noise, and the
seed parameter, which denes what set of random numbers to be used.
feed
details later.
4.2 MPCControl.m
This is a wrapper script, which simply calls the three scripts needed for the
dierent phases of MPC: Design, simulation and evaluation.
4.2.1
MPCDesign.m
In this design le, everything is prepared for the actual simulation. This mainly
consists of setting up the many matrices found in Chapter 3.
4.2
MPCControl.m
31
ParameterDesign.m
Parameters(s)
xs , us , ds , zs
x0 , u0
R, D
x, u, y , U
A, B , C , D
ATLAB name(s)
big
xs, us, ds, zs
x0, u0
R, D
x, u, y, ubar
Ac, Bc, Cc, Ec
Describtion
Large number which acts as innity.
Steady-state point for the system.
Starting points for the simulation.
Vectors of lenght N.
Initialized to zero.
System matrices in continous time.
MPCDesign
and
u
v
and
and
to
DesignDiscreteMatrices.m
A, B , C , E .
, C , E
A, B
This is done by
equation (2.9).
DesignKalman.m.
See section
3.3.
Finally
xp
, , d , , Mx0 , Mu1 , MR ,
H
MD , , s .
4.2.2
MPCSimulate.m
Now the actual simulation is started. This consists of a loop which runs over the
timesteps dened in
t.
and measurement
y,
32
Implementation
and
v.
Dm (t),
and are assumed to span only one timestep, that is from tk to tk+1 . For instance,
a meal at
t = 420
Dm (420),
D(t) [mg/dL/min]
6.5
7.5
t [hour]
8.5
Dm
when feedforward is
used and when it isn't. See Section 3.1.3 for further details of these.
4.2
MPCControl.m
33
First
y , u1 , R
MPCCompute.m.
and
D are transformed
x is updated with
MPCCompute.m;
Kfx.
The gradient
ATLAB's quadprog
qpsolver.m, where qpsolver.m has shown
itself to greatly outperform quadprog.
is used as uk . If the solver fails, uk1 is used as uk .
The rst element of U
is updated.
is made by forming
U
init = [uk uk+1 uk+N 1 uk+N 1
U
T
k k+1 k+N 1 k+N 1 ] .
Then
xp
is updated.
34
Implementation
The found
variable).
ode15s.
4.2.3
is found by evaluating
MPCPlot
After the simulation-loop is nished the results are plotted in convenient ways.
The results include the state vector
disturbances
Dm .
x,
Chapter
= P1 (G + Gb ) Xr G + Dm (t)
dt
dXr
dt
dI
dt
= P2 Xr + P3 (I Ib )
U (t)
VI
G Gsc
=
Rutln
5
= nI +
dGsc
dt
dDm
dt
= Dm (t)
(5.1)
(5.2)
(5.3)
(5.4)
(5.5)
36
Xr
I
This state
and the input is the manipulated insulin infusion rate (U (t), mU/min).
time variable
The
Gb , Xbr , Ib , Gbsc
and
Dm
the system.
The parameters are assumed to be optimal, so they will not be inspected in this
project.
37
Name
Value
P1
P2
P3
n
VI
Rutln
Gb
Xbr
Ib
Gbsc
Dbm
0.028735 min1
0.028355 min1
5.035 105 mU/L
5/54 min
12 L
0.7400mg/dL/min
0.05
81.3 mg/dL
0
15mU/L
Gb 5 Rutln
0
Boundaries on the blood sugar level is needed to avoid the person going into
hyperglycemia or hypoglycemia. These limits corresponds to output constraints.
The input constraints restricts how much, and how fast, the insulin can be
injected, and ensures the system obeys physiological and physical limits.
The chosen constraints are shown below, respectively;
mg/dL
z 180
mg/dL
(5.6)
mU/min
u 100
mU/min
(5.7)
16.7
mU/min
u 16.7
60
mU/min
(5.8)
and
be the inputs
U (t)
and
D(t),
and let
G
X
r
x= I
Gsc
D
G
X
r
x = I
Gsc
Dm
(5.9)
Gb
X
br
xs = Ib ,
Gbsc
Dbm
us = n Ib VI ,
ds = 0
(5.10)
38
5.1 Linearization
By using the procedure shown in Chapter 2, it's clear that this system of dierential equations can be set up like this;
+ BU
+ ED
X = AX
X, U
D = d ds .
where
In this case,
and
and
(5.11)
X = x xs , U = u us
and
A, B
and
A=
x
,
(xs ,us ,ds )
B=
u
,
(xs ,us ,ds )
E=
d
(5.12)
P1 Xb Gb 0
0
P2 P3
A =
0
0
n
0.2
0
0
0
0
0
h
iT
= 0 0 1 0 0
B
VI
= 1 0 0 0 T
E
0
0
0
0.2
0
1
0
(5.13)
(5.14)
(5.15)
(5.16)
A, B
and
can be
evaluated by equation (2.9). These matrices are not evaluated here since they
depend on the used sampling time
Ts .
The model is now linearized and converted to discrete time, and ready to be
used with the MPC controller.
5.2 MPC
39
5.2 MPC
The goal for the controller is to keep the glucose level, for the diabetic, at a
healthy level at all times.
The sensor in the insulin pump is able to measure the glucose on the sub-
Gsc . Since
state which is
this is the only
C = 0 0 0 1 0 . The setpoint for
value, Gbsc .
is given by
The constraints used are giving in equations (5.7-5.8), and it should be noted
that the output constraints are implemented as soft constraints.
5.2.1
Weight matrices
Qz , S , s
and
S.
These
and
k ,
Qz
and
matrices are weights for the output and regularization term, respec-
tively. Tuning can be done by keeping one of these constant while varying the
other, since it's the ratio of these that actually matters in the objective function.
Qz
is selected as identity matrix, and S is varied, see Figures 5.1 and 5.2. The
t = 1 hour.
S , there is
is selceted to
102 ,
regularization and is still low enough to provide a decent response from the
controller in regards of speed.
It would also be possible to tune the Kalman Filter, in this thesis the weights
are selected as;
Qw = 102
and
Q = 102 .
Qz = 1,
S = 102 ,
S = 1,
s = 1,
Qw se = 102 ,
Q = 102
(5.17)
40
4000
u(t) [mU/min]
u(t) [mU/min]
5000
5000
10000
1.2
1.4
1.6
1.8
2000
0
2000
4000
1.2
1.4
t [hour]
90
80
100
90
80
70
1.2
1.4
1.6
1.8
1.2
1.4
t [hour]
(a)
S = 105
(b)
u(t) [mU/min]
u(t) [mU/min]
1.8
1.8
S = 104
1000
1000
0
1000
1
1.2
1.4
1.6
1.8
500
0
500
1.2
1.4
t [hour]
z(t)
Constrains
z(t) [mg/dL]
80
1.2
1.4
1.6
1.8
z(t)
Constrains
120
100
100
80
1.2
1.4
t [hour]
(c)
1.6
t [hour]
120
z(t) [mg/dL]
1.6
t [hour]
2000
1.6
1.8
1.8
t [hour]
S = 103
(d)
400
S = 102
200
u(t) [mU/min]
u(t) [mU/min]
z(t)
Constrains
110
70
200
0
150
100
50
0
50
200
1
1.2
1.4
1.6
1.8
1.2
1.4
t [hour]
1.6
t [hour]
z(t)
Constrains
z(t)
Constrains
160
z(t) [mg/dL]
140
z(t) [mg/dL]
1.8
120
z(t)
Constrains
100
z(t) [mg/dL]
z(t) [mg/dL]
110
2000
1.6
t [hour]
120
100
80
140
120
100
80
60
60
1
1.2
1.4
1.6
1.8
1.2
1.4
t [hour]
(e)
S = 101
1.6
t [hour]
(f)
S=1
1.8
5.2 MPC
41
3000
2000
u(t) [mU/min]
u(t) [mU/min]
5000
1000
0
1000
5000
0.5
1.5
t [hour]
2.5
2000
0.5
80
1.5
t [hour]
2.5
80
0.5
400
500
0
1.5
t [hour]
2.5
2.5
S = 104
200
0
80
1.5
t [hour]
(c)
1.5
t [hour]
z(t)
Constrains
120
100
400
0.5
z(t) [mg/dL]
z(t) [mg/dL]
2.5
z(t)
Constrains
120
2.5
100
80
0.5
S = 103
1.5
t [hour]
(d)
400
2.5
2.5
S = 102
200
u(t) [mU/min]
u(t) [mU/min]
200
500
200
0
1.5
t [hour]
150
100
50
0
50
0.5
2.5
z(t)
Constrains
1.5
t [hour]
z(t)
Constrains
160
z(t) [mg/dL]
140
z(t) [mg/dL]
1.5
t [hour]
(b)
600
120
100
80
60
0.5
S = 105
u(t) [mU/min]
u(t) [mU/min]
90
1000
200
0.5
z(t)
Constrains
100
1500
0.5
2.5
70
1
(a)
1000
0.5
110
90
70
0.5
1.5
t [hour]
120
z(t)
Constrains
100
z(t) [mg/dL]
z(t) [mg/dL]
110
140
120
100
80
1.5
t [hour]
(e)
S = 101
2.5
60
0.5
1.5
t [hour]
(f)
S=1
2.5
42
5.2.2
An appropriate horizon
N,
Ts ,
Dm ,
and in
tation of this.
Figure
5.3
illustrates
what
is
meant
by
stepchange.
The eect of this stepchange on the output
Dm ,
u.
4
t [hour]
use a sampling time of about 180 min/12 samples = 15 min. Since computational speed is
Ts = 8
min.
4000
N = 25
is chosen.
80
3500
75
3000
70
z(t) [mg/dL]
z(t) [mg/dL]
2500
2000
65
60
1500
55
1000
50
500
0
3
t [hour]
(a) Stepchange in
Dm
45
4
t [hour]
(b) Stepchange in
5.2 MPC
5.2.3
43
Noise
and
w N (0, Qw )
v N (0, Rv )
These vectors of noise are created in
randn
function,
which is able to create random numbers with mean zero and standard deviation
one.
To obtain the desired distribution for the noise, the noise is scaled with
periments are required to determine the size of
parameter for
randn,
and
v .
Ex-
noise, and thereby produce simulations with the same noise vectors scaled differently.
By experiments,
w = 0.7
and
103
v
0.4
0.8
0 ,
v = 0.8
(5.18)
These have shown themselfs to give simulations with a reasonable level of noise.
44
5.3 Simulations
The performance of the controller will now be tested with three dierent scenarios. In the rst test scenario our virtual patient is given three regular meals.
This is the main test scenario, and here the eect of feedforward will be investigated, as well as measurement noise and process noise. In the last two scenarios
the size of the meals are varied. Scenario II models a patient eating big meals,
and in Scenario III the patient ingests alot of meals throughout the day.
The main objective for the controller is to keep the patient within healthy blood
sugar limits at all time.
5.3.1
Scenario I
D(t)
(mg/dL/min)
12
18
10
5.3 Simulations
5.3.1.1
45
Feedforward
controller will be equiped with this approach, which simulates a controller where
the patient informs of future incoming meals.
The simulations are done without any process or measurement noise. Figures
5.5 and 5.6 illustrates the evolution of the states and the input/output, respectively, without feedforward, while Figures 5.7 and 5.8 illustrates the case with
feedforward.
200
G(t), Gsc(t) [mg/dL]
Dm(t) [mg/dL/min]
15
10
100
50
9 12 15 18 21 24
t [hour]
9 12 15 18 21 24
t [hour]
9 12 15 18 21 24
t [hour]
50
40
0.02
I(t) [mU/L]
Xr(t) [mU/L]
0.03
0.01
0
0.01
G
Gsc
150
30
20
10
9 12 15 18 21 24
t [hour]
46
u(t) [mU/min]
100
u(t)
Constraints
80
60
40
20
0
0
z(t) [mg/dL]
200
10
12
14
t [hour]
16
18
20
22
24
10
12
14
t [hour]
16
18
20
22
24
z(t)
Setpoint
150
100
50
0
200
G(t), Gsc(t) [mg/dL]
Dm(t) [mg/dL/min]
15
10
100
50
9 12 15 18 21 24
t [hour]
9 12 15 18 21 24
t [hour]
9 12 15 18 21 24
t [hour]
50
40
0.02
I(t) [mU/L]
Xr(t) [mU/L]
0.03
0.01
0
0.01
G
Gsc
150
30
20
10
9 12 15 18 21 24
t [hour]
5.3 Simulations
47
u(t) [mU/min]
100
u(t)
Constraints
80
60
40
20
0
0
z(t) [mg/dL]
200
10
12
14
t [hour]
16
18
20
22
24
10
12
14
t [hour]
16
18
20
22
24
z(t)
Setpoint
150
100
50
0
48
5.3.1.2
Measurement noise
y,
will be used .
A way to think of measurement noise, is how well the sensor, that measure the
glucose level in the subcutaneous layer, works.
Figure 5.9 illustrates the evolution of the ve states for the model, Figure 5.10
the input and the outputs, while 5.11 illustrates the dierence between the measured output and the real output.
160
G(t), Gsc(t) [mg/dL]
Dm(t) [mg/dL/min]
15
10
120
100
9 12 15 18 21 24
t [hour]
9 12 15 18 21 24
t [hour]
50
40
0.02
I(t) [mU/L]
Xr(t) [mU/L]
80
60
9 12 15 18 21 24
t [hour]
0.03
0.01
0
0.01
G
Gsc
140
30
20
10
9 12 15 18 21 24
t [hour]
5.3 Simulations
49
u(t) [mU/min]
100
u(t)
Constraints
80
60
40
20
0
0
z(t) [mg/dL]
200
10
12
14
t [hour]
16
18
20
22
24
10
12
14
t [hour]
16
18
20
22
24
z(t)
Setpoint
150
100
50
0
150
z
y
140
130
120
110
100
90
80
70
60
10
15
20
25
t [hour]
Figure 5.11: The measured and real output with measurement noise
50
Compared to Figures 5.7 and 5.8, Figures 5.9 and 5.10 does show some oscillations, which means the control problem is harder, though, the controller still
performs nicely, as the output is kept within the given blood glucose limits. On
Figure 5.11 the measured output
z.
It shows
that these two accompanies each other pretty good, even though oscillations are
present here too.
5.3.1.3
Process noise
This section will handle the process noise, that is, noise on
x.
Process noise could simulate physiological aspects the model doesn't consider.
200
G(t), Gsc(t) [mg/dL]
Dm(t) [mg/dL/min]
15
10
100
50
9 12 15 18 21 24
t [hour]
9 12 15 18 21 24
t [hour]
9 12 15 18 21 24
t [hour]
50
40
0.02
I(t) [mU/L]
Xr(t) [mU/L]
0.03
0.01
0
0.01
G
Gsc
150
30
20
10
9 12 15 18 21 24
t [hour]
5.3 Simulations
51
u(t) [mU/min]
100
u(t)
Constraints
80
60
40
20
0
0
z(t) [mg/dL]
200
10
12
14
t [hour]
16
18
20
22
24
10
12
14
t [hour]
16
18
20
22
24
z(t)
Setpoint
150
100
50
0
160
z
y
140
120
100
80
60
40
10
15
20
25
t [hour]
Figure 5.14: The measured and real output with process noise
52
5.3.1.4
Virtually every system has both measurement noise and process noise, here they
are both applied to the scenario. This is expected to be the most realistic simulation, as these types of noise would be present in the physical system.
200
G(t), Gsc(t) [mg/dL]
Dm(t) [mg/dL/min]
15
10
150
100
50
9 12 15 18 21 24
t [hour]
9 12 15 18 21 24
t [hour]
9 12 15 18 21 24
t [hour]
50
40
0.02
I(t) [mU/L]
Xr(t) [mU/L]
0.03
0.01
0
0.01
G
Gsc
30
20
10
9 12 15 18 21 24
t [hour]
Figure 5.15: The evolution of states with process and measurement noise
5.3 Simulations
53
u(t) [mU/min]
100
u(t)
Constraints
80
60
40
20
0
0
z(t) [mg/dL]
200
10
12
14
t [hour]
16
18
20
22
24
10
12
14
t [hour]
16
18
20
22
24
z(t)
Setpoint
150
100
50
0
Figure 5.16: Insulin input and output with process and measurement noise
150
z
y
140
130
120
110
100
90
80
70
60
50
10
15
20
25
t [hour]
Figure 5.17: The measured and real output with process and measurement noise
54
5.3.2
Scenario II
In the next two scenarios, the simulations will be done with both measurement
and process noise. Here the diabetic eats ve big meals.
Time (hours)
D(t)
(mg/dL/min)
12
10
15
18
13
22
10
100
50
9 12 15 18 21 24
t [hour]
0.04
50
0.03
40
0.02
0.01
0
0.01
G
Gsc
150
I(t) [mU/L]
Xr(t) [mU/L]
200
G(t), Gsc(t) [mg/dL]
Dm(t) [mg/dL/min]
15
9 12 15 18 21 24
t [hour]
9 12 15 18 21 24
t [hour]
30
20
10
9 12 15 18 21 24
t [hour]
Figure 5.18: The evolution of states with process and measurement noise
5.3 Simulations
55
u(t) [mU/min]
100
u(t)
Constraints
80
60
40
20
0
0
z(t) [mg/dL]
200
10
12
14
t [hour]
16
18
20
22
24
10
12
14
t [hour]
16
18
20
22
24
z(t)
Setpoint
150
100
50
0
Even with these big meals, the controller manages to keep the blood sugar at a
healthy quantity. Figure 5.19 does show that
and lower bound, and at
t 20
period of time. This is the eect of soft output constraints, and is to be expected
when the ingested meals are big.
56
5.3.3
Scenario III
D(t)
(mg/dL/min)
10
12
13
14
15
16
18
13
20
21
22
10
100
50
9 12 15 18 21 24
t [hour]
0.04
50
0.03
40
0.02
0.01
0
0.01
G
Gsc
150
I(t) [mU/L]
Xr(t) [mU/L]
200
G(t), Gsc(t) [mg/dL]
Dm(t) [mg/dL/min]
15
9 12 15 18 21 24
t [hour]
9 12 15 18 21 24
t [hour]
30
20
10
9 12 15 18 21 24
t [hour]
Figure 5.20: The evolution of states with process and measurement noise
5.3 Simulations
57
u(t) [mU/min]
100
u(t)
Constraints
80
60
40
20
0
0
z(t) [mg/dL]
200
10
12
14
t [hour]
16
18
20
22
24
10
12
14
t [hour]
16
18
20
22
24
z(t)
Setpoint
150
100
50
0
Again the controller performs good, but, like with Scenario II, the lower boundary on the output is violated briey.
58
Chapter
6
Conclusion
What the simulations show, is that the MPC controller, based on Bergmans
minimal model, does a good job keeping the patient inside the blood glucose
limits.
This is the case for both Scenario I, where meals of ordinary size is
simulated, Scenario II, where the patient is assumed to eat relatively big meals,
and Scenario III, where big meals combined with many small meals are ingested.
The simulations also show, that the controller handles noise, on both the measurements and on the process, well. This is important since a practical implementation of the system should denitely be expected to contain noise.
Feedforward showed itself to be an advantage, as the controller is able to take
precaution of future meals, and thereby making it more likely for the patient
to stay within the healthy blood glucose limits. This can be seen by comparing
Figures 5.6 and 5.8.
on a real diabetic should use feedforward, it would require the patient to tell
the device what hours a day he would eat, and how big the meals would be.
Therefore feedforward may not be very useful in practice.
It should be kept in mind that the model used for simulations is a minimal
model, so what can be concluded is that there is a change the MPC would
work for the insulin pump case, but the model used is too inadequate to draw
any denite conclusion. Another reason why MPC wouldn't be used is seen on
Figure 5.19 for Scenario II at
t 20,
reveals itself. The amount of blood glucose slightly violates it's lower boundary,
which is potentially disastrous for the patient.
straints are needed to make sure the control problem stay feasible.
MPC is an interesting tool to use as controller in an insulin pump, but the
model of the patient must be improved to make it really useful.
60
Conclusion
Appendix
Impulse-response method
The MPC method solves the control problem using a model. Instead of using a
linear model, the impuls-response method and the step response could be used
[1].
The idea behind these methods, is that the process can apply an impuls at any
input, and then measure the response. Thereby the name impuls-response.
Related to the state-space model from the previous chapters:
xk+1 = A xk + B uk
(A.1)
yk = C xk
(A.2)
where
E dk
yk
is neglected from the equation, due the fact that we only want make an
x0 = 0 and
that u is an
t0 ,
such
integer,
u0 6= 0
and
uk = 0
for
k 1.
I.e the
u = u0
T
(A.3)
xk ,
uk .
zk ,
is;
x0 = 0
y0 = H0 u0 = 0
(A.4)
x1 = B u0
y1 = H1 u0 = C Bu0
(A.5)
x2 = A Bu0
y2 = H2 u0 = C ABu0
(A.6)
.
.
.
.
.
.
(A.7)
xk = A
Where
k1
Bu0
Hk = C Ak1 B
yk = Hk u0 = C A
k1
Bu0
(A.8)
Hk
for
62
Impulse-response method
State
Output
Impulse response
Step response
sequence
sequence
sequence
sequence
x0 = 0
x1 = Bu0
x2 = ABu0
y0 = 0
y1 = C Bu0
y2 = C ABu0
H0 = 0
H1 = C B
H2 = C AB
S0 = 0
S1 = C B
S2 = C AB + C B
.
.
.
.
.
.
.
.
.
.
.
.
xk = Ak1 Bu0
yk = C Ak1 Bu0
Hk = C Ak1 B
Sk =
Pk1
i=0
C Ai B
k = 0 . . . N.
Step responce is then the summerize of all the values of
Sk =
k1
X
Hi
(A.9)
i=0
The relation between the sequences is outlined in Table A;
Using these terms, there can be achived an output using a single input at a
given time
instead of
ts .
uk .
is replaced with
E,
and
u0
with
d0 .
dk ,
as inputs
x0 = 0
H0 = 0
(A.10)
x1 = E d 0
H1 = C B
(A.11)
x2 = A Eu0
H2 = C AB
(A.12)
.
.
.
.
.
.
(A.13)
xk = A
k1
Ed0
Hk = C A
k1
y,
(A.14)
from this.
The impuls-responce method can be used, when the MPC does not have a
model to build the controller on. Then the method is used to make the Markov
parameter and then solve the problem in an indirect way.
Appendix
B
Matlab programs
B.1 Scena1.m
1
%S c e n a 1 .m
%M a t i a s
%T e c h n i c a l
%S p r i n g
%B . S c .
Srensen
s042300
University
and
of
Simon
Kristiansen
s042264
Denmark
2007
Thesis
%F i l e
Model
Predictive
Control
for
an
Artificial
Pancreas
description :
10
11
%S c e n a r i o
12
13
I .
Three
normal
meals
are
simulated .
14
15
clear
all
16
close
all
17
18
%Time
19
t0
0.0;
%[ min ]
Start
20
Ts =
8;
%[ min ]
Sampling
21
tf
6024;
%[ min ]
End
22
=
=
=
( t 0 : Ts : t f ) ' ;
%Time
23
24
%S i z e
25
N =
of
horizon
25;
26
27
%C o n s t r a i n t s
28
umin
0;
umax
29
udmin =
16.7;
udmax =
16.7;
30
zmin
60;
zmax
180;
=
=
=
=
100;
31
32
%M e a l s
33
mealsv
( disturbance )
34
m e a l s v ( 8 6 0 / Ts )
35
m e a l s v ( 1 2 6 0 / Ts )
zeros (1 , length ( t ) ) ;
=
=
5;
7;
vector
time
64
36
Matlab programs
m e a l s v ( 1 8 6 0 / Ts )
10;
1 e 3
0.4
37
38
%N o i s e
39
sigmaw
40
%sigmaw
41
42
43
sigmav
%s i g m a v
seed
[0.7
[0
0.8
0];
0];
0.8;
0;
3507;
%C h o i c e
for
randn
44
45
%Use
46
feed
feedforward ?
=
0;
%1 =
yes ,
0 = no
47
48
%W e i g h t
49
Q =
eye ( 1 ) ;
matrices
e y e ( 1 ) 1 e 2;
50
S =
51
Seta
eye ( 1 ) ;
52
seta
eye ( 1 ) ;
53
54
%S t a r t
55
MPCControl
MPC
B.2 Scena2.m
1
%S c e n a 2 .m
%M a t i a s
%T e c h n i c a l
%S p r i n g
%B . S c .
Srensen
s042300
University
and
of
Simon
Kristiansen
s042264
Denmark
2007
Thesis
%F i l e
Model
Predictive
Control
for
an
Artificial
Pancreas
description :
10
11
%S c e n a r i o
12
13
II .
Five
big
meals
are
simulated .
14
15
clear
all
16
close
all
17
18
%Time
19
t0
0.0;
%[ min ]
Start
20
Ts =
8;
%[ min ]
Sampling
21
tf
6024;
%[ min ]
End
22
=
=
( t 0 : Ts : t f ) ' ;
%Time
23
24
%S i z e
25
N =
of
horizon
25;
26
27
%C o n s t r a i n t s
28
umin
0;
umax
29
udmin =
16.7;
udmax =
16.7;
30
zmin
60;
zmax
180;
=
=
=
=
100;
31
32
%M e a l s
33
mealsv
34
m e a l s v ( 8 6 0 / Ts )
35
m e a l s v ( 1 2 6 0 / Ts )
36
m e a l s v ( r o u n d ( 1 5 6 0 / Ts ) )
37
m e a l s v ( 1 8 6 0 / Ts )
13;
38
m e a l s v ( 2 2 6 0 / Ts )
6;
39
( disturbance )
=
zeros (1 , length ( t ) ) ;
=
=
7;
10;
=
5;
vector
time
B.3 Scena3.m
40
%N o i s e
41
sigmaw
[0.7
42
sigmav
0.8;
43
seed
65
1 e 3
3507;
0.4
%C h o i c e
0.8
for
0];
randn
44
45
%Use
46
feed
feedforward ?
=
1;
%1 =
yes ,
0 = no
47
48
%W e i g h t
49
Q =
eye ( 1 ) ;
matrices
e y e ( 1 ) 1 e 2;
50
S =
51
Seta
eye ( 1 ) ;
52
seta
eye ( 1 ) ;
53
54
%S t a r t
55
MPCControl
MPC
B.3 Scena3.m
1
%S c e n a 3 .m
%M a t i a s
%T e c h n i c a l
%S p r i n g
%B . S c .
Srensen
s042300
University
and
of
Simon
Kristiansen
s042264
Denmark
2007
Thesis
%F i l e
Model
Predictive
Control
for
an
Artificial
Pancreas
description :
10
11
%S c e n a r i o
12
13
III .
Many
meals
throughout
the
24
14
15
clear
all
16
close
all
17
18
%Time
19
t0
0.0;
%[ min ]
Start
20
Ts =
8;
%[ min ]
Sampling
21
tf
6024;
%[ min ]
End
22
=
=
( t 0 : Ts : t f ) ' ;
%Time
vector
23
24
%S i z e
25
N =
of
horizon
25;
26
27
%C o n s t r a i n t s
28
umin
29
30
( bequette
article )
0;
umax
udmin =
16.7;
udmax =
16.7;
zmin
60;
zmax
180;
=
=
100;
31
32
%M e a l s
33
mealsv
( disturbance )
34
m e a l s v ( r o u n d ( 8 6 0 / Ts ) )
35
m e a l s v ( r o u n d ( 1 0 6 0 / Ts ) )
5;
36
m e a l s v ( r o u n d ( 1 2 6 0 / Ts ) )
5;
37
m e a l s v ( r o u n d ( 1 3 6 0 / Ts ) )
7;
38
m e a l s v ( r o u n d ( 1 4 6 0 / Ts ) )
4;
39
m e a l s v ( r o u n d ( 1 5 6 0 / Ts ) )
5;
40
m e a l s v ( r o u n d ( 1 6 6 0 / Ts ) )
6;
41
m e a l s v ( r o u n d ( 1 8 6 0 / Ts ) )
13;
42
m e a l s v ( r o u n d ( 2 0 6 0 / Ts ) )
5;
43
m e a l s v ( r o u n d ( 2 1 6 0 / Ts ) )
3;
zeros (1 , length ( t ) ) ;
=
7;
time
hour
period .
66
44
Matlab programs
m e a l s v ( r o u n d ( 2 2 6 0 / Ts ) )
4;
45
46
%N o i s e
47
sigmaw
[0.7
48
sigmav
0.8;
49
seed
3507;
1 e 3
0.4
%C h o i c e
0.8
for
0];
randn
50
51
%Use
52
feed
feedforward ?
=
1;
%1 =
yes ,
0 = no
53
54
%W e i g h t
55
Q =
eye ( 1 ) ;
matrices
e y e ( 1 ) 1 e 2;
56
S =
57
Seta
eye ( 1 ) ;
58
seta
eye ( 1 ) ;
59
60
%S t a r t
61
MPCControl
MPC
B.4 MPCControl.m
1
%MPCControl .m
%M a t i a s
%T e c h n i c a l
%S p r i n g
%B . S c .
Srensen
s042300
University
of
and
Simon
Kristiansen
s042264
Denmark
2007
Thesis
%F i l e
Model
Predictive
Control
for
an
Artificial
Pancreas
description :
10
11
%Wrapper
12
13
file
for
the
three
MPC
phases .
14
15
MPCDesign
16
MPCSimulate
17
MPCPlot
B.5 MPCDesign.m
1
%MPCDesign .m
%M a t i a s
%T e c h n i c a l
%S p r i n g
%B . S c .
Srensen
s042300
University
of
and
Simon
Kristiansen
s042264
Denmark
2007
Thesis
%F i l e
Model
Predictive
Control
for
an
Artificial
Pancreas
description :
10
11
%D e s i g n
12
%v a r i o u s
13
14
phase ,
design
initializes
15
16
%R e t r i e v e
17
p =
18
P1 = p ( 1 ) ;
the
MPC
matrices
by
calling
files .
parameters
DesignParameters ( ) ;
B.5 MPCDesign.m
19
P2 = p ( 2 ) ;
20
P3 = p ( 3 ) ;
21
n = p(4);
22
VI = p ( 5 ) ;
23
Rutln
= p(6);
24
alpha
= p(7);
25
Gb = p ( 8 ) ;
26
Xb = p ( 9 ) ;
27
Ib
28
Gbsc = p ( 1 1 ) ;
29
Db = p ( 1 2 ) ;
67
= p(10);
30
31
big
10^10;
%A c t s
as
infinity
32
33
%S t e a d y s t a t e
34
xs
[ Gb ;
35
us
= VI n I b ;
36
ds
= Db ;
37
zs
= Gbsc ;
values
Xb ;
Ib ;
( basal
Gbsc ;
values )
Db ] ;
38
39
%S t a r t i n g
40
x0
xs ;
41
u0 =
us ;
points
42
43
%I n i t i a l i z e
44
x =
zeros (5 ,
45
u =
zeros ( length ( t ) , 1 ) ;
46
ubar
47
y =
variables
length ( t ) ) ;
x(: ,
z e r o s ( 2 N , 1 ) ;
1)
u(1)
x0 ;
= u0 ;
ubar ( 1 )
= u0 ;
zeros ( length ( t ) , 1 ) ;
48
49
%D i s t u r b a n c e
50
D =
z e r o s (N,
1);
51
52
%S e t
53
R =
point
r e p m a t ( Gbsc ,
N,
1);
54
55
%G e n e r a t e
noise
56
57
w =
58
for
59
60
seed ) ;
randn ( 5 , l e n g t h ( t ) ) ;
k =
1:5
w( k , : )
= w( k , : )
sigmaw ( k ) ;
end
61
62
v =
seed ) ;
randn ( l e n g t h ( t ) , 1 ) sigmav ;
63
64
65
%T r a n s f o r m
66
umin = umin
into
67
zmin
%S e t
up
zmin
deviation
variables
us ;
umax = umax
zs ;
zmax = zmax
us ;
zs ;
68
69
70
[ Umin ,
71
constraints
Umax ,
Udmin ,
Udmax ,
Zmin ,
D e s i g n C o n s t r a i n t s ( umin ,
umax ,
Zmax ,
72
73
%M a t r i c e s
74
Ac =
in
continous
P1Xb
Gb
P2
time
1;
P3
0;
. . .
0;
. . .
0;
. . .
75
76
77
0.2
0.2
78
79
Bc =
[0;
80
Ec =
[1;
81
Cc =
[0
0;
0;
0
1 / VI ;
0;
1
0;
0;
0];
a l p h a
];
0];
82
83
%M a t r i c e s
in
discrete
time
ubarmin ,
udmax ,
. . .
a l p h a
];
udmin ,
ubarmax ]
zmin ,
zmax ,
. . .
big ,
N) ;
68
84
Matlab programs
[ A, B , E , C ]
D e s i g n D i s c r e t e M a t r i c e s ( Ac , Bc , Ec , Cc , Ts ) ;
85
86
87
%Kalman
[ Kfx ,
Filter
A,
B,
design
C,
E]
DesignKalman (A,
B,
C,
E);
88
89
90
%MPC
matrices
91
. . .
D e s i g n M P C M a t r i c e s ( A , B , E , C , Q, S , N , S e t a , s e t a ) ;
92
93
[ x0x s ;
xp =
0];
B.6 DesignKalman.m
1
%D e s i g n C o n s t r a i n t s .m
%M a t i a s
%T e c h n i c a l
%S p r i n g
%B . S c .
Srensen
s042300
University
of
and
Simon
Kristiansen
s042264
Denmark
2007
Thesis
%F i l e
Model
Predictive
Control
for
an
Artificial
Pancreas
description :
10
11
%D e s i g n s
12
%A , B , E
13
14
matrices
and
needed
such
they
for
the
contain
Kalman
the
Filter ,
integrated
and
extends
noise .
15
16
function
[ Kfx ,
Ae ,
Be ,
Ce ,
Ee ]
DesignKalman (A,
B,
C,
E)
17
18
n =
s i z e (A , 1 ) ;
19
m =
s i z e (C , 1 ) ;
20
21
%Kalman
22
Qw =
design
e y e ( n , n ) 1 e 2;
e y e (m,m) 1 e 2 ;
23
Qxi
24
Rv =
e y e (m,m ) ;
25
Ad =
e y e (m,m ) ;
26
Ae =
[A B;
27
Be =
[B;
z e r o s (m, n )
Ad ] ;
z e r o s (m,m ) ] ;
28
Ee =
[E;
29
Ce =
[C
z e r o s (m,m ) ] ;
30
Qe =
[ Qw
z e r o s (m,m ) ] ;
z e r o s ( n ,m ) ;
z e r o s (m,
n)
Qxi ] ;
31
32
P =
33
Re = Ce P Ce '+Rv ;
d a r e ( Ae ' ,
34
Kfx = P ( Ce '
35
%k e y b o a r d
Ce ' ,
/
Qe ,
Rv ) ;
Re ) ;
B.7 DesignDiscreteMatrices.m
1
%D e s i g n C o n s t r a i n t s .m
%M a t i a s
%T e c h n i c a l
%S p r i n g
%B . S c .
Srensen
s042300
University
of
and
Simon
Kristiansen
s042264
Denmark
2007
Thesis
Model
Predictive
Control
for
an
Artificial
Pancreas
B.8 DesignParameters.m
%F i l e
69
description :
10
11
%C o n v e r t s
12
13
matrices
A, B , E
and
into
discrete
time .
14
15
function
[ Ad , Bd , Ed , Cd ]
D e s i g n D i s c r e t e M a t r i c e s ( Ac ,
Bc ,
Ec ,
Cc ,
Ts )
16
17
M1 =
18
M2 = expm (M1 Ts ) ;
[ Ac
Bc
Ec ;
zeros (2 ,
7)];
19
Ad = M2 ( 1 : s i z e ( Ac , 1 ) ,
20
Bd = M2 ( 1 : s i z e ( Ac , 1 ) ,
s i z e ( Ac , 2 ) + 1 : s i z e ( Ac , 2 ) + 1 ) ;
21
Ed = M2 ( 1 : s i z e ( Ec , 1 ) ,
s i z e ( Ac , 2 ) + s i z e ( Bc , 2 ) + 1 : e n d ) ;
22
Cd = Cc ;
1 : s i z e ( Ac , 2 ) ) ;
B.8 DesignParameters.m
1
%D e s i g n P a r a m e t e r s .m
%M a t i a s
%T e c h n i c a l
%S p r i n g
%B . S c .
Srensen
s042300
University
of
and
Simon
Kristiansen
s042264
Denmark
2007
Thesis
%F i l e
Model
Predictive
Control
for
an
Artificial
Pancreas
description :
10
11
%C o n t a i n s
12
13
basic
parameters
for
the
Bergman
model .
14
15
function
p =
DesignParameters ( )
16
17
%Model
18
P1 =
0.028735;
parameters
%[ min ^ ( 1 ) ]
19
P2 =
0.028344;
%[ min ^ ( 1 ) ]
20
P3 =
5 . 0 3 5 e 5;
%[mU/L ]
21
Gb =
81.3;
%[mg/ dL ]
22
Ib
15;
%[mU/L ]
23
VI =
12;
%[L ]
24
n =
25
Rutln
26
Gbsc = Gb5 R u t l n ;
27
alpha
28
Xb =
0;
%[mU/L ]
29
Db =
0;
%[mg/ dL / min ]
%[ min ^ ( 1 ) ]
5/54;
=
0.74;
%[mg/ dL / min ]
%[mg/ dL ]
0.05;
%[]
30
31
%P a r a m e t e r
32
p =
[ P1 ;
vektor
P2 ;
P3 ;
n;
VI ;
Rutln ;
alpha ;
Gb ;
Xb ;
Ib ;
Gbsc ;
Db ] ;
B.9 DesignConstraints.m
1
%D e s i g n C o n s t r a i n t s .m
%M a t i a s
%T e c h n i c a l
%S p r i n g
%B . S c .
Srensen
s042300
University
of
and
Simon
Kristiansen
s042264
Denmark
2007
Thesis
Model
Predictive
Control
for
an
Artificial
Pancreas
70
Matlab programs
%F i l e
description :
10
11
%A s s i g n s
12
13
constraints
for
u,
Delta_u ,
and
ubar
into
vectors ;
14
15
function
[ Umin ,
16
Umax ,
Udmin ,
Udmax ,
D e s i g n C o n s t r a i n t s ( umin ,
Zmin ,
umax ,
Zmax ,
udmax ,
ubarmin ,
udmin ,
ubarmax ]
zmin ,
zmax ,
. . .
big ,
17
18
19
Umin =
r e p m a t ( umin ,
N,
1);
20
Umax =
r e p m a t ( umax ,
N,
1);
21
22
Udmin =
r e p m a t ( udmin ,
N 1 ,
1);
23
Udmax =
r e p m a t ( udmax ,
N 1 ,
1);
24
25
Zmin =
r e p m a t ( zmin , N , 1 ) ;
26
Zmax =
r e p m a t ( zmax , N , 1 ) ;
27
28
ubarmin
[ Umin ;
z e r o s (N , 1 ) ] ;
29
ubarmax =
[ Umax ;
o n e s ( N, 1 ) b i g ] ;
B.10 DesignMPCMatrices.m
1
%D e s i g n M P C M a t r i c e s .m
%M a t i a s
%T e c h n i c a l
%S p r i n g
%B . S c .
Srensen
s042300
University
and
of
Simon
Kristiansen
s042264
Denmark
2007
Thesis
%F i l e
Model
Predictive
Control
for
an
Artificial
Pancreas
description :
10
11
%D e s i g n s
various
12
%C r e d i t :
John
13
14
matrices
Bagterp
needed
for
the
MPC
algorithm .
Jrgensen
15
16
function
17
18
19
nx =
s i z e (A , 2 ) ;
20
nu =
s i z e (B , 2 ) ;
21
nd =
s i z e (E , 2 ) ;
22
nz
s i z e ( Cz , 1 ) ;
23
24
% Form Gamma,
25
Gamma =
26
Gammad =
27
Phi
Gammad ,
Phi
z e r o s (N nz , N nu ) ;
z e r o s (N nz , N nd ) ;
z e r o s (N nz , nx ) ;
28
29
T = Cz ;
30
kz
31
for
0;
k = 1 :N
32
Gamma( k z + 1 : k z+nz , 1 : nu )
33
Gammad( k z + 1 : k z+nz , 1 : nd )
34
T = TA ;
35
P h i ( k z + 1 : k z+nz , 1 : nx )
36
kz
37
end
k z+n z ;
= TB ;
= TE ;
= T;
barseta ]
. . .
N)
B.10 DesignMPCMatrices.m
71
38
39
for
k = 2 :N
40
Gamma ( ( k 1) n z + 1 : end , ( k 1) nu + 1 : k nu
41
Gammad ( ( k 1) n z + 1 : end , ( k 1) nd + 1 : k nd )
42
= Gamma ( 1 : ( N+1k ) nz , 1 : nu ) ;
= Gammad ( 1 : ( N+1k ) nz , 1 : nd ) ;
end
43
44
% Form QZ
45
QZ =
z e r o s (N nz , N n z ) ;
46
kz
0;
47
for
k = 1 :N
48
49
kz
50
= Qz ;
k z+n z ;
end
51
52
% Form
53
HS =
HS
z e r o s (N nu , N nu ) ;
54
55
if
N == 1
56
57
HS = S ;
else
58
k =0;
59
HS ( 1 : nu , 1 : nu )
60
HS(1+ nu : nu+nu , 1 : nu )
2 S ;
=
S ;
61
62
k = 1 :N2
for
63
ku = k nu ;
64
HS ( kunu + 1 : ku , ku + 1 : ku+nu )
65
HS ( ku + 1 : ku+nu , ku + 1 : ku+nu )
S ;
2 S ;
66
67
S ;
end
68
k=N 1 ;
69
70
ku = k nu ;
71
HS ( kunu + 1 : ku , ku + 1 : ku+nu )
72
HS ( ku + 1 : ku+nu , ku + 1 : ku+nu )
= S;
73
S ;
end
74
75
76
% Form Mum1
77
Mum1 = [ S ;
z e r o s ( ( N 1) nu , nu ) ] ;
78
79
barSeta
e y e (N) S e t a ;
80
barseta
o n e s ( N, 1 ) s e t a ;
81
82
83
T = Gamma' QZ ;
84
H = TGamma + HS ;
85
H =
86
Mx0 = T P h i ;
(H+H ' ) / 2 ;
T ;
87
MR =
88
MD = TGammad ;
89
90
% barH ,
barg
91
92
93
barH =
z e r o s (N, N ) ;
[H
95
% Form
Lambda
96
Lambda =
97
T = [ e y e ( nu , nu )
z ;
barSeta ] ;
94
z e r o s ( ( N 1) nu , N nu ) ;
e y e ( nu , nu ) ] ;
98
99
for
101
k = 1 :N1
Lambda ( ( k 1) nu + 1 : k nu , ( k 1) nu + 1 : ( k +1) nu )
100
end
T;
72
Matlab programs
B.11 MPCSimulate.m
1
%MPCPlot .m
%M a t i a s
%T e c h n i c a l
%S p r i n g
%B . S c .
Srensen
s042300
University
and
of
Simon
Kristiansen
s042264
Denmark
2007
Thesis
%F i l e
Model
Predictive
Control
for
an
Artificial
Pancreas
description :
10
11
%S i m u l a t i o n
phase ,
12
%i n s u l i n
and
13
14
u,
loops
uses
over
this
as
each
input
timestep
to
the
and
finds
Bergman
the
optimal
model .
15
16
17
18
%I t e r a t e
for
k = 1 : l e n g t h ( t ) 1
display (k)
19
20
21
%M e a s u r e m e n t
22
y(k)
Noise
noise
= x(4 ,k)
+ v(k );
23
24
%Model
25
x(1 ,k)
= x(1 ,k)
noise
+ w( 1 , k ) ;
26
x(2 ,k)
= x(2 ,k)
+ w( 2 , k ) ;
27
x(3 ,k)
= x(3 ,k)
+ w( 3 , k ) ;
28
x(4 ,k)
= x(4 ,k)
+ w( 4 , k ) ;
29
30
%Meal
31
x(5 ,k)
Disturbance
disturbance
= x(5 ,k)
at
this
point
in
time
mealsv ( k ) ;
32
33
34
%W i t h o u t
if
feed
35
36
37
feedforward
== 0
% Do
%With
elseif
38
feed
if
== 1
l e n g t h ( t )1 k ;
n =
39
nothing
Feedforward
N <= n ;
40
%A s s i g n
41
D ( 1 : N)
42
future
=
%S i m u l a t i o n
44
D =
45
is
ending ,
assign
zeros
to
z e r o s (N , 1 ) ;
D( 1 : n )
46
m e a l s v ( k : k+n 1 ) ;
end
else
48
49
return ;
50
to
else
43
47
meals
m e a l s v ( k : k+N 1 ) ;
feed
parameter ' )
end
51
52
53
%Dummy
if
54
55
to
handle
u0
un = u0 ;
else
un = u ( k 1 ) ;
56
57
statement
k == 1
end
58
59
60
%MPC
step
[ unow , xp , i n f o ]
. . .
61
z s , z s , u s , d s , xp , u b a r ,
62
63
Phi ,
Gammad ,
Gamma,
. . .
Lambda ,
. . .
big ,
. . .
B.12 MPCCompute.m
73
64
umin ,
65
ubarmin ,
umax ,
66
Udmin ,
67
Kfx ,
68
A,
udmin ,
ubarmax
Udmax ,
udmax ,
. . .
. . .
. . .
. . .
B,
E,
C,
N) ;
69
70
71
%Check
72
if
for
info
error
== 1
73
74
return
75
in
QP
solver ! ');
end
76
77
%S i m u l a t e
78
[ T,X]
step
o d e 1 5 s ( @BergmanMinimalModel ,
[ t (k)
t (k +1)] ,
x(: ,
k) ,
79
. . .
[] ,
p,
unow ) ;
80
81
% Remember
82
x(: ,
k +1) = X( end ,
83
u(k)
= unow ;
84
variables
:);
end
B.12 MPCCompute.m
1
%MPCCompute .m
%M a t i a s
%T e c h n i c a l
%S p r i n g
%B . S c .
Srensen
s042300
University
and
of
Simon
Kristiansen
s042264
Denmark
2007
Thesis
%F i l e
Model
Predictive
Control
for
an
Artificial
Pancreas
description :
10
11
%C a l c u l a t e s
12
13
the
insulin
input
for
this
timestep ,
u0 .
14
15
function
[ u0 , xp , i n f o ]
. . .
16
y s , z s , u s , d s , xp , u b a r ,
17
18
Phi ,
19
umin ,
GammaD,
umax ,
20
ubarmin ,
21
dUmin ,
22
Kfx ,
23
A,
24
25
nu =
l e n g t h ( um1 ) ;
26
nd =
l e n g t h ( ds ) ;
27
nz
length ( zs ) ;
28
%Form
29
dy = y
deviation
variables
ys ;
30
dum1 = um1u s ;
31
dR = Rr e p m a t ( z s ,
N,
1);
32
dD = Dr e p m a t ( d s ,
N,
1);
33
dd = dD ( 1 : nd ,
1);
34
35
%Kalman
36
37
x0
38
= dy
Filter
C xp ;
= xp + Kfx e ;
B,
Gamma,
dumin ,
ubarmax
dUmax ,
. . .
E,
C,
N)
. . .
. . .
Lambda ,
dumax ,
. . .
. . .
big ,
. . .
. . .
74
Matlab programs
39
%U p d a t e
40
g = Mx0 x 0
41
gbar
gradient
+ MRdR + MDdD + Mum1 dum1 ;
[g;
sbareta ] ;
42
43
%C r e a t e
bounds
for
ubar
44
u b a r m i n ( 1 : nu , 1 )
45
ubarmax ( 1 : nu , 1 )
46
47
48
%C r e a t e
c
bounds
Phi x0
for
+ GammaDdD ;
49
Zbarmin
50
Zbarmax = Zmax
= Zmin
c ;
c ;
51
52
%S e t u p
matrix
53
bmin =
[ dUmin ;
system
b i g o n e s (N,
for
boundaries
54
bmax =
[ dUmax ;
Zbarmax ;
55
Abar =
[ Lambda
z e r o s ( N 1 ,
1);
Zbarmin ] ;
b i g o n e s (N , 1 ) ] ;
N) ;
Gamma
e y e ( N , N ) ;
Gamma
e y e (N, N ) ] ;
56
57
58
%C r e a t e
startguess
ubarinit
u b a r ( nu + 1 :N nu , 1 ) ;
u b a r ( ( N 1) nu + 1 :N nu , 1 ) ;
u b a r ( nu N+n z + 1 : end , 1 ) ;
59
. . .
u b a r ( nu N+(N 1) n z + 1 : e n d )
];
60
61
62
% QPSOLVER
63
64
65
66
if
info
67
== 0
du0 =
68
u b a r ( 1 : nu , 1 ) ;
else
69
du0 = dum1 ;
70
end
%
73
74
% QUADPROG
75
76
77
78
79
80
81
82
83
du0 =
84
info
85
86
87
88
89
71
72
. . .
EXITFLAG == 1
u b a r ( 1 : nu , 1 ) ;
0;
else
du0 = dum1 ;
info
1;
end
90
91
92
% Update
93
xp = A x 0
Kalman
Filter
+ B du0 + E dd ;
94
95
% Form
96
u0 = du0 +
physical
variable
us ;
B.13 BergmanMinimalModel.m
1
%B e r g m a n M i n i m a l M o d e l .m
B.14 MPCPlot.m
%M a t i a s
%T e c h n i c a l
%S p r i n g
%B . S c .
Srensen
75
s042300
University
of
and
Simon
Kristiansen
s042264
Denmark
2007
Thesis
%F i l e
Model
Predictive
Control
for
an
Artificial
Pancreas
description :
10
11
%I m p l e m e n t a t i o n
12
%d i f f e r e n t i a l
13
14
of
the
Bergman
which
consists
of
equations .
15
16
function
xdot
BergmanMinimalModel ( t , x , p , u )
17
18
xdot
zeros (5 ,1);
19
20
G = x(1 ,1);
21
X = x(2 ,1);
22
23
Gsc = x ( 4 , 1 ) ;
= x(3 ,1);
24
D = x(5 ,1);
25
26
P1 = p ( 1 ) ;
27
P2 = p ( 2 ) ;
28
P3 = p ( 3 ) ;
29
n = p(4);
30
VI = p ( 5 ) ;
31
Rutln
= p(6);
32
alpha
= p(7);
33
Gb = p ( 8 ) ;
34
Xb = p ( 9 ) ;
35
Ib
36
Gbsc = p ( 1 1 ) ;
= p(10);
37
Db = p ( 1 2 ) ;
38
39
xdot ( 1 , 1 )
40
xdot ( 2 , 1 )
41
xdot ( 3 , 1 )
42
xdot ( 4 , 1 )
43
xdot ( 5 , 1 )
P1 (GGb ) XG +
P2 X + P3 ( I I b ) ;
n I + u / VI ;
(GGsc ) / 5 R u t l n ;
a l p h a D ;
D;
B.14 MPCPlot.m
1
%MPCPlot .m
%M a t i a s
%T e c h n i c a l
%S p r i n g
%B . S c .
Srensen
s042300
University
of
and
Simon
Kristiansen
s042264
Denmark
2007
Thesis
%F i l e
Model
Predictive
Control
for
an
Artificial
Pancreas
description :
10
11
%E v a l u a t i o n
12
13
phase ,
plots
14
15
16
17
%t
t
should
=
t /60;
be
in
hours
the
simulations .
five
76
18
Matlab programs
%P l o t
the
five
states
19
figure ;
20
subplot (221);
21
plot (t , x ( 5 , : ) ) ;
22
y l a b e l ( 'D_{m} ( t )
23
xlabel ( ' t
24
25
V =
axis ;
s e t ( gca ,
[ hour ] ' ) ;
axis ([0
t f /60
V( 3 )
V( 4 ) ] )
0:3:24)
26
27
subplot (222);
28
29
30
y l a b e l ( 'G( t ) ,
31
32
33
xlabel ( ' t
V =
[ mg/ dL ] ' ) ;
[ hour ] ' ) ;
axis ;
s e t ( gca ,
G_{ s c } ( t )
axis ([0
t f /60
V( 3 )
V( 4 ) ] )
0:3:24)
34
35
subplot (223);
36
plot (t , x ( 2 , : ) ) ;
37
y l a b e l ( 'X_{ r } ( t )
38
xlabel ( ' t
39
40
V =
axis ;
s e t ( gca ,
[mU/L ] ' ) ;
[ hour ] ' )
axis ([0
t f /60
V( 3 )
V( 4 ) ] )
0:3:24)
41
42
subplot (224);
43
plot (t , x ( 3 , : ) ) ;
44
ylabel ( ' I ( t )
45
xlabel ( ' t
46
47
V =
axis ;
s e t ( gca ,
[mU/L ] ' ) ;
[ hour ] ' )
axis ([0
t f /60
V( 3 )
V( 4 ) ] )
0:3:24)
48
49
%P l o t
input
and
output
50
figure
51
subplot (211);
52
plot (t ,
53
ylabel ( 'u( t )
54
xlabel ( ' t
55
hold
56
stairs (t ,
r e p m a t ( umin+u s ,
length ( t )) ,
'k ' ,
2);
57
stairs (t ,
r e p m a t ( umax+u s ,
length ( t )) ,
'k ' ,
2);
58
59
60
V =
u,
'b ' )
[mU/ min ] ' ) ;
[ hour ] ' )
on
axis ;
s e t ( gca ,
axis ([0
t f /60
V( 3 ) 1 0
V( 4 ) 1 . 1 ] )
0:2:24)
61
62
subplot (212);
63
plot (t ,
64
hold
x(4 ,
:) ,
65
plot (t ,
r e p m a t ( Gbsc ,
66
plot (t ,
r e p m a t ( z m i n+z s ,
length ( t )) ,
'k ' ,
2);
67
plot (t ,
r e p m a t ( zmax+z s ,
length ( t )) ,
'k ' ,
2);
68
xlabel ( ' t
ylabel ( ' z( t )
70
72
V =
axis ;
s e t ( gca ,
length ( t )) ,
'r ' ,
[ hour ] ' )
69
71
'b ' ) ;
on
[ mg/ dL ] ' ) ;
' Setpoint ' , ' Location ' ,
axis ([0
' XTick ' ,
t f /60
V( 3 ) 0 . 9
0:2:24)
73
74
75
%F o r
investigation
76
st
0.5;
77
% en
2.5;
78
figure
79
subplot (211);
80
plot ( t ,
81
ylabel ( 'u( t )
82
xlabel ( ' t
u,
'b ' )
[mU/ min ] ' ) ;
[ hour ] ' )
V( 4 ) 1 . 1 ] )
1);
B.15 InvestSampling.m
hold
77
83
84
% V =
axis ;
on
axis ([ st
en
85
% %s e t ( g c a ,
0:1:24)
86
87
88
plot ( t ,
89
hold
90
plot ( t ,
91
xlabel ( ' t
92
ylabel ( ' z( t )
93
94
% V =
V( 3 ) 1 0
V( 4 ) 1 . 1 ] )
subplot (212);
x(4 ,
:) ,
'b ' ) ;
on
r e p m a t ( Gbsc ,
length ( t )) ,
'r ' ,
1);
[ hour ] ' )
axis ;
[ mg/ dL ] ' ) ;
axis ([ st
V( 3 ) 0 . 9
en
V( 4 ) 1 . 1 ] )
95
96
97
98
99
100
% y
vs
figure
plot (t ,
hold
x(4 ,
:) , 'b')
on
1 ) , y ( 1 : end 1 ) , ' r
101
p l o t ( t ( 1 : end
102
103
xlabel ( ' t
')
[ hour ] ' )
B.15 InvestSampling.m
1
%I n v e s t S a m p l i n g .m
%M a t i a s
%T e c h n i c a l
%S p r i n g
%B . S c .
Srensen
s042300
University
of
and
Simon
Kristiansen
s042264
Denmark
2007
Thesis
%F i l e
Model
Predictive
Control
for
an
Artificial
Pancreas
description :
10
11
%S c e n a r i o
12
%and
13
%r e s u l t i n g
14
15
the
where
sampling
graphs .
stepchange
time
See
16
17
clear
all
18
close
all
19
20
p =
21
P1 = p ( 1 ) ;
DesignParameters ( ) ;
22
P2 = p ( 2 ) ;
23
P3 = p ( 3 ) ;
24
n = p(4);
25
VI = p ( 5 ) ;
26
Rutln
= p(6);
27
alpha
= p(7);
28
Gb = p ( 8 ) ;
29
Xb = p ( 9 ) ;
30
Ib
31
Gbsc = p ( 1 1 ) ;
= p(10);
32
Db = p ( 1 2 ) ;
33
34
t0
35
Ts =
36
tf
37
38
=
=
=
0.0;
1;
6024;
t 0 : Ts : t f ;
is
in
either
investigated
thesis
for
or
by
is
looking
specified ,
at
the
details .
78
Matlab programs
39
xs
40
us
= n I b VI ;
[ Gb ;
0;
Ib ;
42
x0
xs ;
43
u0 =
us ;
Gbsc ;
0];
41
44
45
N =
length ( t ) ;
47
u =
z e r o s ( 1 ,N ) ;
48
u ( 1 : end )
49
u ( 6 0 : end )
46
=
us ;
us
10;
50
51
x =
52
x(: ,
z e r o s ( 5 ,N ) ;
1)
x0 ;
53
54
55
mealsv
%m e a l s v ( 1 : 6 0 )
0;
56
57
for
k =
58
1:
N1
display (k );
59
60
x(5 ,k)
= x(5 ,k)
[ T,
mealsv ( k ) ;
61
62
X]
o d e 1 5 s ( @BergmanMinimalModel , [ t ( k )
t (k +1)] , x ( : , k ) , [ ] , p , u(k ) ) ;
63
64
65
x(: ,
k +1) = X( end ,
:) ';
end
66
67
figure
68
en
69
860;
/60;
70
71
p l o t ( t ( 1 : en ) ,
72
ylabel ( ' z( t )
73
xlabel ( ' t
74
hold
75
s e t ( gca ,
x(4 ,
1 : en ) ,
'b ' ) ;
[ mg/ dL ] ' ) ;
[ hour ] ' ) ;
on
' XTick ' ,
0 : 1 : en / 6 0 )
76
77
figure
78
s t a i r s ( t ( 1 : en ) ,
79
xlabel ( ' t
80
y l a b e l ( 'D( t )
81
s e t ( gca ,
0:1:24)
82
s e t ( gca ,
[ ] ) ;
83
V =
m e a l s v ( 1 : en ) )
[ hour ] ' ) ;
axis ;
a x i s ( [ V( 1 )
V( 2 )
V( 3 )
0.5
V( 4 ) + 0 . 5 ] )
V( 3 )
0.5
V( 4 ) + 0 . 5 ] )
84
85
figure
86
s t a i r s ( t ( 1 : en ) ,
87
xlabel ( ' t
88
s e t ( gca ,
0:1:24)
89
s e t ( gca ,
[ ] ) ;
90
V =
u ( 1 : en ) )
[ hour ] ' ) ;
axis ;
a x i s ( [ V( 1 )
V( 2 )
B.16 InvestWeights.m
1
%I n v e s t W e i g h t s .m
%M a t i a s
%T e c h n i c a l
%S p r i n g
%B . S c .
Srensen
s042300
University
of
and
Simon
Kristiansen
s042264
Denmark
2007
Thesis
Model
Predictive
Control
for
an
Artificial
Pancreas
B.16 InvestWeights.m
%F i l e
79
description :
10
11
%W e i g h t
12
13
matrix
varied .
14
15
clear
all
16
close
all
17
18
%Time
19
t0
0.0;
%[ min ]
Start
20
Ts =
3;
%[ min ]
Sampling
21
tf
6024;
%[ min ]
End
22
=
=
( t 0 : Ts : t f ) ' ;
%Time
time
vector
23
24
%S i z e
25
N =
of
horizon
25;
26
27
28
%C o n s t r a i n t s
big
( bequette
article )
10^10;
29
% umin
0;
umax
30
% udmin =
16.7;
udmax =
100;
16.7;
31
% zmin
60;
zmax
180;
33
umin
umax
big ;
udmin =
udmax =
big ;
35
zmin
b i g
b i g
b i g
34
zmax
big ;
32
=
=
36
37
%M e a l s
38
mealsv
( disturbance )
39
m e a l s v ( 1 6 0 / Ts )
zeros (1 , length ( t ) ) ;
=
10;
40
41
%N o i s e
42
noisefact
43
seed
0;
%S t a n d a r d
3501;
%C h o i c e
deviation
for
of
noise
randn
44
45
%Use
46
feed
feedforward ?
=
1;
%1 =
yes ,
0 = no
47
48
%W e i g h t
matrices
49
Q =
eye ( 1 ) ;
50
S =
e y e ( 1 ) 1 e 1;
51
Seta
eye ( 1 ) ;
52
seta
eye ( 1 ) ;
53
54
%S t a r t
55
MPCControl ( t ,
56
MPC
Ts ,
tf ,
mealsv ,
umin ,
umax ,
noisefact ,
Q,
udmin ,
S,
udmax ,
Seta ,
seta ,
zmin ,
N,
zmax ,
seed ,
. . .
feed )
80
Matlab programs
Bibliography
[1] Jan M. Maciejowski:
2001
A Critical Assessment of Algorithms and Challenges in the Development of a Closed-Loop Articial Pancreas, Dia-
et al.: Modeling Insulin Action for Development of a ClosedLoop Articial Pancreas, Diabetes Technology & Therapeutics, vol. 7,
num. 1, 2005
[6] Nakhle H. Asmar: