You are on page 1of 91

# Model Predictive Control

B.Sc. Thesis

## Technical University of Denmark

Kongens Lyngby 2007

## Technical University of Denmark

Informatics and Mathematical Modelling
Building 321, DK-2800 Kongens Lyngby, Denmark
Phone +45 45253351, Fax +45 45882673
reception@imm.dtu.dk
www.imm.dtu.dk
Supervisor: John Bagterp Jrgensen, IMM

Summary

This thesis deals with linear Model Predictive Control, MPC, with the goal of
making a controller for an articial 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 dierential 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.

## These two approaches will make

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 dierent phases:

ATLAB. This is

## user calls a le, where the wanted test scenario is initialized.

The

This includes

properties such as constraints and meal disturbances. After all the needed parameters are specied, the MPC controller is designed, and the simulation is
completed. Finally the simulation is evalutated by various plots.
This implementation is used on a modied version of Bergmans minimal model
in Chapter 5. This model consists of ve dierential 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

## begrnsninger p insulin input og input-hastigheden, hvilket srger for der ikke

bliver injiceret for meget insulin, og/eller dette foregr for hurtigt.
P systemets output, patientens blodsukker niveau, bruges der blde begrnsninger, hvilket betyder at grnserne kan blive krydset, for at sikre at kontrol
problemet ikke bliver ulseligt. Dette betyder at der er en risiko for at patientens blodsukker niveau ikke ligger p et sundt niveau i en kortere periode, men
som det vil blive vist, sker dette kun i ekstreme tilflde.
Kontrolleren hndterer forstyrelser i systemet vha.

feedforward og feedback.

## Feedback fr kontrol algoritmen til at evaluere det mlte output og dermed

basere det kommende insulin input p dette, mens feedforward kan komme
fremtidige forstyrrelser, ssom indtagelse af mad og drikke, i forkbet. Sdan
hndtering af forstyrrelser gr at systemet bliver mere robust og hurtigt reagerende.
I simuleringerne bliver patienten udsat for forskellige scenarier med forskellige
strrelser mltider.

## Her bliver kontrollerens ydelse testet, og det viser sig at

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

## Continuous-Discrete Time Conversion

. . . . . . . . . . . . . . .

2.2

Summary

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

## Model Predictive Control

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

## Constraints on input rate of movement . . . . . . . . . . .

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

## Case study - A minimal model

35

5.1

Linearization

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

38

5.2

MPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

39

5.2.1

Weight matrices

39

5.2.2

5.2.3

Noise

5.3

## Soft output constraints

. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .

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

## people with diabetes in the world.

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

## the body, so the energy in the food can be

used.
Figure 1.1 illustrates how the pancreas works
in a healthy body.
When there isn't produced any insulin, the
glucose is accumulated in the blood, which
can cause serious damage.

Therefore peo-

## ple with diabetes needs to have an external

source of insulin, which means injections into
Figure

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.

## If the patient gets too much

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

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 specic case.

The controller

## calculates the optimal quantity of insulin based on measurements of the blood

sugar on the subcutaneous layer. In theory, MPC could be used to make a device, which could act as an articial pancreas, reducing the impact the disease
has on the patients life.

## Figure 1.2: The insulin pump

Chapter

2
Linearization

This chapter will show how to convert a model consisting of rst order dierential 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 dierential equations in continuous time is denoted by;
dx(t)
dt

= x(t)

(2.1)

## The rst step is to identify an equilibrium point, a steady-state,

i.e.

f (xs , us , ds ) = 0.

(xs , us , ds ),

x(t)

## = f (x(t), u(t), d(t))

f
f
(x(t) xs ) +
(u(t) us )
' f (xs , us , ds ) +
x (xs ,us ,ds )
u (xs ,us ,ds )

f
+
(d(t) ds )
d (xs ,us ,ds )
(u(t) us ) + E
(d(t) ds ) ,
= A (x(t) xs ) + B
(2.2)
where;

,
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 ,

## X(t) = x(t) xs , U (t) = u(t) us

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)

## This is a system of linear time invariant dierential equations.

The solution to this system is given in [7];

(s) + ED(s)

ds
eA(ts) BU

(2.6)

t0
Where

## 2.1 Continuous-Discrete Time Conversion

Having the linearized system, the next step is to convert it from continuous
time to discrete time. Considering equation (2.6), and let

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)

## tk be the time dened as tk = t0 + kTs and let x(tk ) = xk , u(tk ) = uk

d(tk ) = dk , for t tk < tk+1 , be the states at time tk .

Let

and

Then the evolution of the states is governed by the dierence equation, as above;

## x(tk+1 ) = A x(tk ) + B u(tk ) + E d(tk ) xk+1 = A xk + B uk + E dk

(2.10)
Over a time-interval, from 1 to

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
.
.
.

= Ak x s +

k1
X

k1
X


Ak1j B uj +
Ak1j E dj

j=0

(2.11)

j=0

## as the measurable states, the output in

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

## These parameters will be of use in later sections.

The system can be set up as a linear discrete-time state-space model of the
form;

## Linearized discrete time state-space model

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 dierential equations in continuous time, into a linear model in discrete time. This
process is summarized below.

Linearization of model,
- Given a system of dierential equations,

x(t)

(xs , us , ds ).

= f
, B
u

- Calculate

= f
E
d

A =

f
x

and

(t) + ED(t)

X(t)
= AX(t)
+ BU
,

- Calculate

A = eATs ,

B=

## - Model in discrete time is

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

## Model Predictive Control

Linear Model Predictive Control will now be introduced.
Figur 3.1 illustrates the basic idea for the MPC.

Where

as possible.

u,

The controller

r,

## chapter, for instance by adding disturbance and dierent sorts of constraints. It

will also be shown how these extended control problems can be formulated as a
Quadratic Programming (QP) problem, since QP problems are easily solved by
known algorithms.

## 3.1 Unconstrained MPC

In this section focus will be on the basic control model, the unconstrained MPC.
The goal of the controller is, as mentioned, to make the dierence between the
output,

zk ,

## and the reference,

rk ,

as small as possible.

1X
||zk rk ||2Qy
2
k=0

(3.1)

10

Since

## Model Predictive Control

z0

1
2
2 ||z0 r0 ||Qz is discarded. This gives the

(3.2)

## Basic control problem

min z =
s.t.

1
2

N
X

||zk rk ||2Qz

k=1

k = 0, 1, . . . , N 1
k = 0, 1, . . . , N

zk = Cxk ,

zk = CAk x0 +

k1
X

Hkj uj +

k1
X

Hkj,d dj

j=0

j=0

For now the term

zdj

(3.3)

## zk = zx0 + zuj = CAk x0 +

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

## the objective function can be written as;

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

## Plugging equation (3.7) into (3.9) gives;

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)

## To make this problem easier to solve, it is convenient to express it as a QP

problem;

1
2
||U b||Qz
2
1
= (U b)T Qz (U b),
by denition
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)

## are given by;

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)

## QP formulation of problem (3.2)

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 inuence 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

where

uk = uk uk1 ;

(3.20)

min = z + u =
s.t.

1
2

N
X

||zk rk ||2Qz +

k=1

## xk+1 = Axk + Buk ,

zk = Cxk ,

1
2

N
1
X

||uk ||2S

k=0

k = 0, 1, . . . , N 1
k = 0, 1, . . . , N

This new term minimizes the dierence between two consecutive steps in
which gives more smooth input, ie. tries to ensure that steps in

u,

are either

## continously decreasing or continously increasing.

Again this should be formulated as a QP problem. Compared to problem (3.2),
the only dierence is a new term in the objective function, so to formulate as a

13

u =

N 1
1 X
||uk ||2S
2
k=0

1
2
1
2

N
1
X

k=0
N
1
X

## (uk uk1 )T S(uk uk1 )

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 inuence
on the solution to the problem. The new QP problem is;

Like with

z ,

the term

## QP formulation of problem (3.20)

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

## The MPC problem will now be extended by adding disturbance.

Virtually

every system has some disturbance. Figure 3.2 illustrates how the disturbance

## Figure 3.2: MPC with 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

zk = Cxk ,

## As mentioned in Chapter 2.1,

analogous to

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

## The introduction of this new term in

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

## The QP problem is summarized below. Compared to (3.24), the problem has

been expanded by a new term in the gradient.

## QP formulation of problem (3.25)

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 Unconstrained MPC

3.1.3

17

Feed-forward/feedback

## Two important aspects in a MPC controller is feedforward and feedback. They

both react on changes in the process, and can therefore make the controller
better in terms of reaction speed and robustness.
The feedback approach is illustrated on Figure 3.3.

## Figure 3.3: MPC with feedback

Feedback is often used to control the dynamic behavior of the system. When
providing a system with feedback, the measured output,

y,

## tential occurring disturbance, is fed back to the controller.

This ensures that the measured output is approximately the same as the wanted
value, the reference,

r,

## which means the system will be more robust. The draw-

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.

## Figure 3.4: MPC with feedforward

When a system exhibits feedforward behavior, it responds to disturbances which
are predened and therefore known.
This means, that the system can respond more quickly to the disturbance and
the measured output will be more robust.

18

## Model Predictive Control

But meanwhile there is a relatively big chance that the output isn't comparable
to the wanted values, the references,

r,

## since it cannot do much about novel

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 Constrained MPC

The MPC will be extended such that it contains constraints.
The MPC problem has to take the limits of the physical system into consideration. It's infrequent that a system has no boundaries. Constraints are imposed
on the input quantity, the input rate of movement and on the output. For all
constraints it's assumed that, for every timestep

3.2.1

## k , the boundaries are the same.

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

## xk+1 = Axk + Buk + Edk ,

zk = Cxk ,

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

## This yields a constrained QP problem;

QP formulation of problem (3.33)

(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

## Constraints on input rate of movement

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

## xk+1 = Axk + Buk + Edk ,

zk = Cxk ,

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

## can be written as;

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

## this can be written as;

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

## is already given in (3.34), combining with (3.37)

gives;

u0 umax + u1

u0 umax

u0 umin + u1

u0 umin

u0 min(umax , umax + u1 )
u0 max(umin , umin + u1 )

(3.39)

## So the input constraints becomes;

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

## Model Predictive Control

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

## xk+1 = Axk + Buk + Edk ,

zk = Cxk ,
umin uk umax ,
umin uk umax ,
zmin zk zmax ,

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

## from equation (3.26) yields;

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

## Soft output constraints

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)

## MPC with input and soft output constraints

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

## xk+1 = Axk + Buk + Edk ,

zk = Cxk ,
umin uk umax ,
umin uk umax ,
< zk k zmax ,
zmin zk + k < ,
0 k < ,

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,

## constraints. If it's not possible to use hard constraints,

is selected as small as

## possible, so the output constraints are only violated by a minimum.

The approach to formulate (3.46) as a QP problem is a little dierent than
for the previous problems. First

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

## Hessian and gradient for this problem are;


= 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

## should still be imposed.

Formulated in terms of

yields;

Umin

 
 U
Umax
0

(3.53)

## The upper bound on

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

## 3.3 Kalman lter

The basic ideas behind the Kalman lter will now be introduced. Kalman Filter
is used to minimize the impact of noise in the problem. For further and more
detailed information, see [8] and [9].
To make the simulations more realistic, noice is assumed on both process and
output. This noise is denoted

Both

wk

and

vk ,

respectively;

(3.58)

yk = Cxk + vk

(3.59)

wk

and

vk

## mean zero and covariance

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 ).

## Now the Kalman Filter is used to estimate the state

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, renes 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

## Model Predictive Control

3.4 Summary
This chapter derived the MPC problem (3.46) shown on Figure 3.7.

## Figure 3.7: The MPC-model

The goal of the MPC controller is to make the output,
ence,

r,

time-step.
output,
bances,

z,

y.
d.

u,

for each

## Feedback is used, such that the controller analyzes the measured

When feedforward is used, the controller analyzes the future disturFeedback and feedforward can well be used simultaneously.

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,

## when there is an addition of noise on the output measurements and/or the

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

## species the name of

the scene. The idea is that, all parameters the user should have inuence 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.

Bounds on

u.

z.

Weight matrices.

SceneX.m

The timevector

t,

A vector

Ts,

tf.

t0,

## mealsv, which denes at which points in time a meal is consumed.

sigmaw and sigmav, which is used to scale the simulated noise, and the
seed parameter, which denes what set of random numbers to be used.
feed

## determines if feedforward should be used. This will be described in

details later.

4.2 MPCControl.m
This is a wrapper script, which simply calls the three scripts needed for the
dierent 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

## First the function

31

ParameterDesign.m

## Several new variables are initialized;

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 innity.
Starting points for the simulation.
Vectors of lenght N.
Initialized to zero.
System matrices in continous time.

MPCDesign

The vectors

and

u
v

and

## randn command and scaled, so

seed option is used so it is

## they have the desired standard deviation. The

possible to reproduce the same noise vector.

## DesignConstraints.m assigns the constraints for u, u, z ,


T
vectors, i.e. Umin = umin
umin umin , etc.

and

to

## See equations (3.38), (3.40), (3.43), and (3.52).

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

## These are all created by basic

, , d , , Mx0 , Mu1 , MR ,
H

MD , , s .

## turbance, corresponding to equation (3.68).

4.2.2

MPCSimulate.m

Now the actual simulation is started. This consists of a loop which runs over the
timesteps dened in

t.

and measurement

y,

32

Implementation

and

v.

## Meals are simulated by adjusting

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

## Figure 4.2: Modelling meals, change in

On Figure 4.3 is shown how the implementation treats

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

times

## Now comes the actual control step, where the optimal

timestep, this is done in

First

y , u1 , R

MPCCompute.m.

and

D are transformed
x is updated with

MPCCompute.m;

the Kalman gain

Kfx.

## Starting guess for

qpsolver.m, where qpsolver.m has shown
is used as uk . If the solver fails, uk1 is used as uk .
The rst element of U

is updated.

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).

## the Bergman model using the

ode15s.

4.2.3

is found by evaluating

## ATLAB Ordinary Dierential Equation solver

MPCPlot

After the simulation-loop is nished the results are plotted in convenient ways.
The results include the state vector
disturbances

Dm .

x,

Chapter

## Case study - A minimal

model
In this case study, the Bergman minimal model will be used with the implemented MPC.
Based on measurements of the glucose level in the subcutaneous layer, the controller will calculate the optimal amount of insulin to inject into the patient.
To model the patient, a modied version of Bergmans minimal model is used.
As the name implies, this model is small, and is mainly used to test the MPC.
It would be unrealistic to think of it as an accurate model of a real patient.
The model consists of ve dierential equations, (see [2] for further details):
dG

= 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

## Gsc (mg/dL): Glucose concentration on the subcutaneous layer.

approximates G, and is the one which are measurable.
Dm

This state

## (mg/dL/min): Meal glucose disturbance.

and the input is the manipulated insulin infusion rate (U (t), mU/min).
time variable

The

## can be found in Table 5.1.

Gb , Xbr , Ib , Gbsc

and

Dm

## denote the basal values for

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

## Table 5.1: Bergman model parameter values

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)

## Neither these will be investigated during this project.

To use the equations in MPC, let

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

## This model will now be used with MPC.

First step is to linearize the model.

5.1 Linearization
By using the procedure shown in Chapter 2, it's clear that this system of dierential equations can be set up like this;

+ BU
+ ED

X = AX
X, U
D = d ds .
where

In this case,
and

and

(5.11)

## is the state vector,

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)

## By using parameter values from Table 5.1, the matrices

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 .

## cutaneous layer, that is, the state

measureable, the vector

is given by

## this state is selected as it's basal

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 ,

## output constraints, and are kept constant as identity matrices.

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

that

is selceted to

102 ,

## since this values gives some

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

## Horizon and sampling time

An appropriate horizon

N,

Ts ,

Dm ,

and in

## See Appendix B.15 for the implemen-

tation of this.
Figure

5.3

illustrates

what

is

meant

by

stepchange.
The eect of this stepchange on the output

## is seen on Figure 5.4. This Figure shows that,

for the stepchange in

Dm ,

## a new steady-state after approximately three

hours (180 minutes), which is slightly faster
than for the stepchange in

u.

## A general rule-of-thumb says, that one should

4
t [hour]

use a sampling time of about 180 min/12 samples = 15 min. Since computational speed is

Ts = 8

min.

## The horizon should be at least three hours, so

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

## As described in Section 3.3, noise can be simulated on output measurement

and/or the model itself;

## xk+1 = Axk + Buk + Edk + wk x

k+1 = Axk + Buk + Edk
yk = Cxk + vk yk = Cxk
where

and

## are both assumed to be Gaussian distributed;

w N (0, Qw )
v N (0, Rv )
These vectors of noise are created in

## ATLAB by use of the

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-

## it's possible to reproduce the same random vectors of

noise, and thereby produce simulations with the same noise vectors scaled differently.
By experiments,


w = 0.7

and

103

v
0.4

## are chosen to be;

0.8


0 ,

v = 0.8

(5.18)

These have shown themselfs to give simulations with a reasonable level of noise.

44

## Case study - A minimal model

5.3 Simulations
The performance of the controller will now be tested with three dierent scenarios. In the rst test scenario our virtual patient is given three regular meals.
This is the main test scenario, and here the eect 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

Time (hours)

D(t)

(mg/dL/min)

12

18

10

feedforward.

5.3 Simulations

5.3.1.1

45

Feedforward

## In this section the

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

## Figure 5.6: Insulin input and output without 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]

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

## Using feedforward, the controller knows when there is going to be an increase

in the glucose value, because of meal consumption. That means that before the
consumptions are made, the controller injects some insulin. The glucose level is
held on a lower value than without feedforward and therefore it is more likely
for the patient to maintain a heathy blood glucose level.
From now, on all simulations are done with feedforward.

48

## Case study - A minimal model

5.3.1.2

Measurement noise

## As mentioned in Section 5.2.3, the system can be simulated with measurement

noise and/or process noise. In this section only the measurement noise, noise
on

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 dierence 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

## Figure 5.10: Insulin input and output with measurement noise

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

## Case study - A minimal model

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

## is compared to the real 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

## Figure 5.13: Insulin input and output with process noise

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

## Measurement and process noise

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

## Case study - A minimal model

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

## Table 5.3: Scenario II meals disturbances

Figures 5.18 and 5.19 illustrates the states and the input/output respectively.

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

## Figure 5.19: Insulin input and output

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

## the lower bound is violated for a very short

period of time. This is the eect of soft output constraints, and is to be expected
when the ingested meals are big.

56

5.3.3

Scenario III

Time (hours)

D(t)

(mg/dL/min)

10

12

13

14

15

16

18

13

20

21

22

## Table 5.4: Scenario III meals disturbances

Again, Figures 5.20 and 5.21, illustrates the states and the input/output

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

## Figure 5.21: Insulin input and output

Again the controller performs good, but, like with Scenario II, the lower boundary on the output is violated briey.

58

## Case study - A minimal model

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 denitely 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.

## Though it should be kept in mind, that if a controller

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 denite conclusion. Another reason why MPC wouldn't be used is seen on
Figure 5.19 for Scenario II at

t 20,

## where the the eect of soft constraints

reveals itself. The amount of blood glucose slightly violates it's lower boundary,
which is potentially disastrous for the patient.

## On the other hand, soft con-

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 a vector of measured outputs.

is neglected from the equation, due the fact that we only want make an

x0 = 0 and
that u is an

input at,
vector

t0 ,

such

integer,

u0 6= 0

and

uk = 0

for

k 1.

I.e the


u = u0

T

(A.3)

xk ,

uk .

zk ,

## at the dierent states,

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)

## the previous chapters, and the impuls response sequence is indicated by

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

## Table A.1: Relation between the dierent sequences

k = 0 . . . N.
Step responce is then the summerize of all the values of

## I.e. the step response can be dened as;

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

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)

## and the step response, S,

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

## ATLAB scripts used.

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

## randn ( ' seed ' ,

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

## randn ( ' seed ' ,

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

## [ Hbar , Gamma, Gammad , P h i , Mx0 , Mum1,MR,MD, Lambda , b a r s e t a ]

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

e y e (m,m ) ;

26

Ae =

[A B;

27

Be =

[B;

z e r o s (m, n )

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

## [ barH , Gamma, Gammad , P h i , Mx0 , Mum1,MR,MD, Lambda ,

D e s i g n M P C M a t r i c e s ( A , B , E , Cz , Qz , S , N , S e t a , s e t a )

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

27

Phi

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

## QZ( k z + 1 : k z+nz , k z + 1 : k z+n z )

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 ;

## HS ( ku+nu + 1 : ku+2 nu , ku + 1 : ku+nu )

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

## % Form H , Mx0 ,MR,MD

83

T = Gamma' QZ ;

84

H = TGamma + HS ;

85

H =

86

Mx0 = T P h i ;

(H+H ' ) / 2 ;

T ;

87

MR =

88

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

## display ( ' Fejl

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 ]

## = MPCCompute ( y ( k ) , un , R , D, Zmin , Zmax ,

. . .

61

z s , z s , u s , d s , xp , u b a r ,

62

## Hbar , Mx0 , Mum1,MR,MD, b a r s e t a ,

63

Phi ,

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

## display ( ' Error

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 ]

## = MPCCompute ( y , um1 , R , D, Zmin , Zmax ,

. . .

16

y s , z s , u s , d s , xp , u b a r ,

17

## Hbar , Mx0 , Mum1,MR,MD, s b a r e t a ,

18

Phi ,

19

umin ,

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

+ 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 )

## = max ( umin , dumin+dum1 ) ;

45

ubarmax ( 1 : nu , 1 )

## = min ( umax , dumax+dum1 ) ;

46
47
48

%C r e a t e
c

bounds

Phi x0

for

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

## [ u b a r , i n f o ]= q p s o l v e r ( Hbar , g b a r , u b a r m i n , ubarmax , Abar , bmin , bmax , u b a r i n i t ) ;

65
66

if

info

67

== 0

du0 =

68

u b a r ( 1 : nu , 1 ) ;

else

69

du0 = dum1 ;

70

end
%

73

74

75

76

77

78

79

80

81

82

83

du0 =

84

info

85

86

87

88

89

71
72

## Abar2 = [ Abar ; Abar ] ;

b b a r = [ bmax ; bmin ] ;
[ u b a r , f e v a l , EXITFLAG ]

. . .

## q u a d p r o g ( Hbar , g b a r , Abar2 , b b a r , [ ] , [ ] , u b a r m i n , ubarmax , u b a r i n i t ) ;

if

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

## model ' ' ,

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

## l e g e n d ( ' G' , 'G_{ s c } ' , ' L o c a t i o n ' ,

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

## ' XTick ' ,

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

## ' XTick ' ,

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

## ' XTick ' ,

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 ' ,

## ' linewidth ' ,

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

## ' NorthWest ' ) ;

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 ' ,

## ' linewidth ' ,

2);

67

plot (t ,

r e p m a t ( zmax+z s ,

length ( t )) ,

'k ' ,

## ' linewidth ' ,

2);

68

xlabel ( ' t

ylabel ( ' z( t )

70

72

V =

axis ;

s e t ( gca ,

length ( t )) ,

'r ' ,

## ' linewidth ' ,

[ 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 ] ' )

## ' NorthWest ' ) ;

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 ,

## ' XTick ' ,

0:1:24)

86

87

88

plot ( t ,

89

hold

90

plot ( t ,

91

xlabel ( ' t

92

ylabel ( ' z( t )

93

## legend ( ' z ( t ) ' , ' Constrains ' , ' Location ' ,

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

## ' NorthEast ' ) ;

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

## legend ( ' z ' , ' y ' )

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

## zeros ( length ( t ))4;

%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 ,

## ' YTick ' ,

[ ] ) ;

83

V =

m e a l s v ( 1 : en ) )

[ hour ] ' ) ;

axis ;

## [ mg/ dL / min ] ' ) ;

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 ,

## ' YTick ' ,

[ ] ) ;

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

## Model Predictive Control of

Blood Glucose in Type 1 Diabetics Using Subcutaneous Glucose Measurements, Rensselaer Polytechnic Institute, 2002

## et al.: Hypoglycemia Prediction and Detection Using

Optimal Estimation, Diabetes Technology & Therapeutics, vol. 7, num.

## [3] Cesar C. Palerm

1, 2005

A Critical Assessment of Algorithms and Challenges in the Development of a Closed-Loop Articial Pancreas, Dia-

## betes Technology & Therapeutics, vol. 7, num. 1, 2005

et al.: Modeling Insulin Action for Development of a ClosedLoop Articial Pancreas, Diabetes Technology & Therapeutics, vol. 7,

## [5] H.M. Steil

num. 1, 2005
[6] Nakhle H. Asmar:

## tice Hall, 2004

[7] John B. Jrgensen, Sten B. Jrgensen:

## nical University of Denmark, 1998

[8] Greg Welch, Gary Bishop:

## versity of North Carolina,2006

James B. Rawlings:
Disturbance Models for
Oset-Free Model-Predictive Control, AIChE Journal Vol.49 No.2, 2003