94
Modeling and Simulation of Glucose-Insulin Dynamics Allan Lyngby Lassen, s052408 Thor Sch¨ utt Svane Nielsen, s052970 Kongens Lyngby 2008 IMM-B.Sc.-2008-18

Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

Embed Size (px)

Citation preview

Page 1: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

Modeling and Simulation ofGlucose-Insulin Dynamics

Allan Lyngby Lassen, s052408

Thor Schutt Svane Nielsen, s052970

Kongens Lyngby 2008

IMM-B.Sc.-2008-18

Page 2: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

Technical University of Denmark

Informatics and Mathematical Modelling

Building 321, DK-2800 Kongens Lyngby, Denmark

Phone +45 45253351, Fax +45 45882673

[email protected]

www.imm.dtu.dk

IMM-B.Sc.: ISSN 0909-3192

Page 3: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

Summary

This report is written on basis of an analysis of various mathematical modelsof the human organic system. These models have been developed in order tohelp the understanding of how the blood sugar levels behave in a diabetic person,depending on the time at which the given person eat and when a shot of insulinis taken.

Diabetes is an incurable disease which is segmented into two different types.These two types are known as Diabetes mellitus Type 1 or just type 1 diabetes.This type is also formerly known as childhood-diabetes. The other type is calledDiabetes mellitus Type 2, type 2 diabetes or formerly adult-onset diabetes[1]:

Type 1 is an inherited type of the disease, but it doesn’t necessarily have to showup in childhood. In principle it can be ’activated’ at any point of your life,but most often the symptoms will start in a young age. Unfortunatelythis requires a daily injection of insulin to avoid the side effects.

Type 2 is more of a lifestyle illness which is closely related to obesity, and inactivityin the everyday life. This can normally be held under control with regularexercise and healthy food. In most cases injections are not a necessity.

The common thing for both types is a horrible disease that constantly demandsawareness of your current state of blood sugar. If you are low on blood sugaryou will feel bad and perhaps even have a case of hypoglycemia which can leadto confusion, cramps, unconsciousness or eventually death in the severe cases[2].

Page 4: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

ii

On the other hand if you have a glucose concentration too high over a longerperiod, the so called hyperglycemia can also have serious consequences, such asblindness or gangrene, possibly leading to amputation of arms or legs.

We know that approximately 21 million[4] out of the total population of 290million in the United States, i.e. roughly 7.2% was suffering from diabetes in2005. The side effects and this large ratio of diseased people has a huge impacton the financial sector and of course on the many individuals as well[4]:

In 2002, diabetes was the sixth leading cause of death and had anestimated total cost of $132 billion [in the US].

On a more local scale, here in Denmark we have around 220.000 (≈4%) diseasedpeople and astonishingly enough there are possibly 100.000 people bearing thedisease without knowing it[1].In Denmark 2.5 billion DKR are yearly used on treatment, with the followingdistribution of the money[1]:80% are used on acutal treatment of complications.10% are used on prevention of further development of complications.10% are used on medicin.

There can’t be any doubt that this is a problem that should be taken seriously,both on every diseased individual and also on a more macroscopic financial scale.All in all we have many very good reasons for coming up with a model to helpout the diabetics, and that will be the topic in the chapters to come.

Page 5: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

Resume

As already stated, these numbers show that diabetes is a world wide problemwith a lot of consequences and that – if possible – needs some sort of solution.So basically we need a model in order to minimize the mentioned consequences.A lot of great minds have thought about how to model the human organicsystem in a sensible way and we will try to implement some of these models,and of course test them in some different scenarios. Our agenda will look likethis:

• Diabetes, described shortly

• The ’simple’ model

• The Hovorka model

• The Man-Rizza-Cobelli model

• Implementation in a GUI

First of all we will briefly describe what diabetes is all about. Not in detail, butin such a way that we know what we are dealing with, and why we are doingit. This will lead us onto our first model, which is our own tiny model, thatwill be introduced in a more thorough fashion than the other models; simply tointroduce the reader to the way of thinking in this kind of simulation.

All of these models have at least one thing in common, they are all based on theprinciples of differential equations. Some of them are nice and easily solvable,

Page 6: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

iv

while some of the more advanced models simply can’t be solved analytically.In all cases though, we will use the knowledge of numerical computations andsolve them with use of a computer. When it comes to solve things numericallyMATLAB is usually the first choice for many, because it’s an easy-to-use toolwith lots of pre-implemented commands that are suitable for most tasks. So ofcourse we will use MATLAB as a tool to simulate our models.

As one can see, this report demands a certain knowledge to (numerical) math-ematics specifically on the topic of differential equations. Furthermore it’s ab-solutely necessary to have a good acquaintance of MATLAB. The purpose ofthis report is to be understandable primarily for co-students in such a way thatthey should be able to follow the overall principles used in our work.The main focus will be on the mathematical part, meaning that our goal is notto explain every physiological process in the human body, nor to get the readerto understand diabetes in details for that matter. A basic understanding willease the reading of some chapters, though.

Since we are using a mixture of mathematics and programming we will use acertain notation when referring to things that has to do with MATLAB. TheVerbatim-font will be used like in this example:[T,G]=ode15s(@Hovorka,[t0 t1],x0,odeOptions,u,p);

One could ask the valid question what good a mathematical model would doin order to reduce the discomforts of having diabetes. It’s pretty clear that themodel alone won’t do any difference, but the goal is to end up with reasonablecalculations to an apparatus that gives advice about injection of insulin.However, there is a lot of theory, scientific work and research behind such adevice, and of course usage of scientific areas we don’t know about with ourbackground. So basically it’s not within our plans to make such a device, butwe intend to do the next best thing which is to implement the model(s) in aGraphical User Interface, also with the help of MATLAB.

Before we get so far, we will as promised start out with a short introduction todiabetes in the first chapter.

Page 7: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

v

Page 8: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

vi Contents

Page 9: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

Contents

Summary i

Resume iii

1 Diabetes – a short sum up 1

1.1 Insulin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2 The connection between diabetes and the pancreas . . . . . . . . 2

2 The simplest of models 7

2.1 Derivation of the simple model . . . . . . . . . . . . . . . . . . . 8

2.2 Solution of the simple model . . . . . . . . . . . . . . . . . . . . 9

2.3 Testing the model . . . . . . . . . . . . . . . . . . . . . . . . . . 11

3 Hovorka’s Model 13

3.1 Presentation of the model . . . . . . . . . . . . . . . . . . . . . . 14

3.2 Hovorka put to the test . . . . . . . . . . . . . . . . . . . . . . . 17

Page 10: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

viii CONTENTS

4 Man-Rizza-Cobelli’s Model 23

4.1 Presentation of the model . . . . . . . . . . . . . . . . . . . . . . 24

4.2 Man-Rizza-Cobelli put to the test . . . . . . . . . . . . . . . . . . 30

5 Implementation in a GUI 33

5.1 GUI Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

5.2 Code structuring . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

6 Conclusion 39

A MATLAB code 41

A.1 The simple model . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

A.2 GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

Page 11: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

Chapter 1

Diabetes – a short sum up

Before we go to the mathematics it might be nice to get some further detailson diabetes, and exactly how our calculations are going to be used in the GUI.

As already explained there are two types of diabetes where we in particular willconsider type 1, the one where frequent injections are necessary. We will dealwith this disease in the following two sections. One chapter about the hormonecalled insulin which is essential in the issue with diabetes. Lastly we’ll use asection to explain the relation between the pancreas and blood sugar.

1.1 Insulin

Insulin is a hormone which is produced in the pancreas, and which controlssome important physiological processes in the body. However, if a person issuffering from type 1 diabetes the immune system consider the pancreas as anunfamiliar object and kills the cells which produce insulin. Logically, if theinsulin production is set out of control it has to be supplied ’manually’ throughinjections on a daily basis.

Page 12: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

2 Diabetes – a short sum up

However, a diabetic will quickly experience that it’s not as simple as taking ashot at a given time of day. If the diabetic person shouldn’t risk going intohypoglycemia and at the same time avoid high levels of blood sugar, it’s quitea puzzle to do it just right.

Figure 1.1: Crystals of insulin.

1.2 The connection between diabetes and thepancreas

It should by now be clear that the insulin is produced in the pancreas. Butthe functions of the pancreas are actually quite complex, and hard to replicatemechanically.In a healthy individual the ’task’ of the physiological pancreas is to maintainthe blood glucose level in a reasonable area.

Figure 1.2: The physiological pancreas.

Page 13: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

1.2 The connection between diabetes and the pancreas 3

As explained[5, 6] in figure 1.2 above, the pancreas will adjust the blood sugarin two ways. If there is too low concentration of sugar in the blood the alphacells in the pancreas will release the hormone called glucagon which will preparethe liver to emit sugar into the blood, and thereby raise the concentration to anormal level.On the other hand if the concentration is high, the beta cells in the pancreaswill release insulin. This process makes the fat cells capable of taking in thesugar in the blood, and consequently lowering the concentration.One can imagine what will happen if the pancreas is not fully functional. Inthe first case of low blood sugar it’s possible to do something yourself simplyby eating something sugary, but of course it’s not possible for the pancreas tosupply the body with the sufficient insulin, and therefore you have to keep somesort of schedule of when to inject it.

Figure 1.3: A person measuring his blood.

In order to measure the current level of blood sugar, you have to take a sampleof your blood and determine the value by using a special device. Diabeticshave to be more or less constantly aware of the current concentration of bloodglucose. The following table of approximate values is giving an idea of how tointerpret the value that comes out of the device designed to make this kind ofmeasurement.

mmol/l mg/dl interpretation

------ ----- -----------------------------------------------2.0 35 extremely low, danger of unconsciousness3.0 55 low, marginal insulin reaction

4.0 75 slightly low, first symptoms of lethargy etc.5.5 100 mecca

5-6 90-110 normal preprandial in non-diabetics8.0 150 normal postprandial in non-diabetics

10.0 180 maximum postprandial in non-diabetics11.0 20015.0 270 a little high to very high depending on patient

16.5 30020.0 360 getting up there

22.0 400 max mg/dl for some meters and strips33.0 600 high danger of severe electrolyte imbalance

----- ----- -----------------------------------------------

Preprandial = before meal, postprandial = after meal.

Page 14: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

4 Diabetes – a short sum up

We have tried to visualize this table, in order to get a better understanding ofhow little it actually takes to reach a hazardous concentration. The white arearepresents the optimal state, and the black level is considered dangerous. Inthe rest of the report we will use 70-130 mg/dL as the ’good interval’, that thediabetic should strive to be in.

mg/

dL

100

200

300

400

500

600

Figure 1.4: Color scale of glucose levels, from white/good to black/bad

According to this table, it’s clear that the gap between optimal state and hypo-glycemia is very small. Since hypoglycemia definitely is the worst state to be in,it should be obvious why many diabetics prefer to ”overdose” themselves withsugar. This is one of the reasons why it’s so hard to dose in the right amount;even a small miscalculation could in principal have unpleasant consequences.

1.2.1 Converting between mmol/L and mg/dL

As we saw in the table before, the blood sugar level is usually measured in eithermmol/L or mg/dL dependent on where you are in the world. At this moment wewould like to state the difference between these two units, since we are using oneof them in the calculations and the other when displaying the results. Assumingthat blood sugar consists only of carbohydrates C6H12O6 or CHO, makes thecalculations look like this

C6H12O6 ⇒

C : 6 · 12g/molH : 12 · 1g/molO : 6 · 16g/mol

= 180g/mol = 180000mg/mol.

We can now find the ratio between the two units going step by step

mmol/L ∼1

1000mol/L ⇒

1

1000mol/L · 180000mg/mol = 180mg/L.

We now just need the last unit conversion from L to dL

180mg/L ·1

10L/dL = 18mg/dL.

Page 15: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

1.2 The connection between diabetes and the pancreas 5

So the conclusion is that the following ratio exists when looking at CHO

1mmol/L ∼ 18mg/dL.

And this result concludes the chapter about diabetes. We now have the sufficientbasic knowledge about diabetes, in order to produce the first mathematicalmodel. This model should give the idea of how the mathematics works anddemonstrate the assumptions we are working under.

Page 16: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

6 Diabetes – a short sum up

Page 17: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

Chapter 2

The simplest of models

The purpose of this model is too illustrate how the process of modeling somekind of biological happening, can look like. In this case we look at how theabsorption of glucose will distribute itself through the body, from the amounteaten to the amount going out again.Let’s start out with considering this very simple model consisting of two com-partments that should represent the stomach and the colon respectively.

D(t)D1(t) D2(t)

UG(t)

Figure 2.1: Principle of two-compartment system.

Page 18: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

8 The simplest of models

2.1 Derivation of the simple model

All of the differential equations rely primarily on the principle of conservation ofmass and indirectly of the conservation of energy. So the accumulated glucosemust necessarily equal what goes in minus what goes out, otherwise some of theglucose should just have disappeared by itself and of course that’s impossible

ACC = IN − OUT.

Looking at the two-compartment system in figure 2.1, we can model the accu-mulated mass in the respective compartments separately. So let’s start by look-ing at the first compartment, as mentioned before the balance equation is

ACC1 = IN1 − OUT1.

What goes in is of course the amount of CHO (glucose) D(t) that the person eatsover time. Out of this quantity it’s only a certain part that is actually obtainedin the body, we use the symbol AG for this percentage (normally 80%). We nowhave

IN1 = AGD(t)∆t.

The amount that goes out must be the glucose level in the compartment D1(t)over time, distributed over the total time τ it takes the glucose to flow throughthe system

OUT1 =D1(t)

τ∆t.

At last there is the accumulated amount. If we look at a time window of ∆t,then the amount in the compartment after that time window minus the amountin the beginning of that time window, must be the accumulated glucose. Thiscan mathematically be formulated as

ACC1 = D1(t + ∆t) − D1(t).

We now gather all the expressions in the balance equation

D1(t + ∆t) − D1(t) = AGD(t)∆t −D1(t)

τ∆t ⇔

D1(t + ∆t) − D1(t)

∆t= AGD(t) −

D1(t)

τ.

Next we let the time window be infinitesimal small, that is we let ∆t → 0,because now the left hand side can be recognized as the differential quotient.This gives us the differential equation for the first compartment

dD1(t)

dt= AGD(t) −

D1(t)

τ. (2.1)

Page 19: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

2.2 Solution of the simple model 9

Since its a two-compartment system there is a differential equation for eachcompartment, and to find the next one we use exactly the same approach asbefore with the mass balance equation

ACC2 = IN2 − OUT2.

It is obvious that what enters the second compartment, must be what goes outof the first, so

IN2 = OUT1 =D1(t)

τ∆t.

The amount of CHO, that in the end goes through the system, is denoted bythe function UG(t) which is defined as

OUT2 = UG(t)∆t =D2(t)

τ∆t.

Again we collect all the expressions in the mass balance equation and do thesame tricks as before

D2(t + ∆t) − D2(t) =D1(t)

τ∆t −

D2(t)

τ∆t ⇔

D2(t + ∆t) − D2(t)

∆t=

D1(t)

τ−

D2(t)

τ⇒ ∆t → 0

dD2(t)

dt=

D1(t)

τ−

D2(t)

τ.

This concludes the derivation of the interdependent differential equations for thetwo-compartment model, which in this context is a Glucose Absorption Model- very useful for later use. As a matter of form let’s just sum-up the equations

dD1(t)

dt= AGD(t) −

D1(t)

τ(2.2)

dD2(t)

dt=

D1(t)

τ− UG(t) (2.3)

where D(t) is the CHO eating rate [mg/min] and UG(t) = D2(t)τ is the blood

glucose absorption rate [mg/min].

2.2 Solution of the simple model

Before we go on with our test cases, we first want to calculate analytically thesolution to the second differential equation (2.3) from the derived system.

Page 20: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

10 The simplest of models

This is because it will give us important insight to how the solution will behave.We start by rewriting the equation to the following

dD2(t)

dt+

1

τD2(t) =

1

τD1(t).

To have it on this form, one can easily see the solution by using the formula forlinear first order differential equations. The general solution is

D2(t) = D1(t) + Ke−t/τ .

We want an expression without the arbitrary constant K, so therefore we lookat the initial state, i.e. t = 0 where we put D2(0) ≡ D0 and D1(0) ≡ Din

D2(0) = Din + K = D0 ⇔ K = D0 − Din ⇒

D2(t) = Din + (D0 − Din)e−t/τ ⇔

D2(t) = D0e−t/τ + Din(1 − e−t/τ ).

So now we have found the solution to the system and it’s not surprising that it’sexponential, since its very often is represented in nature. If we try to analyze alittle bit on this solution, we see that - reasonably assuming that τ > 0

D2(t) → Din for t → ∞.

As time goes towards infinity, the solution will find a steady state of the amountthat was initially put in the compartment.Next we can look at how many periods of τ is needed, before the solutionhas reached 95% of its steady state value. To make the calculations easier weassume that at t = 0 the compartment is empty, i.e. D0 = 0, and to give aclearer understanding we set Din = 1. Let’s look at the 4 first periods

D2(τ) = 1 − e−1 = 0.632

D2(2τ) = 1 − e−2 = 0.865

D2(3τ) = 1 − e−3 = 0.950

D2(4τ) = 1 − e−4 = 0.982.

The conclusion to this is that we should expect, after a time window of 3τ ,that the solution has reached 95% of its steady state. Of course we shouldremember that this is a two-compartment model and that Din isn’t a constant,but a function that controls how much goes out of the first compartment. Sodepending on how the persons eating pattern, the glucose absorption rate candiffer widely from each scenario.In the next section we test two very different cases, when the person gets a mealimpulse response and when he eats continuously.

Page 21: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

2.3 Testing the model 11

2.3 Testing the model

We have now calculated the analytic solution, and have come to solve it num-erically. In order to solve it we use the built-in ode15s solver, that is used to solvestiff problems. Stiff problems are characterized to be differential equations thathave a numerically unstable solution, unless almost infinitely small incrementsin time are used. This solver will be used in all problems to come.

2.3.1 One meal - impulse response

We now implement the system of differential equations derived before and tryto model the course of the blood glucose absorption rate, when a person eatsa meal. In this first test we simulate that the person gets the amount of CHOlike an impulse, i.e. the meal is being eaten instantly, so to speak.In this test case we have chosen to feed the person 100mg of CHO, for which ittakes 40 min to get through the system. The graph is pictured below.

0 100 200 300 4000

0.1

0.2

0.3

0.4

0.5

0.6

0.7

Glucose Absorption Rate − impulse response

time [min]

UG

[mg/

min

]

Figure 2.2: The CHO absorption rate under an impulse response.

The graph shows that after 40 minutes the CHO absorption rate is at its peak,which of course is the result of setting the parameter τ = 40. After that theabsorption level exponentially decays. We see the graph is consistent with whatto expect of the result, since the person only eats at t = 0 and doesn’t consumeanything after the 40 minutes it takes for the sugar to go through the system.Naturally since Din = 0 we see the solution going asymptotically towards zero.

Page 22: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

12 The simplest of models

2.3.2 One meal - continuous eating

In this next scenario we try to simulate a person that has an empty stomach(meaning haven’t consumed any CHO at t = 0), but eats glucose at a rate of1mg/min. We still use the parameter τ = 40. This graph is depicted below.

0 100 200 300 4000

0.1

0.2

0.3

0.4

0.5

0.6

0.7

Glucose Absorption Rate − continuous eating

time [min]

UG

[mg/

min

]

Figure 2.3: The CHO absorption rate with continuous eating.

Since the person gets a constantly flow of 0.8mg/min (remember that its only80% of the CHO eaten that is actually absorbed by the body), then the solutionwill exponentially approach this value. One can also see that after around 5τthe solution has founded its steady state, precisely as expected.

We have now made some analysis of the simple model, and have gained someknowledge of how a model is built up. So basically we are now ready to takeon some more realistic and interesting models. First off, we try the relativelysimple Hovorka model.

Page 23: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

Chapter 3

Hovorka’s Model

With the introduction of the simple model, we saw some of the principlesthat are used when a model of multiple compartments is deduced. Of course ourmodel lacked most of the scientific principles that are used in the constructionof a more realistic scenario. This process can take a long time with use of testpersons and measurements from various types of medical apparatus. These aresome of the reasons why we are not able to deduce the following models to come,and why we are simply presenting them instead.

The first ’realistic’ model we have chosen to present is developed primarily byRoman Hovorka and is therefore being referenced as Hovorka’s model.

Page 24: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

14 Hovorka’s Model

The main idea of this model compared to our own is to split the compartmentsinto more specific biological processes. By doing this it’s possible to get amore detailed description of the body, and thereby a more precise idea of theinteraction between different (groups of) organs. The ’price’ for doing this, is ofcourse that we get more equations and increased complexity in each equation.We will now have a look at the Hovorka model as it’s described in the article[7],and try to explain the meaning of the different sets of equations.

3.1 Presentation of the model

Similar to the last model, we will start out with a figure that visualizes theconnections between the different compartments.

Figure 3.1: Compartment model of glucose-insulin system

Q1 ∪Q2 represents the glucose subsystem, S1 ∪S2∪ I(t) is the insulin subsystemand finally x1 ∪ x2 ∪ x3 acts as the so called insulin action subsystem. Inresemblance to the approach from the simple model, we will now pin out eachof these compartments, state their individual differential equations and shortlydescribe their significance in the entire system.

Page 25: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

3.1 Presentation of the model 15

3.1.1 The glucose subsystem

First of all we have the collection of equations that takes care of glucose kinetics.Hovorka refers to Q1 as the accessible compartments, i.e. where measurementsare made, and consequently Q2 are the non-accessible

dQ1(t)

dt= −

[

F c01

VGG(t)+ x1(t)

]

Q1(t) + k12Q2(t) − FR + UG(t) + EGP0[1 − x3(t)]

= −(F c01 + FR) − x1(t)Q1(t) + k12Q2(t) + UG(t) + EGP0[1 − x3(t)] (3.1)

dQ2(t)

dt= x1(t)Q1(t) − [k12 + x2(t)]Q2(t). (3.2)

From the first to the second line, i.e. the Q1 equation, we simply make use ofthe definition below, so it can easily be shown that they really are identical.This is purely a matter of taste, but we choose to stick with the simple lookfrom what we call 3.1

G(t) =Q1(t)

VG. (3.3)

This equation from just above is actually quite important, since we use thisto get the actual data we are plotting. To put it shortly, this represents thecurrent blood glucose level and is therefore sort of the key function in all ofthese equations and numbers.A lot of constants have been introduced, and their numeric values can all befound in [7], but we can state here that VG is the distribution volume in theaccessible compartment. EGP is the endogenous glucose production, but as youmay notice we use EGP0 in the model which means that it is extrapolated to azero insulin concentration. k12 is a transfer rate constant, or more specificallythe transfer rate from the non-accessible to the accessible compartment.Most of the described parameters that takes part in the equations above areeither constants based on tests and measurements, but a few of them are morecomplex. In particular we have the total non-insulin-dependent glucose flux(F01), here made into another constant, that’s dependent of the ambient glucoseconcentration

F c01 =

{

F01 G(t) ≥ 4.5mmol/LF01G(t)/4.5 otherwise.

(3.4)

And also this function below, representing the renal glucose clearance. In thismodel, it is however only present when the blood glucose exceeds 9mmol/L

FR =

{

0.003(G(t) − 9)VG G(t) ≥ 9mmol/L0 otherwise.

(3.5)

Page 26: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

16 Hovorka’s Model

3.1.2 The insulin subsystem

Next up we have the insulin subsystem. These two equations cover the insulinabsorption, and even though they are not visible on figure 3.1 they make upa two-compartment system that are interacting with each other. In fact, bylooking at the equations it should be very recognizable when comparing themto the system from chapter 2

dS1(t)

dt= u(t) −

S1(t)

τI(3.6)

dS2(t)

dt=

S1(t)

τI−

S2(t)

τI. (3.7)

In specific u(t) is the administration of short-acting insulin. Here too, we makea definition that looks like this UI(t) = S2(t)/τI , being the insulin absorptionrate. We use this definition straight away in the following equation

dI(t)

dt=

UI(t)

VI− keI(t). (3.8)

The two constants used in this equation are ke, the fractional elimination rateand VI , the insulin distribution volume. Where I(t) is the mathematical des-cription of the plasma insulin concentration.

3.1.3 The insulin action subsystem

The final subsystem in this model consists of three equations that close thesystem. As described in the article, the solutions for each of these equationstakes care of the effects of the insulin on glucose distribution/transport, glucosedisposal and endogenous glucose production

dx1(t)

dt= kb1I(t) − ka1x1(t) (3.9)

dx2(t)

dt= kb2I(t) − ka2x2(t) (3.10)

dx3(t)

dt= kb3I(t) − ka3x3(t). (3.11)

All of the constants are rate constants of deactivation (kai) and activation (kbi)respectively.

Page 27: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

3.2 Hovorka put to the test 17

3.2 Hovorka put to the test

Time has now come to test the Hovorka model, to see if the results are sensible.We will begin with testing a single day in some different scenarios, and hopefullyget some more insight in what it means to be a diabetic, which can finally leadto a better GUI.

3.2.1 Insulin before or after meal

First of all it would be interesting to see at what time it’s best to take yourinsulin relative to the time of your meal, in order to stay in the good interval.We will test this by taking the exact same scenario in two cases, with exceptionof the time for insulin injection. In this case, we would first of all like toknow if it’s best to inject before or after a meal. So here we choose to testtmeal,i−30min and tmeal,i+30min for i = 1, 2, 3 which represents breakfast, lunchand dinner in a normal day. Just as a point of reference we use CHO1 = 45gand CHO2 = CHO3 = 70g, which means that we assume that the amountof carbohydrates eaten for breakfast is 45g, and so on. The insulin doses arealso exactly the same with 2U/L for breakfast and 3U/L for lunch and dinner.Finally we here assume that the duration of a meal is 30 minutes.

Figure 3.2: Insulin taken before and after meal

As it’s obvious to see, there is a tremendous difference in the two graphs. Theblue graph represents the ’before case’ and the black the ’after case’. The beforecase is smooth and inside the green box at all times. From this, it is also clearto see that it’s a lot harder to control the blood sugar properly when you are

Page 28: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

18 Hovorka’s Model

taking insulin after a meal. There is a lot more zigzag behavior, and even twominimum points way below the healthy zone. To get the peaks down in thehealthy area, you would have to take more insulin, but that would also meanthat the dangerously low values would become even lower.From this point of view, it is easy to conclude that it is a lot better to dose yourinsulin before a given meal. We can definitely use this in the GUI, later on.

3.2.2 What time to eat dinner

Usually a person has a strict working day, with a fixed time for getting out ofbed, eating breakfast and so on. Also the lunch break is often scheduled, sothat leaves the dinner, where you can make a potential change in eating time.So the next scenario we want to test is dinner time, in five different cases. Herethe eating times are 16:00,17:00,18:00,19:00 and finally 20:00. This shouldhopefully give us an idea of the effect of postponing dinner time an hour or two.All the rest of the data are exactly the same as before.

Figure 3.3: Dinner at varying time, the blacker the earlier.

According to this figure, the peak will become larger in scale if the diabeticperson choose to eat in a late hour. As a consequence of this, it will becomeharder to maintain a reasonable blood sugar level the rest of the night. Onthe other hand, we see that an early dinner will result in a slightly loweredconcentration of blood sugar. So it would seem that neither an early or a latedinner would be advantageous to the diabetic; rather the opposite.

Page 29: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

3.2 Hovorka put to the test 19

3.2.3 Bodyweight influence

As opposed to the first model, this one is actually directly dependent on theweight of the person taken into consideration. It would be nice to know justhow much the bodyweight affects the blood sugar level. With the same data asearlier, only adjusting the bodyweight, we get the following graph.We base the test on 10 scenarios, here BW=65:2:85, i.e. BW = [65, 67, . . . , 83, 85].

Figure 3.4: BW=65:2:85, i.e. the redder the heavier.

It is definitely confirmed that the model is influenced by the weight of the person.Since we are only adjusting the weight, and not anything else, the interpretationof this image would be: the behavior of blood glucose if your true bodyweightis differing from the doses and basal state insulin you are taking, according towhat you think you weigh. Put in another way, the figure is showing whathappens if you by mistake think you weigh more or less than you really do.

Page 30: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

20 Hovorka’s Model

We now want to change this figure, such that the basal state is 90mg/dL for allscenarios. This would give a better idea of what happens if you weigh somethingdifferent from 75kg. In order to do this, we have to find the u-value giving 90as basal state, when we change the body value. The results from our work canbe viewed in figure (3.5).

65 70 75 80 856

6.5

7

7.5

8

8.5

Body weight − kg

u−va

lue

givi

ng 9

0 as

bas

al s

tate

Figure 3.5: The linear behaviour of u, when changing the body weight.

Luckily enough for us, it would seem that the behavior is almost exactly linear.So if we find the slope of this line, this would hopefully give us the basal statefor all weights. Using the slope formula gives us approximately this value forthe different cases

∆y

∆x≈ 0.0954119.

So from this, we use the approximation that u = 0.0954119 ·BW , which we willalso use in the GUI for the Hovorka model. In specific we have u75 = 7.1558925,which is pretty much the value we have used up to this point.

Page 31: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

3.2 Hovorka put to the test 21

We now try to make the same graph, using the new correction for u making itsuitable for every weight that is put in. in the same scenario as before, we getthe following update.

Figure 3.6: BW=65:2:85, i.e. the redder the heavier, corrected for u.

As one can see, there is not that much of a difference between the two plots.What we can conclude from this, is that a heavier person should in general takemore insulin. But in the realistic case, a heavier person would probably eatmore as well, and should thereby take even more insulin. Of course the oppositeis expected to be true, that a light person will eat less and should take lessinsulin as well.

So we have learned that insulin preferably should be taken prior to a meal, thatvarying meal times, as expected, can have some influence on the glucose con-centration. Finally we have learned that u should be adjusted by the calculatedconstant, such that the basal state is always 90. From there it is up to the userto give the right values, of how much you eat and how much insulin you take.We have now made some valuable tests with Hovorka, and are ready to proceedto the next model.

Page 32: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

22 Hovorka’s Model

Page 33: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

Chapter 4

Man-Rizza-Cobelli’s Model

This next model is developed by Chiara Dalla Man, Robert A. Rizza andClaudio Cobelli, therefore we call it the Man-Rizza-Cobelli model.This model is a bit more complex than Hovorka’s, and is supposed to be morerealistic, which just makes it interesting to compare the two.

Just like the previous model, this simulation model is based on the principleof having a system of differential equations, and in the spirit of Hovorka’s modelwe have several systems (organs), which is all modeled separately and all con-nected. In the following we will simply try to present these different compart-ments with their matching equations and later on implement the model in ourGUI by means of MATLAB.

Page 34: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

24 Man-Rizza-Cobelli’s Model

4.1 Presentation of the model

Figure 4.1: This gives a general overview of the glucose-insulin control system,which is the fundamental idea of the Man-Rizza-Cobelli model.

In figure (4.1) one can see the principle of this model, there are 6 main com-partments; the glucose system, the insulin system, gastro-intestinal tract, liver,muscle and adipose tissue and of course the pancreas (beta-cell). Beside these,there are other organs which also have an effect on the glucose flow in the body,e.g. the brain and the kidneys.

Figure 4.2: The different unit process models, we will go in too detail with eachone later on.

Page 35: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

4.1 Presentation of the model 25

The last-mentioned belongs to the class of unit processes, and it’s these pro-cesses that describe how the different compartments interact with each other.A more detailed picture of what processes that we speak of, is included in figure(4.2). It should be stressed that all the constants that might be mentioned inthis chapter, can be found in [9].

4.1.1 The glucose subsystem

Figure 4.3: A schematic view of the glucose subsystem.

To describe the glucose kinetics, the two-compartment model is being used,which more or less is a well known model in this context. As we see in figure(4.3) the two variables Gp ∪ Gt make up the glucose system. Gp representthe glucose masses in plasma and rapidly equilibrating tissues, while Gt is theglucose masses in slowly equilibrating tissues. Both is defined by the followingequations

dGp(t)

dt= EGP (t) + Ra(t) − Uii(t) − E(t) − k1 · Gp(t) + k2 · Gt(t)(4.1)

dGt(t)

dt= −Uid(t) + k1 · Gp(t) − k2 · Gt(t). (4.2)

So the differential equations are made up of a series of unit processes, there’sthe endogenous glucose production (EGP), which in short is the glucose that isproduced by the liver.Then there is the glucose rate of appearance in plasma (Ra) coming fromthe gastro-intestinal tract-compartment, the renal excretion (E ), the insulin-independent (Uii) and -dependent glucose utilizations (Uid), respectively. Moreabout these mentioned processes and how they are defined, in their respectivesubsection later in this model description.Finally we also see that the two main compartment interact with each other,

Page 36: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

26 Man-Rizza-Cobelli’s Model

this is controlled by the rate parameters k1 and k2.The probably most important variable in Man-Rizza-Cobelli’s model is theplasma glucose concentration (G), founded by

G(t) =Gp

VG(4.3)

where VG is the distribution volume of glucose. This quantity is what we baseour studies on, because it’s the plasma glucose concentration that tells us if thediabetic needs a shot of insulin, or ingest some sugar.

4.1.2 The insulin subsystem

Figure 4.4: A schematic view of the insulin subsystem.

The two-compartment model is used to describe the insulin kinetics as well, herewe name Ip to represent the insulin masses in plasma, and Il the insulin massesin liver. So the insulin subsystem is made up of Ip ∪ Il and they are controlledby following differential equations

dIl(t)

dt= −(m1 + m3(t)) · Il(t) + m2 · Ip(t) + S(t) (4.4)

dIp(t)

dt= −(m2 + m4) · Ip(t) + m1 · Il(t). (4.5)

In this we have several rate parameters (m1, m2 and m4) and an insulin secretion(S ) from the pancreas to the liver. As done with the glucose system, we want ameasurable variable for the plasma insulin concentration, which is achieved by

I(t) =Ip

VI(4.6)

where VI is the distribution volume of insulin.

Page 37: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

4.1 Presentation of the model 27

4.1.3 The gastro-intestinal tract

This part of the model describes the glucose transit through the stomach andintestine. It is a model that just recently has been developed, that is a physio-logical model of glucose intestinal absorption.The stomach is being modeled as a two-compartment, one for the solid phase(Qsto1) and one for the liquid (Qsto2). These variables Qsto1 ∪ Qsto2 make upthe amount of glucose in the stomach. There is also a single compartmentrepresenting the gut (Qgut), which is the glucose masse in the intestine

Qsto(t) = Qsto1(t) + Qsto2(t) (4.7)

dQsto1(t)

dt= D(t) − kgri · Qsto1(t) (4.8)

dQsto2(t)

dt= kgri · Qsto1(t) − kempt(Qsto) · Qsto2(t) (4.9)

dQgut(t)

dt= kempt(Qsto) · Qsto2(t) − kabs · Qgut(t). (4.10)

Here D(t) is the amount of ingested glucose, kgri is the rate of grinding andkabs is the rate constant of intestinal absorption. In this part of the model, onemight notice the function kempt(Qsto) which is the rate of gastric emptying. Itsspecial because its nonlinear and has this complex definition

kempt(Qsto) = kmin+kmax − kmin

2(tanh(a(Qsto(t)−bD(t)))−tanh(c(Qsto(t)−dD(t)))+2).

(4.11)Another vital element of this section is the glucose rate of appearance in plasma(Ra), which has been mentioned earlier. This factor we now define as

Ra(t) =f · kabs · Qgut(t)

BW(4.12)

where BW is the bodyweight.

4.1.4 The liver

The liver also has a glucose production called endogenous glucose production, orshort EGP. Endogenous means that the glucose is made by the person himself,and in this context the liver. The functional description of EGP is constrainedto be non-negative and is defined by

EGP = kp1 − kp2 · Gp(t) − kp3 · Id(t) − kp4 · Ipo, EGP ≥ 0 (4.13)

where kp1 is the extrapolated EGP at zero glucose and insulin, kp2 liver glucoseeffectiveness, kp3 parameter governing amplitude of insulin action on the liver

Page 38: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

28 Man-Rizza-Cobelli’s Model

and kp4 parameter governing amplitude of portal insulin action on the liver.Moreover there is the amount of insulin in portal vein (Ipo) that has been sec-reted in the pancreas, while the delayed insulin signal (Id) is being realized bya series of two compartments

dI1(t)

dt= −ki(I1(t) − I(t)) (4.14)

dId(t)

dt= −ki(Id(t) − I1(t)). (4.15)

Here ki is a rate parameter for the delay between insulin signal and insulinaction.

4.1.5 The muscle and adipose tissue

The description of the muscle and adipose tissue’s (body tissue) glucose utili-zation during a meal, is quite complicated, so we will try to keep it on a simpleplan.First of all we have the interstitial fluid (X )

dX(t)

dt= −p2U · X(t) + p2U · (I(t) − Ib) (4.16)

where I is the plasma insulin, the reader might remember this quantity from theprevious section about the insulin subsystem. Ib represent the insulin plasma’sbasal state, while p2U is the rate constant of insulin action on the peripheralglucose utilization.It’s assumed that glucose utilization is made up of two compartments, insulin-independent and insulin-dependent utilization. The insulin-independent utili-zation (Uii) takes place in the first compartment and represents the uptake bythe brain and erythrocytes (red blood cells)

Uii(t) = Fcns. (4.17)

We see that it is constant and CNS refers to the central nervous system whichcontrols the contraction of - among others - the brain.In the other compartment the insulin-dependent utilization (Uid) takes place

Uid(t) = Vm(X(t)) ·Gt(t)

Km(X(t)) + Gt(t)(4.18)

and depends nonlinearly from glucose in the tissue. The functions Vm and Km

are assumed linearly dependent from a remote insulin and they are defined asfollows

Vm(X(t)) = Vm0 + Vmx · X(t) (4.19)

Km(X(t)) = Km0 + Kmx · X(t). (4.20)

Page 39: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

4.1 Presentation of the model 29

So in conclusion the total glucose utilization (U )

U(t) = Uii(t) + Uid(t). (4.21)

4.1.6 The kidneys

The glucose renal excretion occurs only if the plasma glucose exceeds a certainlevel, and is modelled by this function

E(t) =

{

ke1 · (Gp(t) − ke2) Gp > ke2

0 Gp ≤ ke2. (4.22)

We see that there is a linear relationship between the excretion and plasmaglucose. Further more ke1 is the glomerular filtration rate and ke2 is the renalthreshold of glucose.

4.1.7 The pancreas (beta-cell)

We have reached the final organ to model, which is of great importance whenwe speak of diabetics; the pancreas. The pancreas (beta-cells) is responsiblefor most of the insulin secretion in the body, so depending on whether we aresimulating a type 1 or a type 2 diabetic, this part of the model shouldn’t orshould be implemented along with the other equations, respectively. The modelequations are

dY (t)

dt=

{

−α · (Y (t) − β · (G(t) − h)) β · (G(t) − h) ≥ −Sb

−α · Y (t) − α · Sb β · (G(t) − h) < −Sb. (4.23)

Here α is the delay between glucose signal and insulin secretion, β is the pancre-atic responsivity to glucose and h is the threshold level of glucose above whichthe β-cells initiate to produce new insulin.Furthermore we have the equations for the insulin secretion into the plasma

dIpo(t)

dt= Spo(t) − S(t) (4.24)

where

Spo(t) =

{

Y (t) + Sb + K · G(t) G > 0

Y (t) + Sb G ≤ 0(4.25)

K is the pancreatic responsivity to the glucose rate of change and

S(t) = γ · Ipo(t) (4.26)

where γ is the transfer rate constant between portal vein and liver.

Page 40: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

30 Man-Rizza-Cobelli’s Model

4.2 Man-Rizza-Cobelli put to the test

It’s time for the implementation of the Man-Rizza-Cobelli model, but let usstart out by saying that this complex model gave us our share of complications.At first we decided to include all the equations described in the above of thischapter, and thereby we are modelling a type 2 diabetic. The result can be seenin the figure (4.5).

Figure 4.5: The Man-Rizza-Cobelli model simulated for a day, where insulin istaken before the meals.

The immediate reaction to this plot is that this isn’t completely as expected.The glucose concentration level is way to high at all times, which is obvioussince the graph is a quite a bit over the ’green zone’. But if we analyze thisfigure a little bit further, we find that its not totally useless.The behavior of the curve is somewhat like what we could expect; the personhas a steady state in the beginning of the day (we will return to this point lateron), he injects himself with insulin and the curve descends a bit, and when theCHO is consumed it rises. This pattern is in agreement with both what we sawwith Hovorka’s model, but also what we might expect of the reality.

The main problem we had with our implementation is to get the ’correct’ steadystate value, in comparison to Hovorka we want the basis insulin level to lie at90mg/dL, since this is a safe level for the diabetic. Unfortunately the time didn’tallow us to examine this thoroughly (that’s why the current level is around

Page 41: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

4.2 Man-Rizza-Cobelli put to the test 31

200mg/dL), so if we were to have more time, maybe we could have discovered aconnection between the steady state value and other parameters like bodyweight,insulin- and glucose basal values etc.

Another point that has been bothering us is, since its a type 2 diabetic wehave simulated (meaning the person has a functional pancreas producing insulinwhen necessary), it’s difficult to interpret the effect of the insulin injected. Fromthe look of it the insulin has some effect, but it’s far from being in the samemagnitude as with Hovorka. This could just be caused by the presence of thepancreas which - as explained earlier - has the ’job’ of adjusting the insulin levelat all times.Again, if we had more time available we would have tried to remove the pancreas-equations, so that we had the case of a type 1 diabetic.

So just to sum-up on this model, it seems like the behavior of the curve is whatwe want, but the values aren’t quite consistent with what we expect. That’swhy we have decided not to run a series of test cases, since we aren’t confidentenough that the model simulation is totally correct. We have tried to give ourguess on what the reasons might be for this, but as we stressed before the timewasn’t on our side. Of course this could be something to examine in the senseof continuing our model analysis.

The models have now been built, and even though there are some trouble withone of the models we would like to try to implement them both in a GUI, suchthat they in principle could be compared in the same program. That is whatwe will try to achieve in the following chapter.

Page 42: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

32 Man-Rizza-Cobelli’s Model

Page 43: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

Chapter 5

Implementation in a GUI

As promised we have decided to implement the mathematical models into aGUI1 with help from the MATLAB guide. guide is acronym for GUI DesignEnvironment and is a built-in environment made especially for building GUI’s.

The idea of this computer program is to make it easy for a person interestedin using a program like this, who has no knowledge of mathematical modelingwhatsoever.The purpose of the program is to plot the blood sugar concentration over agiven time interval in a scenario given by the user.

Our goal is to design a GUI from scratch, using the models already written andtested in the preliminary chapters. The considerations of design, limitationsand assumptions of the program will be outlined in the sections to come.

1Graphical User Interface

Page 44: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

34 Implementation in a GUI

5.1 GUI Design

Let’s start out by showing the graphical design of our GUI.

Figure 5.1: The modelSimulator as seen on startup.

The design is built up around three main sections, that should all add up tohelp the user in the best possible way.

• Menu bar (not visible in figure 5.1)

• Axis window

• Parameter setup

We will now go through each of these sections, and explain their functionalityand purpose in the program.

Page 45: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

5.1 GUI Design 35

5.1.1 Menu bar

The structure of the menu bar is set up like this:

Figure 5.2: The structure of the menu bar.

Most of the available features should be self-explaining, but let’s take a look atthe most important of these functions.First of all we have the Save Data, which saves the current data from theparameter setup. The data is saved to a .mat file, and is able to be loaded atanother time.Data is loaded with help from Load Data, which loads a .mat file and plotsthe blood sugar concentration in the given scenario directly.As a feature, we have the Save Figure command. By pressing this button, thecurrent figure in the axis window will be saved to a format chosen by the user.Available formats are .png, .jpeg, .gif and .bmp.The Clear Axis button will logically enough clear the axis, but will also deletethe calculated data. The function will always ask the user, to double check thatthe action was done purposely.Next up is the Model Info button. This will produce a box with valuableinformation about the model data. More specifically the user is provided withfour types of key figures i.e. the max, min, mean and median value of the bloodglucose concentration.Finally we have the Product Help. This is a simple help function, which willguide the user through the simple steps of making a successful simulation.

5.1.2 Axis window

The axis window is where all the information is delivered to the user, and istherefore prioritized to fill out most of the window. When a simulation has beenrun, the graph of the solution to the given scenario is plotted in this window.Dependent on which model that has been chosen, either one or two graphs are

Page 46: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

36 Implementation in a GUI

plotted in the case that ’Both Models’ has been chosen.A green rectangle is always plotted in the area [t0, tend] × [70, 130] in order todisplay the optimal concentration. Finally we plot three vertical lines for eachmeal in the simulation, one cyan, one blue and a magenta line. Each of theselines represent the time for insulin, eating time and the end of eating timerespectively.

5.1.3 Parameter setup

The parameter setup is also an important part of the overall program. It is herethe model is built from the data the user provides.In order to avoid erroneous or weird results, we have put some restrictions onmost of the parameters. In specific we check all data coming from an edit textin MATLAB, simply because the user can write practically whatever he/shewants. So all data has to be checked if it is a number within certain limits, wedo this with the self-written MATLAB function

state = checkNumber(number,min,max,integer)

This function checks that min<number<max, and that it really is a number, andnot a string for example. Furthermore if integer=true, the number is checkedwhether it is an integer or not. state is true if all demands are fulfilled andfalse otherwise.So, all in all the user has the following choices when setting up the model.

Parameter Choices and restrictions

Model Type Hovorka/Man-Rizza-Cobelli/BothTime Window Day/WeekInsulin Time 10/20/30/40 Minutes before mealBodyweight 0 < BW < 500

Eating Time Hour eath,i ∈ {0, 1, . . . , 23}, i ∈ {1, 2, 3}Eating Time Min eatm,i ∈ {0, 1, . . . , 59}, i ∈ {1, 2, 3}

CHO Amount 0 ≤ CHOi < ∞, i ∈ {1, 2, 3}Insulin Dose 0 ≤ Ii < ∞, i ∈ {1, 2, 3}

In general we only restrict us from actually wrong inputs. However with thebodyweight, it is not possible to weigh more than 500kg, since we think of this asa maximal upper limit. Besides, we have used the result from the first Hovorkatest, and only made it possible to inject insulin before a meal, not after.

Page 47: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

5.2 Code structuring 37

Finally, there is also a restriction making sure that the next insulin shot beforea meal can’t be taken before the prior meal has been eaten.

5.2 Code structuring

In the following section, we will take a brief look at how the code is set up, inthe most important functions. Simply to show the idea of how the whole thingis programmed.

5.2.1 buttonSimulate

buttonSimulate is the callback function connected to the big Simulate buttonin the bottom of the GUI. This is basically the MATLAB way of saying, thatthis function is called, when the mentioned button is pushed. Pushing thisbutton activates several other functions, that has been made to gather relatedcode in one place and thereby have a greater overview.setupModel is the first function to be run. This function extracts all necessaryinformation from the GUI, and verifies their correctness by using the alreadydescribed checkNumber.If everything is as expected, the next function simulateModel is called. Thisfunction makes the simulation with the selected model, and the given data takenfrom the setupModel.Finally we run the plotData, using the data calculated in simulateModel. Thisfunction takes care of virtually all the graphics presented in the axis window.

5.2.2 menuSaveData

This function runs the aforementioned setupModel to collect data. These dataare saved to a file chosen by the user. The data is saved to a .mat file for severalreasons. First of all, saving to a .mat file means no loss in precision, howeversince we are mainly saving integers or relatively small double precision num-bers this isn’t the most important reason. Secondarily, and more importantly,variable names are maintained when saving and loading a .mat file, and we cantherefore refer to the same variable name during the whole process.

Page 48: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

38 Implementation in a GUI

5.2.3 menuLoadData

This function first and foremost loads the user specified .mat file, and changesthe state of the parameter setup section such that it matches the state be-ing loaded. This synchronization is taken care of by changeState. From thispoint, the simulation will be run and finally plotted in the axis window usingsimulateModel and plotData.

This concludes the last chapter concerning the GUI, which leaves us with onlythe conclusion to write.

Page 49: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

Chapter 6

Conclusion

We have now reached the end of this bachelor thesis and in the overall con-clusion; two models (Hovorka and Man-Rizza-Cobelli respectively) under thesubject ’modeling and simulation of glucose-insulin dynamics’, have been im-plemented and tested in a self made GUI with MATLAB as the tool.

Starting with Hovorka’s model we think it’s safe to say that the implementationwas successful. We base this on the results that we derived, being in agreementwith the reality. We could quickly conclude that it was clearly an advantage fora diabetic to take his or hers insulin shot before consuming a meal. This madeit a lot easier for the diabetic to maintain a relative smooth curve, unlike thebig deviations that occurred when taking the insulin shot after a meal.Otherwise we found out that in this model the bodyweight had a rather largeinfluence on how one’s blood sugar concentration behaved. A diabetic shouldalways be aware of the weight, because the consequences could be fatal.Of course we should stress that these results are based on a relatively simplemodel, and as we all know a model can only be a simplified imitation of reality- especially something as complex as the human body. But on the other hand,the results is somewhat in accordance with past experiences of diabetics.

We also put effort in understanding and implementation of the more complexMan-Rizza-Cobelli model. This was in the hope of having another even more

Page 50: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

40 Conclusion

realistic model, to compare results with and to see if they had the same ten-dencies. As already mentioned we had a little trouble with getting the correctsteady state value, but on the good side we could see that the behavior of thecurve plus the daily pattern was pretty much alike. That’s why we are confidentthat with more time available, we could definitely in the end get the model tobehave as desired.So in conclusion there are still things to pick up on and further examine. Es-pecially for this model since its recently been decided to be officially acceptedfor testing on real data. I.e. data from the simulation with Man-Rizza-Cobellimodel is viewed as ’reliable results’1.

Last, but not least, we can proudly present our very own program, which isintended to give the user (diabetic) an easy alternative to plan the daily/weeklyroutine of meals and insulin shots. The GUI as it is, only contains the mostimportant features, but with more time it offers unexpected possibilities. If timeallowed us to, even more features can be added, such as the implementation ofmore models, more options regarding the time window or number/size of mealsetc.

Finally we would just like to take the time out, to thank our supervisor JohnBagterp Jørgensen for his help and guidance during the creation of this thesis.

1Information from our supervisor John Bagterp Jørgensen.

Page 51: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

Appendix A

MATLAB code

This appendix is the collection of all the MATLAB code that has been of im-portance to this project. Most of it is the programs used to built up our GUI.

Page 52: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

42 MATLAB code

A.1 The simple model

A.1.1 CHOAbsorption.m

1 function xdot = CHOAbsorption(t,x,D,tau ,A_G )

2 % CHOABSORPTION a model for absorption of CHO in two compartments.

3 % CHOAbsorption is a simplified model for the absorption of CHO in

4 % the body. It ’s based on the principle of a two -compartment

5 % system , where the first compartment is representing the stomach ,

6 % while the other is the colon . The parametric inputs means the

7 % following .

8 % t: The time window for the simulation

9 % x: The initial conditions

10 % D: CHO eating rate

11 % tau : The total eating time

12 % A_G : % of eaten CHO that ’s actually absorped in the body

13 %

14 % Syntax :

15 % [T,X]= ode15s (@CHOAbsorption ,[t0 t1],x0,odeOptions ,D,tau ,Ag);

16

17 U = x/tau;

18

19 % Initialize

20 xdot = zeros (2,1);

21

22 % The differential equations that constitutes the Glucose

23 % Absorption Model

24 xdot = [

25 A_G*D - U(1);

26 U(1) - U(2)

27 ];

Page 53: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.1 The simple model 43

A.1.2 SimulateCHOAbsorption.m

1 % SIMULATECHOABSORPTION simulates the CHO Absorption Model .

2

3 % %%%%%%%%%%%%%%%%%%%%%%%%% IMPULSE RESPONSE %%%%%%%%%%%%%%%%%%%%%%%%%

4 % The input parameters

5 tau = 40; % [min ]

6 A_G = 0.8; % [-]

7 D0 = 100; % [mg]

8

9 % Simulate for 8 hours

10 t0 = 0; % [min ]

11 t1 = 8*60; % [min ]

12

13 % The initial CHO amount

14 x0 = [A_G*D0 ;0];

15

16 % Simulation is executed

17 odeOptions = [];

18 [T1 ,X1] = ode15s (@CHOAbsorption ,[t0 t1],x0,odeOptions ,0,tau ,A_G);

19

20 % Blood Glucose absorption rate is calculated

21 U_G1 = X1(:,2) /tau;

22

23 % Plots the result

24 plot(T1,U_G1 ,’b-’,’LineWidth ’,2);

25 axis([t0 t1 min(U_G1) max(U_G1)])

26 title (’Glucose Absorption Rate - impulse response ’)

27 xlabel (’time [min ]’)

28 ylabel (’U_G [mg/min ]’)

29

30 % %%%%%%%%%%%%%%%%%%%%%%%%% CONTINUOUS EATING %%%%%%%%%%%%%%%%%%%%%%%%

31 D0 = 0; % [mg]

32

33 % The initial CHO amount

34 x0 = [A_G*D0 ;0];

35

36 % Simulation is executed

37 [T2 ,X2] = ode15s (@CHOAbsorption ,[t0 t1],x0,odeOptions ,1,tau ,A_G);

38

39 % Blood Glucose absorption rate is calculated

40 U_G2 = X2(:,2) /tau;

41

42 % Plots the result in a new figure

43 figure

44 plot(T2,U_G2 ,’r-’,’LineWidth ’,2);

45 axis([t0 t1 min(U_G2) max(U_G2)])

46 title (’Glucose Absorption Rate - continuous eating ’)

47 xlabel (’time [min ]’)

48 ylabel (’U_G [mg/min ]’)

Page 54: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

44 MATLAB code

A.2 GUI

A.2.1 modelSimulator.m

1 function varargout = modelSimulator(varargin )

2 % MODELSIMULATOR simulates a diabetic .

3 % This GUI simulates a diabetic ’s blood sugar concentration. The

4 % user can choose between two models ; Hovorka ’s and Man -Rizza -

5 % Cobelli ’s model , respectively.

6 % Other than that there is a possibility of changing different

7 % parameters , e.g. the time window for the simulation (day or

8 % week), when and how much to eat , when and how much insulin to

9 % take etc .

10

11 % Begin initialization code - DO NOT EDIT

12 gui_Singleton = 1;

13 gui_State = struct (’gui_Name ’, mfilename , ...

14 ’gui_Singleton’, gui_Singleton , ...

15 ’gui_OpeningFcn’, @modelSimulator_OpeningFcn ,...

16 ’gui_OutputFcn’, @modelSimulator_OutputFcn , ...

17 ’gui_LayoutFcn’, [] , ...

18 ’gui_Callback’, []) ;

19 if nargin && ischar ( varargin {1})

20 gui_State . gui_Callback = str2func (varargin {1});

21 end

22

23 if nargout

24 [ varargout {1: nargout }] = gui_mainfcn (gui_State , varargin {:});

25 else

26 gui_mainfcn (gui_State , varargin {:});

27 end

28 % End initialization code - DO NOT EDIT

29 % -----------------------------------------------------------------

30

31 % --- Executes just before modelSimulator is made visible

32 function modelSimulator_OpeningFcn (hObject , eventdata , handles ,

varargin )

33

34 % Choose default command line output for modelSimulator

35 handles .output = hObject ;

36

37 % Useable data are initialized as empty

38 handles .T = [];

39 handles .G = [];

40 handles .t = [];

41 handles .T2 = [];

42 handles .G2 = [];

43

44 % Update handles structure

45 guidata (hObject , handles );

46 % -----------------------------------------------------------------

47

48 % --- Outputs from this function are returned to the command line

Page 55: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 45

49 function varargout = modelSimulator_OutputFcn (hObject , eventdata ,

handles )

50

51 % Get default command line output from handles structure

52 varargout {1} = handles .output ;

53 % -----------------------------------------------------------------

54

55 % --- Executes on selection change in popupModel

56 function popupModel_Callback(hObject , eventdata , handles )

57 % -----------------------------------------------------------------

58

59 % --- Executes during object creation , after setting all properties

60 function popupModel_CreateFcn (hObject , eventdata , handles )

61

62 if ispc && isequal (get(hObject ,’BackgroundColor’), get (0, ’

defaultUicontrolBackgroundColor ’))

63 set(hObject ,’BackgroundColor’,’white ’);

64 end

65 % -----------------------------------------------------------------

66

67 % --- Executes on selection change in popupInsulin

68 function popupInsulin_Callback (hObject , eventdata , handles )

69 % -----------------------------------------------------------------

70

71 % --- Executes during object creation , after setting all properties

72 function popupInsulin_CreateFcn (hObject , eventdata , handles )

73

74 if ispc && isequal (get(hObject ,’BackgroundColor’), get (0, ’

defaultUicontrolBackgroundColor ’))

75 set(hObject ,’BackgroundColor’,’white ’);

76 end

77 % -----------------------------------------------------------------

78

79 function menuProductHelp_Callback (hObject , eventdata , handles )

80

81 % Remove info , if present

82 if( strcmp ( get(handles .menuInfo ,’Checked ’) , ’on’ ) )

83 set(handles .menuInfo ,’Checked ’,’off ’)

84 delete (handles .menuInfoBox );

85 end

86

87 if( strcmp ( get(handles .menuProductHelp ,’Checked ’) , ’off ’ ) )

88 set(handles .menuProductHelp ,’Checked ’,’on’)

89

90 % Listbox is set up

91 pos = [400 300 400 300];

92 handles .menuHelpBox = uicontrol (’Style ’,’Listbox ’,’Position ’,

pos);

93

94 % The help string is saved in anoter m-file

95 string = helpText ;

96

97 % The text is wrapped to fit the help box

98 [outstring ,newpos ] = textwrap (handles .menuHelpBox ,string );

99 pos (4) = newpos (4);

Page 56: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

46 MATLAB code

100 set(handles .menuHelpBox ,’String ’,outstring ,’Position ’,[pos (1) ,

pos (2) ,pos (3) +10, pos (4) ])

101

102 elseif ( strcmp ( get(handles .menuProductHelp ,’Checked ’) , ’on’ ) )

103 set(handles .menuProductHelp ,’Checked ’,’off ’)

104 delete (handles .menuHelpBox );

105 end

106

107 % Update handles structure

108 guidata (hObject , handles );

109 % -----------------------------------------------------------------

110

111 function breakHour_Callback(hObject , eventdata , handles )

112 % -----------------------------------------------------------------

113

114 % --- Executes during object creation , after setting all properties

115 function breakHour_CreateFcn (hObject , eventdata , handles )

116

117 if ispc && isequal (get(hObject ,’BackgroundColor ’), get (0, ’

defaultUicontrolBackgroundColor ’))

118 set(hObject ,’BackgroundColor’,’white ’);

119 end

120 % -----------------------------------------------------------------

121

122 function breakMin_Callback(hObject , eventdata , handles )

123 % -----------------------------------------------------------------

124

125 % --- Executes during object creation , after setting all properties

126 function breakMin_CreateFcn(hObject , eventdata , handles )

127

128 if ispc && isequal (get(hObject ,’BackgroundColor ’), get (0, ’

defaultUicontrolBackgroundColor ’))

129 set(hObject ,’BackgroundColor’,’white ’);

130 end

131 % -----------------------------------------------------------------

132

133 function lunchHour_Callback(hObject , eventdata , handles )

134 % -----------------------------------------------------------------

135

136 % --- Executes during object creation , after setting all properties

137 function lunchHour_CreateFcn (hObject , eventdata , handles )

138

139 if ispc && isequal (get(hObject ,’BackgroundColor ’), get (0, ’

defaultUicontrolBackgroundColor ’))

140 set(hObject ,’BackgroundColor’,’white ’);

141 end

142 % -----------------------------------------------------------------

143

144 function lunchMin_Callback(hObject , eventdata , handles )

145 % -----------------------------------------------------------------

146

147 % --- Executes during object creation , after setting all properties

148 function lunchMin_CreateFcn(hObject , eventdata , handles )

149

Page 57: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 47

150 if ispc && isequal (get(hObject ,’BackgroundColor’), get (0, ’

defaultUicontrolBackgroundColor ’))

151 set(hObject ,’BackgroundColor’,’white ’);

152 end

153 % -----------------------------------------------------------------

154

155 function dinnerHour_Callback(hObject , eventdata , handles )

156 % -----------------------------------------------------------------

157

158 % --- Executes during object creation , after setting all properties

159 function dinnerHour_CreateFcn (hObject , eventdata , handles )

160

161 if ispc && isequal (get(hObject ,’BackgroundColor’), get (0, ’

defaultUicontrolBackgroundColor ’))

162 set(hObject ,’BackgroundColor’,’white ’);

163 end

164 % -----------------------------------------------------------------

165

166 function dinnerMin_Callback(hObject , eventdata , handles )

167 % -----------------------------------------------------------------

168

169 % --- Executes during object creation , after setting all properties

170 function dinnerMin_CreateFcn(hObject , eventdata , handles )

171

172 if ispc && isequal (get(hObject ,’BackgroundColor’), get (0, ’

defaultUicontrolBackgroundColor ’))

173 set(hObject ,’BackgroundColor’,’white ’);

174 end

175 % -----------------------------------------------------------------

176

177 % --- Executes on button press in buttonSimulate

178 function buttonSimulate_Callback (hObject , eventdata , handles )

179

180 % Remove help , if present

181 if( strcmp ( get(handles .menuProductHelp ,’Checked ’) , ’on’ ) )

182 set(handles .menuProductHelp ,’Checked ’,’off ’)

183 delete (handles .menuHelpBox );

184 end

185

186 % Remove info , if present

187 if( strcmp ( get(handles .menuInfo ,’Checked ’) , ’on’ ) )

188 set(handles .menuInfo ,’Checked ’,’off ’)

189 delete (handles .menuInfoBox );

190 end

191

192 % Gets the model type

193 modelContents = get(handles .popupModel ,’String ’);

194 modelType = modelContents{get (handles .popupModel ,’Value ’)};

195

196 % This function sets up the model , by extracting all the data and

197 % check if they are correct . If an error occurs in assigning the

198 % values , an empty vector is returned

199 modelData = setupModel (handles );

200

201 % If the vector modelData is empty an error has occured , and the

Page 58: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

48 MATLAB code

202 % program stops here

203 if( isempty (modelData ) )

204 return

205 end

206

207 % Now the model is simulated , if everything is in order

208 [handles .T,handles .G,handles .t,handles .T2 ,handles .G2] = ...

209 simulateModel(modelType ,modelData );

210

211 % Here we plot the simulated data

212 plotData (handles .axes ,handles .T,handles .G,handles .t,handles .T2 , ...

213 handles .G2 ,modelData ([2,3,6,7,10 ,11]) );

214

215 % Update handles structure

216 guidata (hObject , handles );

217 % -----------------------------------------------------------------

218

219 function breakInsulin_Callback (hObject , eventdata , handles )

220 % -----------------------------------------------------------------

221

222 % --- Executes during object creation , after setting all properties

223 function breakInsulin_CreateFcn (hObject , eventdata , handles )

224

225 if ispc && isequal (get(hObject ,’BackgroundColor ’), get (0, ’

defaultUicontrolBackgroundColor ’))

226 set(hObject ,’BackgroundColor’,’white ’);

227 end

228 % -----------------------------------------------------------------

229

230 function lunchInsulin_Callback (hObject , eventdata , handles )

231 % -----------------------------------------------------------------

232

233 % --- Executes during object creation , after setting all properties

234 function lunchInsulin_CreateFcn (hObject , eventdata , handles )

235

236 if ispc && isequal (get(hObject ,’BackgroundColor ’), get (0, ’

defaultUicontrolBackgroundColor ’))

237 set(hObject ,’BackgroundColor’,’white ’);

238 end

239 % -----------------------------------------------------------------

240

241 function dinnerInsulin_Callback (hObject , eventdata , handles )

242 % -----------------------------------------------------------------

243

244 % --- Executes during object creation , after setting all properties

245 function dinnerInsulin_CreateFcn (hObject , eventdata , handles )

246

247 if ispc && isequal (get(hObject ,’BackgroundColor ’), get (0, ’

defaultUicontrolBackgroundColor ’))

248 set(hObject ,’BackgroundColor’,’white ’);

249 end

250 % -----------------------------------------------------------------

251

252 function editBW_Callback(hObject , eventdata , handles )

253 % -----------------------------------------------------------------

Page 59: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 49

254

255 % --- Executes during object creation , after setting all properties

256 function editBW_CreateFcn(hObject , eventdata , handles )

257

258 if ispc && isequal (get(hObject ,’BackgroundColor’), get (0, ’

defaultUicontrolBackgroundColor ’))

259 set(hObject ,’BackgroundColor’,’white ’);

260 end

261 % -----------------------------------------------------------------

262

263 function breakCHO_Callback(hObject , eventdata , handles )

264 % -----------------------------------------------------------------

265

266 % --- Executes during object creation , after setting all properties

267 function breakCHO_CreateFcn(hObject , eventdata , handles )

268

269 if ispc && isequal (get(hObject ,’BackgroundColor’), get (0, ’

defaultUicontrolBackgroundColor ’))

270 set(hObject ,’BackgroundColor’,’white ’);

271 end

272 % -----------------------------------------------------------------

273

274 function edit12_Callback(hObject , eventdata , handles )

275 % -----------------------------------------------------------------

276

277 % --- Executes during object creation , after setting all properties

278 function edit12_CreateFcn(hObject , eventdata , handles )

279

280 if ispc && isequal (get(hObject ,’BackgroundColor’), get (0, ’

defaultUicontrolBackgroundColor ’))

281 set(hObject ,’BackgroundColor’,’white ’);

282 end

283 % -----------------------------------------------------------------

284

285 function dinnerCHO_Callback(hObject , eventdata , handles )

286 % -----------------------------------------------------------------

287

288 % --- Executes during object creation , after setting all properties

289 function dinnerCHO_CreateFcn(hObject , eventdata , handles )

290

291 if ispc && isequal (get(hObject ,’BackgroundColor’), get (0, ’

defaultUicontrolBackgroundColor ’))

292 set(hObject ,’BackgroundColor’,’white ’);

293 end

294 % -----------------------------------------------------------------

295

296 function lunchCHO_Callback(hObject , eventdata , handles )

297 % -----------------------------------------------------------------

298

299 % --- Executes during object creation , after setting all properties

300 function lunchCHO_CreateFcn(hObject , eventdata , handles )

301

302 if ispc && isequal (get(hObject ,’BackgroundColor’), get (0, ’

defaultUicontrolBackgroundColor ’))

303 set(hObject ,’BackgroundColor’,’white ’);

Page 60: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

50 MATLAB code

304 end

305 % -----------------------------------------------------------------

306

307 function menuSaveFigure_Callback (hObject , eventdata , handles )

308

309 % Remove help , if present

310 if( strcmp ( get(handles .menuProductHelp ,’Checked ’) , ’on’ ) )

311 set(handles .menuProductHelp ,’Checked ’,’off ’)

312 delete (handles .menuHelpBox );

313 end

314

315 % Remove info , if present

316 if( strcmp ( get(handles .menuInfo ,’Checked ’) , ’on’ ) )

317 set(handles .menuInfo ,’Checked ’,’off ’)

318 delete (handles .menuInfoBox );

319 end

320

321 % If there is no data to plot , an error will occur

322 if( isempty (handles .T) || isempty (handles .G) || isempty (handles .t)

)

323 errordlg (’Not enough data to save plot!’,’Error saving figure ’);

324 return

325 end

326

327 % Get information about file

328 [filename , pathname , filterindex ] = uiputfile (...

329 {’*.png ’,’png file (*. png )’;...

330 ’*. jpeg’,’jpeg file (*. jpeg)’;...

331 ’*.gif ’,’gif file (*. gif )’;...

332 ’*.bmp ’,’bmp file (*. bmp )’;...

333 } ,’Choose a file to be saved ’);

334

335 % If user press cancel , nothing should happen from here

336 if( filterindex ==0 )

337 return

338 end

339

340 boundbox_x = 40;

341 boundbox_y = 50;

342

343 oldColor = get (handles .figure1 ,’Color ’);

344 set (handles .figure1 ,’Color ’ ,[1 1 1]);

345

346 % Get frame , and save it to the given path

347 f = getframe (handles .axes ,[- boundbox_x -boundbox_y ...

348 1081+ boundbox_x 440+ boundbox_y ]);

349

350 set (handles .figure1 ,’Color ’,oldColor );

351

352 savepath = [pathname filename ];

353 imwrite ( f.cdata , savepath );

354 % -----------------------------------------------------------------

355

356 function menuWindow_Callback (hObject , eventdata , handles )

357 % -----------------------------------------------------------------

Page 61: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 51

358

359 function menuAxisClear_Callback (hObject , eventdata , handles )

360

361 % Continue only , if wanted

362 beep;

363 answer = questdlg (’Do you really want to clear axis. All data will

be deleted !’, ...

364 ’Attention ’,’Yes ’,’No’,’No’);

365

366 if( strcmp (answer ,’No’) || strcmp (answer ,’’) )

367 return

368 end

369

370 % Remove help , if present

371 if( strcmp ( get(handles .menuProductHelp ,’Checked ’) , ’on’ ) )

372 set(handles .menuProductHelp ,’Checked ’,’off ’)

373 delete (handles .menuHelpBox );

374 end

375

376 % Remove info , if present

377 if( strcmp ( get(handles .menuInfo ,’Checked ’) , ’on’ ) )

378 set(handles .menuInfo ,’Checked ’,’off ’)

379 delete (handles .menuInfoBox );

380 end

381

382 handles .t = [];

383 handles .T = [];

384 handles .G = [];

385 handles .T2 = [];

386 handles .G2 = [];

387

388 axes(handles .axes);

389 cla ;

390

391 % Update handles structure

392 guidata (hObject , handles );

393 % -----------------------------------------------------------------

394

395 function menuAllClear_Callback (hObject , eventdata , handles )

396 % -----------------------------------------------------------------

397

398 function menuHelp_Callback(hObject , eventdata , handles )

399 % -----------------------------------------------------------------

400

401 function menuExit_Callback(hObject , eventdata , handles )

402

403 % If there is no data , it is ok to close

404 if( isempty (handles .T) && isempty (handles .G) && isempty (handles .t)

)

405 close (handles .figure1 );

406 return

407 end

408

409 % Otherwise , the user should be asked

410 beep;

Page 62: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

52 MATLAB code

411 answer = questdlg (’Do you really want to close . All unsaved data

will be deleted !’,’Attention ’,’Yes ’,’No’,’No’);

412

413 if( strcmp (answer ,’Yes ’) )

414 close (handles .figure1 );

415 end

416 % -----------------------------------------------------------------

417

418 function menuInfo_Callback(hObject , eventdata , handles )

419

420 if( isempty (handles .T) || isempty (handles .G) || isempty (handles .t)

)

421 errordlg (’No data to analyze !’,’Error !’);

422 return

423 end

424

425 % Remove help , if present

426 if( strcmp ( get(handles .menuProductHelp ,’Checked ’) , ’on’ ) )

427 set(handles .menuProductHelp ,’Checked ’,’off ’)

428 delete (handles .menuHelpBox );

429 end

430

431 if( strcmp ( get(handles .menuInfo ,’Checked ’) , ’off ’ ) )

432 set(handles .menuInfo ,’Checked ’,’on’)

433

434 % Listbox is set up

435 pos = [71 566 280 100];

436 handles .menuInfoBox = uicontrol (’Style ’,’Listbox ’,’Position ’,

pos);

437 set(handles .menuInfoBox ,’FontName ’,’Monospaced ’);

438

439 % Gets the model type

440 modelContents = get(handles .popupModel ,’String ’);

441 modelType = modelContents{get(handles .popupModel ,’Value ’)};

442 CobelliPresent = false;

443 HovorkaPresent = false;

444

445 switch modelType

446 case ’Man -Rizza -Cobelli ’

447 CobelliPresent = true;

448 case ’Hovorka ’

449 HovorkaPresent = true;

450 case ’Both Models ’

451 CobelliPresent = true;

452 HovorkaPresent = true;

453 end

454

455 string = cell (0);

456 if( HovorkaPresent && ~CobelliPresent )

457 string = {’Hovorka :’;

458 [’Maximum glucose conc. ’,num2str (max(handles .G)),’ mg/dL’];

459 [’Minimum glucose conc. ’,num2str (min(handles .G)),’ mg/dL’];

460 [’Mean glucose conc. ’,num2str (mean(handles .G)),’ mg/dL’];

461 [’Median glucose conc. ’,num2str (median (handles .G)),’ mg/dL’];

462 };

Page 63: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 53

463

464 elseif ( ~HovorkaPresent && CobelliPresent )

465 string = {’Man -Rizza -Cobelli :’;

466 [’Maximum glucose conc. ’,num2str (max(handles .G)),’ mg/dL’];

467 [’Minimum glucose conc. ’,num2str (min(handles .G)),’ mg/dL’];

468 [’Mean glucose conc. ’,num2str (mean(handles .G)),’ mg/dL’];

469 [’Median glucose conc. ’,num2str (median (handles .G)),’ mg/dL’];

470 };

471

472 elseif ( HovorkaPresent && CobelliPresent )

473 string = {’Hovorka :’;

474 [’Maximum glucose conc. ’,num2str (max(handles .G)),’ mg/dL’];

475 [’Minimum glucose conc. ’,num2str (min(handles .G)),’ mg/dL’];

476 [’Mean glucose conc. ’,num2str (mean(handles .G)),’ mg/dL’];

477 [’Median glucose conc. ’,num2str (median (handles .G)),’ mg/dL’];

478 ’’;

479 ’Man -Rizza -Cobelli :’;

480 [’Maximum glucose conc. ’,num2str (max(handles .G2)),’ mg/dL’];

481 [’Minimum glucose conc. ’,num2str (min(handles .G2)),’ mg/dL’];

482 [’Mean glucose conc. ’,num2str (mean(handles .G2)),’ mg/dL’];

483 [’Median glucose conc. ’,num2str (median (handles .G2)),’ mg/dL’]

484 };

485 pos = [71 476 280 100];

486 end

487

488 % The text is wrapped to fit the info box

489 [outstring ,newpos ] = textwrap (handles .menuInfoBox ,string );

490 pos (4) = newpos (4);

491 set(handles .menuInfoBox ,’String ’,outstring ,’Position ’,[pos (1) ,

pos (2) ,pos (3) +10, pos (4) ])

492

493 elseif ( strcmp ( get(handles .menuInfo ,’Checked ’) , ’on’ ) )

494 set(handles .menuInfo ,’Checked ’,’off ’)

495 delete (handles .menuInfoBox );

496 end

497

498 % Update handles structure

499 guidata (hObject , handles );

500 % -----------------------------------------------------------------

501

502 function menuSaveData_Callback (hObject , eventdata , handles )

503

504 % Get information about file

505 [filename , pathname , filterindex ] = uiputfile (...

506 {’*.mat ’,’MAT file (*. mat )’;...

507 } ,’Choose a file to be saved ’);

508

509 % Gets the model type

510 modelContents = get(handles .popupModel ,’String ’);

511 modelType = modelContents{get (handles .popupModel ,’Value ’)};

512

513 modelNumber = get (handles .popupModel ,’Value ’);

514

515 % This function sets up the model , by extracting all the data and

516 % check if they are correct . If an error occurs in assigning the

Page 64: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

54 MATLAB code

517 % values , an empty vector is returned

518 modelData = setupModel (handles );

519

520 % If the vector modelData is empty an error has occured , and the

521 % program stops here

522 if( isempty (modelData ) )

523 return

524 end

525

526 switch filterindex

527 case 0

528 return

529 case 1

530 save([ pathname filename ],’modelType ’,’modelData ’,’

modelNumber ’,’-MAT ’);

531 end

532 % -----------------------------------------------------------------

533

534 function menuLoadData_Callback (hObject , eventdata , handles )

535

536 % Get information about file

537 [filename , pathname , filterindex ] = uigetfile (...

538 {’*.mat ’,’MAT file (*. mat )’;...

539 } ,’Choose a file to be saved ’);

540

541 % Data is initialized

542 modelData = [];

543 modelNumber = [];

544 modelType = [];

545

546 % Load if .mat file , otherwise quit

547 switch filterindex

548 case 1

549 load([ pathname filename ]);

550 otherwise

551 return

552 end

553

554 % This should happen , if a wrong .mat file has been loaded

555 if( isempty (modelData ) || isempty ( modelNumber ) || isempty ( modelType

) )

556 errordlg (’Make sure that the file you loaded is a save file’,’

Error loading data’);

557 return

558 end

559

560 % External function to set the current state , to what has been

561 % loaded

562 changeState (handles ,modelData ,modelNumber );

563

564 % Now the model is simulated , if everything is in order

565 [handles .T,handles .G,handles .t,handles .T2 ,handles .G2] = ...

566 simulateModel(modelType ,modelData );

567

568 % Here we plot the simulated data

Page 65: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 55

569 plotData (handles .axes ,handles .T,handles .G,handles .t,handles .T2 ,...

570 handles .G2 ,modelData ([2,3,6,7,10 ,11]) );

571

572 % Update handles structure

573 guidata (hObject , handles );

574 % -----------------------------------------------------------------

A.2.2 setupModel.m

1 function output = setupModel ( handles )

2 %SETUPMODEL will extract and check all data from modelSimulator.

3 % The function modelData = setupModel (handles ) will take out the

4 % relevant information from the modelSimulator GUI , and won ’t make

5 % sense for any other purpose .

6 %

7 % When all data has been turn into usable numbers , they are

8 % checked one by one with help from the CHECKNUMBER function . If

9 % an error is detected an error dialog pops up and specifies what

10 % is wrong .

11 %

12 % See also: MODELSIMULATOR

13

14 % Gets the bodyweight value

15 BW = str2double (get(handles .editBW ,’String ’));

16

17 % Extracts breakfast related info

18 bHour = str2double (get(handles .breakHour ,’String ’));

19 bMin = str2double (get(handles .breakMin ,’String ’));

20 bCHO = str2double (get(handles .breakCHO ,’String ’));

21 bInsulin = 1000* str2double (get(handles .breakInsulin ,’String ’));

22

23 % Extracts lunch related info

24 lHour = str2double (get(handles .lunchHour ,’String ’));

25 lMin = str2double (get(handles .lunchMin ,’String ’));

26 lCHO = str2double (get(handles .lunchCHO ,’String ’));

27 lInsulin = 1000* str2double (get(handles .lunchInsulin ,’String ’));

28

29 % Extracts dinner related info

30 dHour = str2double (get(handles .dinnerHour ,’String ’));

31 dMin = str2double (get(handles .dinnerMin ,’String ’));

32 dCHO = str2double (get(handles .dinnerCHO ,’String ’));

33 dInsulin = 1000* str2double (get(handles .dinnerInsulin ,’String ’));

34

35 % Get the string for which the insulin should be taken compared to

36 % the meal

37 insulinContents = get(handles .popupInsulin ,’String ’);

38 insulinTime = str2double (insulinContents{get(handles .popupInsulin ,’

Value ’)});

39

40 % Gets the radio button value , i.e. if the value is 1 then the

41 % program should simulate for a day , otherwise a week

42 isDay = get (handles .radiobuttonDay ,’Value ’);

Page 66: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

56 MATLAB code

43

44 % Checks that the given input is allowed

45 state (1) = checkNumber ( BW , realmin , 500 );

46 state (2) = checkNumber ( bHour , 0 , 23 , true );

47 state (3) = checkNumber ( bMin , 0 , 59 , true );

48 state (4) = checkNumber ( bCHO , 0 , inf );

49 state (5) = checkNumber ( bInsulin , 0 , inf );

50 state (6) = checkNumber ( lHour , 0 , 23 , true );

51 state (7) = checkNumber ( lMin , 0 , 59 , true );

52 state (8) = checkNumber ( lCHO , 0 , inf );

53 state (9) = checkNumber ( lInsulin , 0 , inf );

54 state (10) = checkNumber ( dHour , 0 , 23 , true );

55 state (11) = checkNumber ( dMin , 0 , 59 , true );

56 state (12) = checkNumber ( dCHO , 0 , inf );

57 state (13) = checkNumber ( dInsulin , 0 , inf );

58

59 % The vector with data which should be returned , if no errors are

60 % found

61 output = [BW ,bHour ,bMin ,bCHO ,bInsulin ,lHour ,lMin ,lCHO ,lInsulin ,

dHour ,...

62 dMin ,dCHO ,dInsulin ,insulinTime ,isDay ];

63

64 % If not all input is correct , an error message will be displayed .

65 % Furthermore the output is made into an empty vector

66 if ( ~ all( state ) )

67 index = find( state == 0 );

68 inputName = {’Bodyweight ’, ’Hour of breakfast ’, ’Minutes of

breakfast ’, ...

69 ’CHO amount in breakfast ’, ’Insulin dose for breakfast ’, ’

Hour of lunch ’, ’Minutes of lunch ’, ...

70 ’CHO amount in lunch ’, ’Insulin dose for lunch ’, ’Hour of

dinner ’, ’Minutes of dinner ’, ...

71 ’CHO amount in dinner ’, ’Insulin dose for dinner ’};

72 errordlg ([ num2str (inputName {index }),’ is not assigned correctly

!’],’Input error ’);

73 output = [];

74 return

75 end

76

77 % Checks that the meals are in the correct order , otherwise an

78 % error message will be displayed

79 if ( ~(60* bHour+bMin +30 < 60* lHour +lMin - insulinTime && 60* lHour +

lMin +30 < 60* dHour+dMin -insulinTime ) )

80 errordlg (’The meals must come in a logical order!’,’Input error

’);

81 output = [];

82 end

Page 67: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 57

A.2.3 simulateModel.m

1 function [T,G,t,T2 ,G2] = simulateModel(modelType ,modelData )

2 % SIMULATEMODEL takes care of modelling for modelSimulator

3 % This function is used to simulate data for the given model , with

4 % the given input data.

5 % Output is dependent on whether one or two simulations are asked

6 % for .

7 %

8 % See also: MODELSIMULATOR

9

10 % isDay is extracted and afterwards deleted from modelData , since

11 % it ’s useless after this

12 isDay = modelData (15);

13 modelData (15) = [];

14

15 % T2 and G2 , representing the values of the secondary function in

16 % ’Both Models ’, are initialized

17 T2 = [];

18 G2 = [];

19

20 % Use the data in the simulation of the model chosen by the user

21 switch modelType

22 case ’Hovorka ’

23 if(isDay)

24 [T,G,t] = GUISimulateHovorkaDay (modelData );

25 else

26 [T,G,t] = GUISimulateHovorkaWeek (modelData );

27 end

28 case ’Man -Rizza -Cobelli ’

29 if(isDay)

30 [T,G,t] = GUISimulateCobelliDay (modelData );

31 else

32 [T,G,t] = GUISimulateCobelliWeek (modelData );

33 end

34 case ’Both Models ’

35 if(isDay)

36 [T,G,t] = GUISimulateHovorkaDay (modelData );

37 [T2 ,G2] = GUISimulateCobelliDay (modelData );

38 else

39 [T,G,t] = GUISimulateHovorkaWeek (modelData );

40 [T2 ,G2] = GUISimulateCobelliWeek (modelData );

41 end

42 end

Page 68: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

58 MATLAB code

A.2.4 plotData.m

1 function plotData (handle ,T,G,t,T2,G2, mealStrings )

2 % PLOTDATA will plot the simulated data.

3 % The function is made to plot all data from simulateModel.

4 %

5 % See also: MODELSIMULATOR

6

7 % Gets the axes handles and clear the axis

8 axes(handle );

9 cla ;

10

11 % These constants represents the minimum and the maximum , when

12 % plotting . This is the minimum of a value chosen to be 50 and the

13 % minimum of the two functions . If only one function is present ,

14 % the result will still be correct

15 axisMin = min( min ([G;G2]) , 50 );

16 axisMax = max( max ([G;G2]) ,150 );

17

18 % Plots the simulated blood sugar concentration

19 hold on

20 plot(T/60,G,’k-’,’LineWidth ’,2);

21 plot(T2/60,G2,’b-’,’LineWidth ’ ,2);

22

23 % Plots lines showing when the insulin is taken , and when the

24 % person has started and stopped eating

25 for i=2:3: length (t) -1

26 plot ([t(i) t(i)]/60 ,[ axisMin axisMax ],’c--’);

27 plot ([t(i+1) t(i+1) ]/60 ,[ axisMin axisMax ],’b--’);

28 plot ([t(i+2) t(i+2) ]/60 ,[ axisMin axisMax ],’m--’);

29 end

30

31 % Plots two lines that indicates ’the allowed ’ blood sugar area ,

32 % where the person can be in without suffering

33 fill( [t(1) t(end ) t(end ) t(1)] , [70 70 130 130] ,[0 1 0.2], ’

EdgeAlpha ’, 0, ’FaceAlpha ’ ,0.2) ;

34

35 % Put a legend on the plot

36 if(isempty (G2))

37 legend (’G’)

38 else

39 legend (’H’,’C’)

40 end

41

42 hold off

43

44 axis ([t(1) /60 t(end )/60 axisMin axisMax ])

45

46 % In order to get the correct time labels on the axis , that

47 % indicates when the person starts eating , we need to use

48 % troublesome methods

49 timeCell = cell (0);

50 j=0;

51 for i=1:2: length ( mealStrings )

52 currString = ’’;

Page 69: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 59

53 if(mealStrings (i+1) <10)

54 currString = ’0’;

55 currString = [num2str (mealStrings (i)),’:’,currString ,

num2str (mealStrings (i+1))];

56 else

57 currString = [num2str (mealStrings (i)),’:’,num2str (

mealStrings (i+1))];

58 end

59 j=j+1;

60 timeCell {j} = currString ;

61 end

62

63 set (gca ,’XTick ’,t(3:3: end -2) /60,’XTickLabel ’,timeCell );

64

65 % Info text for the plot

66 text(t(2) /60 - 2.25 ,120 , ’Insulin taken \rightarrow ’,’Color ’,’Cyan ’,

’FontWeight ’,’Bold’);

67 text(t(3) /60 - 2.15 ,116 , ’Start eating \rightarrow ’,’Color ’,’Blue’,’

FontWeight ’,’Bold’);

68 text(t(4) /60 + 0.15 ,120 , ’\leftarrow Stop eating ’,’Color ’,’Magenta ’,

’FontWeight ’,’Bold’);

69

70 % Labels to the axis

71 xlabel (’Time of day [h]’)

72 ylabel (’G - Blood sugar conc. [mg/dL]’)

A.2.5 helpText.m

1 function text = helpText ()

2 % HELPTEXT is the help text for Product Help in modelSimulator

3 % This is the text being used when Product Help is activated . This

4 % is only made in order to maintain a better overview of the main

5 % file

6 %

7 % See also: MODELSIMULATOR

8

9 % Output

10 text = {’Step 1: Choose model ’,

11 ’Choose between the models Hovorka and Man -Rizza -Cobelli or

both at the same time.’,

12 ’’,

13 ’Step 2: Choose time window ’,

14 ’You can choose to simulate either a day or a 7-day week.’,

15 ’In the case of a week , the data will be used periodically ,

’,

16 ’i.e. the parameters are the same for each of the 21 meals .

’,

17 ’’,

18 ’Step 3: Take Insulin ’,

19 ’This decides how long before a meal the insulin shot

should be taken .’,

20 ’’,

Page 70: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

60 MATLAB code

21 ’Step 4: Set parameters ’,

22 ’Specify the data , asked for in the box in the lower right

side.’,

23 ’All the data will be checked by the program , and an error

message ’,

24 ’will be displayed , if something seems wrong.’,

25 ’’,

26 ’Step 5: Simulate !’,

27 ’After simulate has been pressed , this help box will

disappear ’,

28 ’and the simulation should be succesful .’};

A.2.6 checkNumber.m

1 function state = checkNumber ( number , minimum , maximum ,

checkInteger )

2 % CHECKNUMBER checks if a number fulfills certain conditions .

3 % checkNumber takes the value that should be examined as argument .

4 % It will check the lower and upper boundary and if desired -

5 % whether the number is and integer or not .

6 %

7 % state = checkNumber ( x , lower , upper , integerCheck )

8 %

9 % If integer is true , it will be checked if it really is an

10 % integer . If it’s false or omitted no check will be done.

11

12 % state is initialized

13 state = true;

14

15 % checkInteger is set to 0 if the user hasn ’t done anything else

16 if( nargin ==3 )

17 checkInteger = false;

18 end

19

20 % checks that the number is not over or under the allowed

21 % boundaries and that is not a complex number or a character

22 if( number > maximum || number < minimum || ~isreal ( number ) ||

isnan ( number ) || ischar ( number ) )

23 state = false ;

24 elseif ( checkInteger ) % checks if the number is integer , if wanted

25 if( mod ( number , 1 ) ~= 0 )

26 state = false;

27 end

28 end

Page 71: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 61

A.2.7 changeState.m

1 function changeState (handles ,modelData ,modelNumber )

2 % CHANGESTATE changes the state of modelSimulator

3 %

4 % This function is used when loading a state in MODELSIMULATOR. The

5 % function makes sure that the data that ’s plotted coincides with

the state

6 % that is displayed in the parameter setup

7 %

8 % See also: MODELSIMULATOR

9

10 set (handles .popupModel ,’Value ’,modelNumber )

11

12 set (handles .editBW ,’String ’,num2str (modelData (1)));

13

14 set (handles .breakHour ,’String ’,num2str (modelData (2)));

15 set (handles .breakMin ,’String ’,num2str ( modelData (3)));

16 set (handles .breakCHO ,’String ’,num2str ( modelData (4)));

17 set (handles .breakInsulin ,’String ’,num2str (modelData (5) /1000) );

18

19 set (handles .lunchHour ,’String ’,num2str (modelData (6)));

20 set (handles .lunchMin ,’String ’,num2str ( modelData (7)));

21 set (handles .lunchCHO ,’String ’,num2str ( modelData (8)));

22 set (handles .lunchInsulin ,’String ’,num2str (modelData (9) /1000) );

23

24 set (handles .dinnerHour ,’String ’,num2str (modelData (10)));

25 set (handles .dinnerMin ,’String ’,num2str (modelData (11)));

26 set (handles .dinnerCHO ,’String ’,num2str (modelData (12)));

27 set (handles .dinnerInsulin ,’String ’,num2str (modelData (13) /1000) );

28

29 set (handles .popupInsulin ,’Value ’,modelData (14) /10);

30 if( modelData (15) == 1 )

31 set(handles .radiobuttonDay ,’Value ’,1);

32 end

Page 72: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

62 MATLAB code

A.2.8 Hovorka.m

1 function xdot = Hovorka (t,x,u,D,p)

2 % HOVORKA is a model for absorption of CHO in a human body.

3 % Hovorka is a mathematical model , based on various differential

4 % equations , representing different parts of the human body.

5 %

6 % The parametric inputs means the following .

7 % t: The time window for the simulation

8 % x: The initial conditions

9 % u: Administration of short -acting insulin

10 % D: CHO eating rate

11 % p: Vector containing the 16 constants necessary for the model

12 %

13 % Syntax :

14 % [T,X]= ode15s (@Hovorka ,[t0 t1],xInitial0 ,odeOptions ,u,D,p);

15

16 % Defining the various equation names

17 D1 = x(1,1);

18 D2 = x(2,1);

19 S1 = x(3,1);

20 S2 = x(4,1);

21 Q1 = x(5,1);

22 Q2 = x(6,1);

23 I = x(7,1);

24 x1 = x(8,1);

25 x2 = x(9,1);

26 x3 = x(10 ,1) ;

27

28 % Unpack data

29 tau_G = p(1);

30 tau_I = p(2);

31 A_G = p(3);

32 k_12 = p(4);

33 k_a1 = p(5);

34 k_b1 = p(6);

35 k_a2 = p(7);

36 k_b2 = p(8);

37 k_a3 = p(9);

38 k_b3 = p(10);

39 k_e = p(11);

40 V_I = p(12);

41 V_G = p(13);

42 F_01 = p(14);

43 EGP_0 = p(15);

44

45 % Certian parameters are defined

46 U_G = x(2,1)/tau_G;

47 U_I = x(4,1)/tau_I;

48

49 % Constitutive equations

50 G = Q1/V_G;

51

52 if(G >=4.5)

53 F_01c = F_01;

Page 73: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 63

54 else

55 F_01c = F_01*G/4.5;

56 end

57

58 if(G>=9)

59 F_R = 0.003*( G -9)*V_G;

60 else

61 F_R = 0;

62 end

63

64 % Mass balances / differential equations

65 xdot = zeros (10 ,1) ;

66

67 xdot (1,1) = A_G*D - D1/tau_G ; % dD1

68 xdot (2,1) = D1/tau_G - U_G; % dD2

69 xdot (3,1) = u - S1/tau_I; % dS1

70 xdot (4,1) = S1/tau_I - U_I; % dS2

71 xdot (5,1) = -(F_01c +F_R)-x1*Q1+k_12*Q2+U_G +EGP_0 *(1-x3); % dQ1

72 xdot (6,1) = x1*Q1 -( k_12+x2)*Q2; % dQ2

73 xdot (7,1) = U_I/V_I - k_e *I; % dI

74 xdot (8,1) = k_b1*I - k_a1*x1; % dx1

75 xdot (9,1) = k_b2*I - k_a2*x2; % dx2

76 xdot (10 ,1) = k_b3*I - k_a3*x3; % dx3

A.2.9 HovorkaWrap.m

1 function xdot = HovorkaWrap (x,u,D,p)

2 % HOVORKAWRAP is used to find steady state for Hovorka

3 % This is a helping function to Hovorka , used to calculate the

4 % steady state values . This function only makes sense in

5 % combination with Hovorka .

6 %

7 % See also: HOVORKA , GUISIMULATEHOVORKADAY , GUISIMULATEHOVORKAWEEK

8

9 xdot = Hovorka (0,x,u,D,p);

Page 74: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

64 MATLAB code

A.2.10 GUISimulateHovorkaDay.m

1 function [T,G,t] = GUISimulateHovorkaDay (modelData )

2 % GUISIMULATEHOVORKADAY models Hovorka for a day

3 % GUISimulateHovorkaDay is used in modelSimulator when simulating

4 % the Hovorka model for a whole day . This isn ’t meant to be run as

5 % a single function , but ONLY as an assistant function for

6 % modelSimulator.

7 %

8 % See also: HOVORKA , MODELSIMULATOR

9

10

11 eatingTime = 30; % [min ]

12

13 % Data is unpacked

14 BW = modelData (1);

15 bHour = modelData (2);

16 bMin = modelData (3);

17 bCHO = modelData (4);

18 bInsulin = modelData (5);

19 lHour = modelData (6);

20 lMin = modelData (7);

21 lCHO = modelData (8);

22 lInsulin = modelData (9);

23 dHour = modelData (10);

24 dMin = modelData (11);

25 dCHO = modelData (12);

26 dInsulin = modelData (13);

27 insulinTime = modelData (14);

28

29 % Initialize the time vector

30 t = zeros (11 ,1) ;

31

32 % Relevant moments are calculated

33 t(1) = 0;

34 t(2) = bHour *60+bMin -insulinTime ;

35 t(3) = bHour *60+ bMin;

36 t(4) = bHour *60+ bMin+eatingTime ;

37 t(5) = lHour *60+lMin -insulinTime ;

38 t(6) = lHour *60+ lMin;

39 t(7) = lHour *60+ lMin+eatingTime ;

40 t(8) = dHour *60+dMin -insulinTime ;

41 t(9) = dHour *60+ dMin;

42 t(10) = dHour *60+ dMin+eatingTime ;

43 t(11) = 24*60;

44

45 % The constants needed for the Hovorka Model

46 S_IT = 51.2e-4;

47 S_ID = 8.2e-4;

48 S_IE = 520e-4;

49

50 tau_G = 40; % [min ]

51 tau_I = 55; % [min ]

52 A_G = 0.8; % [-]

53 k_12 = 0.066; % [min ]

Page 75: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 65

54 k_a1 = 0.006; % [min ^-1]

55 k_b1 = S_IT*k_a1; %

56 k_a2 = 0.06; % [min ^-1]

57 k_b2 = S_ID*k_a2; %

58 k_a3 = 0.03; % [min ^-1]

59 k_b3 = S_IE*k_a3; %

60 k_e = 0.138; % [min ^-1]

61 V_I = 0.12* BW; % [L]

62 V_G = 0.16* BW; % [L]

63 F_01 = 0.0097* BW; % [mmol/min ]

64 EGP_0 = 0.0161* BW; % [mmol/min ]

65

66 % Defines the vector with the given parameters

67 p = [tau_G ,tau_I ,A_G ,k_12 ,k_a1 ,k_b1 ,k_a2 ,k_b2 ,k_a3 ,k_b3 ,k_e ,V_I ,V_G

,F_01 ,EGP_0 ];

68 odeOptions = [];

69 options = optimset (’Display ’,’off ’);

70

71 % u is set such that the basal state is always 90

72 u = 0.0954119* BW;

73

74 % Calculate the steady state values

75 xStart = zeros (10 ,1) ;

76 xInitial0 = fsolve (@HovorkaWrap ,xStart ,options ,u,0,p);

77

78 % A progress bar is created

79 waitbarHandle = waitbar (0, ’Modelling Hovorka , please wait ... ’);

80

81 % Midnight to first insulinshot

82 [T1 ,X1]= ode15s (@Hovorka ,[t(1) t(2)],xInitial0 ,odeOptions ,u,0,p);

83

84 waitbar (1/10 , waitbarHandle);

85

86 % Insulinshot before breakfast

87 xInitial1 = X1(end ,:) ’;

88 xInitial1 (3) = xInitial1 (3)+bInsulin ;

89 [T2 ,X2]= ode15s (@Hovorka ,[t(2) t(3)],xInitial1 ,odeOptions ,u,0,p);

90

91 waitbar (2/10 , waitbarHandle);

92

93 % Breakfast start

94 xInitial2 = X2(end ,:) ’;

95 [T3 ,X3]= ode15s (@Hovorka ,[t(3) t(4)],xInitial2 ,odeOptions ,u,bCHO

/(180* eatingTime )*1000 , p);

96

97 waitbar (3/10 , waitbarHandle);

98

99 % Breakfast stop

100 xInitial3 = X3(end ,:) ’;

101 [T4 ,X4]= ode15s (@Hovorka ,[t(4) t(5)],xInitial3 ,odeOptions ,u,0,p);

102

103 waitbar (4/10 , waitbarHandle);

104

105 % Insulinshot before lunch

106 xInitial4 = X4(end ,:) ’;

Page 76: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

66 MATLAB code

107 xInitial4 (3) = xInitial4 (3)+lInsulin ;

108 [T5 ,X5]= ode15s (@Hovorka ,[t(5) t(6) ],xInitial4 ,odeOptions ,u,0,p);

109

110 waitbar (5/10 , waitbarHandle);

111

112 % Lunch start

113 xInitial5 = X5(end ,:) ’;

114 [T6 ,X6]= ode15s (@Hovorka ,[t(6) t(7) ],xInitial5 ,odeOptions ,u,lCHO

/(180* eatingTime )*1000 , p);

115

116 waitbar (6/10 , waitbarHandle);

117

118 % Lunch stop

119 xInitial6 = X6(end ,:) ’;

120 [T7 ,X7]= ode15s (@Hovorka ,[t(7) t(8) ],xInitial6 ,odeOptions ,u,0,p);

121

122 waitbar (7/10 , waitbarHandle);

123

124 % Insulinshot before dinner

125 xInitial7 = X7(end ,:) ’;

126 xInitial7 (3) = xInitial7 (3)+dInsulin ;

127 [T8 ,X8]= ode15s (@Hovorka ,[t(8) t(9) ],xInitial7 ,odeOptions ,u,0,p);

128

129 waitbar (8/10 , waitbarHandle);

130

131 % Dinner start

132 xInitial8 = X8(end ,:) ’;

133 [T9 ,X9]= ode15s (@Hovorka ,[t(9) t(10)],xInitial8 ,odeOptions ,u,dCHO

/(180* eatingTime )*1000 , p);

134

135 waitbar (9/10 , waitbarHandle);

136

137 % Dinner stop

138 xInitial9 = X9(end ,:) ’;

139 [T10 ,X10 ]= ode15s (@Hovorka ,[t(10) t(11)],xInitial9 ,odeOptions ,u,0,p)

;

140

141 waitbar (95/100 , waitbarHandle);

142

143 % Collects all the simulated intervals

144 T_concat = ’T = [’;

145 X_concat = ’X = [’;

146 for n = 1: length (t) -1

147 T_concat = [T_concat ,’T’,int2str (n),’;’];

148 X_concat = [X_concat ,’X’,int2str (n),’(:,5) ;’];

149 end

150 T_concat = [T_concat ,’];’];

151 X_concat = [X_concat ,’];’];

152

153 eval(T_concat );

154 eval(X_concat );

155

156 G = 18*X/V_G;

157 waitbar (1, waitbarHandle);

158 close (waitbarHandle);

Page 77: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 67

A.2.11 GUISimulateHovorkaWeek.m

1 function [T,G,t] = GUISimulateHovorkaWeek (modelData )

2 % GUISIMULATEHOVORKAWEEK models Hovorka for a week

3 % GUISimulateHovorkaWeek is used in modelSimulator when simulating

4 % the Hovorka model for a whole week. This isn ’t meant to be run

5 % as a single function , but ONLY as an assistant function for

6 % modelSimulator.

7 %

8 % See also: HOVORKA , MODELSIMULATOR

9

10 eatingTime = 30; % [min ]

11

12 % Data is unpacked

13 BW = modelData (1) ;

14 bHour = modelData (2) ;

15 bMin = modelData (3);

16 bCHO = modelData (4);

17 bInsulin = modelData (5) ;

18 lHour = modelData (6) ;

19 lMin = modelData (7);

20 lCHO = modelData (8);

21 lInsulin = modelData (9) ;

22 dHour = modelData (10);

23 dMin = modelData (11);

24 dCHO = modelData (12);

25 dInsulin = modelData (13);

26 insulinTime = modelData (14);

27

28 % Initialize the time vector , i.e. start and end , plus 3 scheduled

29 % times per meal (3 per day ) in 7 days

30 t = zeros (2+7*3*3 ,1) ;

31

32 % Relevant moments are calculated

33 t(1) = 0;

34 t(2) = bHour *60+bMin -insulinTime ;

35 t(3) = bHour *60+ bMin;

36 t(4) = bHour *60+ bMin+eatingTime ;

37 t(5) = lHour *60+lMin -insulinTime ;

38 t(6) = lHour *60+ lMin;

39 t(7) = lHour *60+ lMin+eatingTime ;

40 t(8) = dHour *60+dMin -insulinTime ;

41 t(9) = dHour *60+ dMin;

42 t(10) = dHour *60+ dMin+eatingTime ;

43

44 % Sets up the following days

45 for i=11: length (t)

46 t(i) = t(i-9) +24*60;

47 end

48

49 t(end ) = 24*7*60;

50

51 % The constants needed for the Hovorka Model

52 S_IT = 51.2e -4;

53 S_ID = 8.2e-4;

Page 78: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

68 MATLAB code

54 S_IE = 520e-4;

55

56 tau_G = 40; % [min ]

57 tau_I = 55; % [min ]

58 A_G = 0.8; % [-]

59 k_12 = 0.066; % [min ]

60 k_a1 = 0.006; % [min ^-1]

61 k_b1 = S_IT*k_a1; %

62 k_a2 = 0.06; % [min ^-1]

63 k_b2 = S_ID*k_a2; %

64 k_a3 = 0.03; % [min ^-1]

65 k_b3 = S_IE*k_a3; %

66 k_e = 0.138; % [min ^-1]

67 V_I = 0.12* BW; % [L]

68 V_G = 0.16* BW; % [L]

69 F_01 = 0.0097* BW; % [mmol/min ]

70 EGP_0 = 0.0161* BW; % [mmol/min ]

71

72 % Defines the vector with the given parameters

73 p = [tau_G ,tau_I ,A_G ,k_12 ,k_a1 ,k_b1 ,k_a2 ,k_b2 ,k_a3 ,k_b3 ,k_e ,V_I ,V_G

,F_01 ,EGP_0 ];

74 odeOptions = [];

75 options = optimset (’Display ’,’off ’);

76

77 % u is set such that the basal state is always 90

78 u = 0.0954119* BW;

79

80 % Calculate the steady state values

81 xStart = zeros (10 ,1) ;

82 xInitial0 = fsolve (@HovorkaWrap ,xStart ,options ,u,0,p);

83

84 waitbarHandle = waitbar (0,’Modelling Hovorka , please wait... ’);

85

86 % Midnight to first insulinshot

87 [T1 ,X1]= ode15s (@Hovorka ,[t(1) t(2) ],xInitial0 ,odeOptions ,u,0,p);

88

89 T = T1;

90 X = X1(:,5);

91

92 waitbar (1/65 , waitbarHandle);

93

94 % This takes care of all the following meals , all the following

95 % days

96 for i=2:9: length (t) -1

97

98 % Insulinshot before breakfast

99 if(i==2)

100 xInitial1 = X1(end ,:) ’;

101 xInitial1 (3) = xInitial1 (3)+bInsulin ;

102 else

103 xInitial1 = X10(end ,:) ’;

104 xInitial1 (3) = xInitial1 (3)+bInsulin ;

105 end

106

Page 79: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 69

107 [T2,X2]= ode15s (@Hovorka ,[t(i) t(i+1) ],xInitial1 ,odeOptions ,u,0,

p);

108

109 % Breakfast start

110 xInitial2 = X2(end ,:) ’;

111 [T3,X3]= ode15s (@Hovorka ,[t(i+1) t(i+2)],xInitial2 ,odeOptions ,u,

bCHO /(180* eatingTime )*1000 , p);

112

113 % Breakfast stop

114 xInitial3 = X3(end ,:) ’;

115 [T4,X4]= ode15s (@Hovorka ,[t(i+2) t(i+3)],xInitial3 ,odeOptions ,u

,0,p);

116

117 % Insulinshot before lunch

118 xInitial4 = X4(end ,:) ’;

119 xInitial4 (3) = xInitial4 (3)+ lInsulin ;

120 [T5,X5]= ode15s (@Hovorka ,[t(i+3) t(i+4)],xInitial4 ,odeOptions ,u

,0,p);

121

122 % Lunch start

123 xInitial5 = X5(end ,:) ’;

124 [T6,X6]= ode15s (@Hovorka ,[t(i+4) t(i+5)],xInitial5 ,odeOptions ,u,

lCHO /(180* eatingTime )*1000 , p);

125

126 % Lunch stop

127 xInitial6 = X6(end ,:) ’;

128 [T7,X7]= ode15s (@Hovorka ,[t(i+5) t(i+6)],xInitial6 ,odeOptions ,u

,0,p);

129

130 % Insulinshot before dinner

131 xInitial7 = X7(end ,:) ’;

132 xInitial7 (3) = xInitial7 (3)+ dInsulin ;

133 [T8,X8]= ode15s (@Hovorka ,[t(i+6) t(i+7)],xInitial7 ,odeOptions ,u

,0,p);

134

135 % Dinner start

136 xInitial8 = X8(end ,:) ’;

137 [T9,X9]= ode15s (@Hovorka ,[t(i+7) t(i+8)],xInitial8 ,odeOptions ,u,

dCHO /(180* eatingTime )*1000 , p);

138

139 % Dinner stop

140 xInitial9 = X9(end ,:) ’;

141 [T10 ,X10 ]= ode15s (@Hovorka ,[t(i+8) t(i+9)],xInitial9 ,odeOptions ,

u,0,p);

142

143 T = [T;T2;T3;T4;T5;T6;T7;T8;T9;T10 ];

144 X = [X;X2(:,5);X3 (:,5);X4(:,5);X5(:,5);X6(:,5);X7(:,5) ;X8(:,5)

;...

145 X9(:,5);X10 (:,5) ];

146

147 waitbar ((i+9) /65, waitbarHandle);

148 end

149

150 close (waitbarHandle);

151 G = 18*X/V_G;

Page 80: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

70 MATLAB code

A.2.12 Cobelli.m

1 function xdot = Cobelli (t,x,u,D,p)

2 % COBELLI is a model for absorption of CHO in a human body.

3 % Cobelli is a mathematical model , based on various differential

4 % equations , representing different parts of the human body.

5 %

6 % The parametric inputs means the following .

7 % t: The time window for the simulation

8 % x: The initial conditions

9 % u: Administration of short -acting insulin

10 % D: CHO eating rate

11 % p: Vector containing the 36 constants necessary for the model

12 %

13 % Syntax :

14 % [T,X]= ode15s (@Cobelli ,[t0 t1],xInitial0 ,odeOptions ,u,D,p);

15

16 % Functions are defined

17 G_p = x(1,1);

18 G_t = x(2,1);

19 I_l = x(3,1);

20 I_p = x(4,1);

21 Q_sto1 = x(5,1);

22 Q_sto2 = x(6,1);

23 Q_gut = x(7,1);

24 I_1 = x(8,1);

25 I_d = x(9,1);

26 X = x(10 ,1) ;

27 Y = x(11 ,1) ;

28 I_po = x(12 ,1) ;

29

30 %Constants

31 V_G = p(1,1);

32 k_1 = p(2,1);

33 k_2 = p(3,1);

34 V_I = p(4,1);

35 m_1 = p(5,1);

36 m_2 = p(6,1);

37 m_4 = p(7,1);

38 m_5 = p(8,1);

39 m_6 = p(9,1);

40 HE_b = p(10 ,1) ;

41 k_max = p(11 ,1) ;

42 k_min = p(12 ,1) ;

43 k_abs = p(13 ,1) ;

44 k_gri = p(14 ,1) ;

45 f = p(15 ,1) ;

46 a = p(16 ,1) ;

47 b = p(17 ,1) ;

48 c = p(18 ,1) ;

49 d = p(19 ,1) ;

50 k_p1 = p(20 ,1) ;

51 k_p2 = p(21 ,1) ;

52 k_p3 = p(22 ,1) ;

53 k_p4 = p(23 ,1) ;

Page 81: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 71

54 k_i = p(24 ,1) ;

55 F_cns = p(25 ,1) ;

56 V_m0 = p(26 ,1) ;

57 V_mx = p(27 ,1) ;

58 K_m0 = p(28 ,1) ;

59 p_2U = p(29 ,1) ;

60 K = p(30 ,1) ;

61 alpha = p(31 ,1) ;

62 beta = p(32 ,1) ;

63 gamma = p(33 ,1) ;

64 k_e1 = p(34 ,1) ;

65 k_e2 = p(35 ,1) ;

66 BW = p(36 ,1) ;

67

68 S = gamma*I_po;

69 S_b = (m_6 -HE_b)/m_5 ;

70

71 % Insulin basal state

72 I_b = 0;

73

74 h = 0; % = G_b

75

76

77 %Certain useful parameters are defined

78 HE = -m_5*S + m_6 ;

79 m_3 = HE*m_1 /(1-HE);

80

81 %GI Tract

82 Ra = f*k_abs*Q_gut/BW;

83 Q_sto = Q_sto1 + Q_sto2 ;

84 k_emptQ_sto = k_min + (k_max -k_min)/2*( tanh(a*(Q_sto -b*D)) - tanh(c

*(Q_sto -d*D))+2);

85 %Liver

86 EGP = max (0 , k_p1 - k_p2*G_p - k_p3*I_d - k_p4*I_po);

87 %Muscle and Adipose Tissue

88 V_m = V_m0 + V_mx*X;

89 K_m = K_m0;

90 U_id = V_m*G_t /(K_m+G_t );

91 %Kidneys - Glucose Renal Excretion

92 if(G_p > k_e2)

93 E = k_e1*(G_p -k_e2);

94 else

95 E = 0;

96 end

97 %Brain - CNS Glucose Utilization

98 U_ii = F_cns;

99

100

101 %Mass balances / differential equations

102 xdot = zeros (12 ,1) ;

103

104 xdot (1,1) = EGP + Ra - U_ii - E - k_1*G_p + k_2*G_t; %dG_p

105 xdot (2,1) = -U_id + k_1 *G_p - k_2*G_t; %dG_t

106

107 G = xdot (1,1)/V_G ;

Page 82: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

72 MATLAB code

108

109 xdot (3,1) = -(m_1+m_3)*I_l + m_2*I_p + S; %dI_l

110 xdot (4,1) = -(m_2+m_4)*I_p + m_1*I_l + u; %dI_p

111

112 I = xdot (4,1)/V_I;

113

114 xdot (5,1) = D - k_gri*Q_sto1 ; %dQ_sto1

115 xdot (6,1) = k_gri *Q_sto1 - k_emptQ_sto *Q_sto2 ; %dQ_sto2

116 xdot (7,1) = k_emptQ_sto *Q_sto2 - k_abs*Q_gut; %dQ_gut

117

118 xdot (8,1) = -k_i *(I_1 -I); %dI_1

119 xdot (9,1) = -k_i *(I_d -I_1 ); %dI_d

120

121 xdot (10 ,1) = p_2U*(I-I_b)-p_2U*X; %dX

122

123 if(beta *(G-h) >= -S_b) %dY

124 xdot (11 ,1) = -alpha *(Y-beta *(G-h));

125 else

126 xdot (11 ,1) = -alpha *(Y+S_b);

127 end

128

129 %Pancreas / Beta -Cell

130 if(G > 0)

131 S_po = Y + S_b + K*G;

132 else

133 S_po = Y + S_b;

134 end

135

136 xdot (12 ,1) = S_po - S; %dI_po

A.2.13 CobelliWrap.m

1 function xdot = CobelliWrap (x,u,D,p)

2 % COBELLIWRAP is used to find steady state for Cobelli

3 % This is a helping function to Cobelli , used to calculate the

4 % steady state values . This function only makes sense in

5 % combination with Cobelli .

6 %

7 % See also: COBELLI , GUISIMULATECOBELLIDAY , GUISIMULATECOBELLIWEEK

8

9 xdot = Cobelli (0,x,u,D,p);

Page 83: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 73

A.2.14 GUISimulateCobelliDay.m

1 function [T,G,t] = GUISimulateCobelliDay (modelData )

2 % GUISIMULATECOBELLIDAY models Cobelli for a day

3 % GUISimulateCobelliDay is used in modelSimulator when simulating

4 % the Cobelli model for a whole day . This isn ’t meant to be run as

5 % a single function , but ONLY as an assistant function for

6 % modelSimulator.

7 %

8 % See also: COBELLI , MODELSIMULATOR

9

10 eatingTime = 30; % [min ]

11

12 % Data is unpacked

13 BW = modelData (1) ;

14 bHour = modelData (2) ;

15 bMin = modelData (3) ;

16 bCHO = 1000* modelData (4); % converted to mg

17 bInsulin = 6.67* modelData (5); % converted to pmol/L

18 lHour = modelData (6) ;

19 lMin = modelData (7) ;

20 lCHO = 1000* modelData (8); % converted to mg

21 lInsulin = 6.67* modelData (9); % converted to pmol/L

22 dHour = modelData (10);

23 dMin = modelData (11);

24 dCHO = 1000* modelData (12); % converted to mg

25 dInsulin = 6.67* modelData (13); % converted to pmol/L

26 insulinTime = modelData (14);

27

28 % Initialize the time vector

29 t = zeros (11 ,1) ;

30

31 % Relevant moments are calculated

32 t(1) = 0;

33 t(2) = bHour *60+bMin -insulinTime ;

34 t(3) = bHour *60+ bMin;

35 t(4) = bHour *60+ bMin+eatingTime ;

36 t(5) = lHour *60+lMin -insulinTime ;

37 t(6) = lHour *60+ lMin;

38 t(7) = lHour *60+ lMin+eatingTime ;

39 t(8) = dHour *60+dMin -insulinTime ;

40 t(9) = dHour *60+ dMin;

41 t(10) = dHour *60+ dMin+eatingTime ;

42 t(11) = 24*60;

43

44 V_G = 1.49; % [dL/kg]

45 k_1 = 0.042; % [min ^-1]

46 k_2 = 0.071; % [min ^-1]

47 V_I = 0.04; % [L/kg]

48 m_1 = 0.379; % [min ^-1]

49 m_2 = 0.673; % [min ^-1]

50 m_4 = 0.269; % [min ^-1]

51 m_5 = 0.0526; % [min *kg/pmol]

52 m_6 = 0.8118; % [-]

53 HE_b = 0.6; % [-]

Page 84: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

74 MATLAB code

54 k_max = 0.0465; % [min ^-1]

55 k_min = 0.0076; % [min ^-1]

56 k_abs = 0.023; % [min ^-1]

57 k_gri = 0.0465; % [min ^-1]

58 f = 0.90; % [-]

59 a = 0.00006; % [mg^-1]

60 b = 0.68; % [-]

61 c = 0.00023; % [mg^-1]

62 d = 0.09; % [-]

63 k_p1 = 3.09; % [mg/kg/min ]

64 k_p2 = 0.0007; % [min ^-1]

65 k_p3 = 0.005; % [mg/kg/min per pmol/L]

66 k_p4 = 0.0786; % [mg/kg/min per pmol/kg]

67 k_i = 0.0066; % [min ^-1]

68 F_cns = 1; % [mg/kg/min ]

69 V_m0 = 4.65; % [mg/kg/min ]

70 V_mx = 0.034; % [mg/kg/min per pmol/L]

71 K_m0 = 466.21; % [mg/kg]

72 p_2U = 0.084; % [min ^-1]

73 K = 0.99; % [pmol/kg per mg/dL]

74 alpha = 0.013; % [min ^-1]

75 beta = 0.05; % [pmol/kg/min per mg/dL]

76 gamma = 0.5; % [min ^-1]

77 k_e1 = 0.0007; % [min ^-1]

78 k_e2 = 269; % [mg/kg]

79

80 p = [V_G ;k_1;k_2;V_I;m_1;m_2 ;m_4;m_5;m_6 ;HE_b;k_max;k_min;k_abs ;

k_gri ;...

81 f;a;b;c;d;k_p1;k_p2;k_p3;k_p4;k_i;F_cns;V_m0;V_mx;K_m0;p_2U;K;alpha

;...

82 beta;gamma;k_e1;k_e2;BW];

83

84 odeOptions = [];

85 options = optimset (’Display ’,’off ’);

86

87 u = 7.15;

88

89 % Calculate the steady state values

90 xStart = zeros (12 ,1) ;

91 xInitial0 = fsolve (@CobelliWrap ,xStart ,options ,u,0,p);

92

93 % A progress bar is created

94 waitbarHandle = waitbar (0,’Modelling Cobelli , please wait... ’);

95

96 % Midnight to first insulinshot

97 [T1 ,X1]= ode15s (@Cobelli ,[t(1) t(2) ],xInitial0 ,odeOptions ,u,0,p);

98

99 waitbar (1/10 , waitbarHandle);

100

101 % Insulinshot before breakfast

102 xInitial1 = X1(end ,:) ’;

103 xInitial1 (3) = xInitial1 (3)+bInsulin ;

104 [T2 ,X2]= ode15s (@Cobelli ,[t(2) t(3) ],xInitial1 ,odeOptions ,u,0,p);

105

106 waitbar (2/10 , waitbarHandle);

Page 85: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 75

107

108 % Breakfast start

109 xInitial2 = X2(end ,:) ’;

110 [T3 ,X3]= ode15s (@Cobelli ,[t(3) t(4)],xInitial2 ,odeOptions ,u,bCHO/

eatingTime ,p);

111

112 waitbar (3/10 , waitbarHandle);

113

114 % Breakfast stop

115 xInitial3 = X3(end ,:) ’;

116 [T4 ,X4]= ode15s (@Cobelli ,[t(4) t(5)],xInitial3 ,odeOptions ,u,0,p);

117

118 waitbar (4/10 , waitbarHandle);

119

120 % Insulinshot before lunch

121 xInitial4 = X4(end ,:) ’;

122 xInitial4 (3) = xInitial4 (3)+lInsulin ;

123 [T5 ,X5]= ode15s (@Cobelli ,[t(5) t(6)],xInitial4 ,odeOptions ,u,0,p);

124

125 waitbar (5/10 , waitbarHandle);

126

127 % Lunch start

128 xInitial5 = X5(end ,:) ’;

129 [T6 ,X6]= ode15s (@Cobelli ,[t(6) t(7)],xInitial5 ,odeOptions ,u,lCHO/

eatingTime ,p);

130

131 waitbar (6/10 , waitbarHandle);

132

133 % Lunch stop

134 xInitial6 = X6(end ,:) ’;

135 [T7 ,X7]= ode15s (@Cobelli ,[t(7) t(8)],xInitial6 ,odeOptions ,u,0,p);

136

137 waitbar (7/10 , waitbarHandle);

138

139 % Insulinshot before dinner

140 xInitial7 = X7(end ,:) ’;

141 xInitial7 (3) = xInitial7 (3)+dInsulin ;

142 [T8 ,X8]= ode15s (@Cobelli ,[t(8) t(9)],xInitial7 ,odeOptions ,u,0,p);

143

144 waitbar (8/10 , waitbarHandle);

145

146 % Dinner start

147 xInitial8 = X8(end ,:) ’;

148 [T9 ,X9]= ode15s (@Cobelli ,[t(9) t(10)],xInitial8 ,odeOptions ,u,dCHO/

eatingTime ,p);

149

150 waitbar (9/10 , waitbarHandle);

151

152 % Dinner stop

153 xInitial9 = X9(end ,:) ’;

154 [T10 ,X10 ]= ode15s (@Cobelli ,[t(10) t(11)],xInitial9 ,odeOptions ,u,0,p)

;

155

156 waitbar (95/100 , waitbarHandle);

157

Page 86: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

76 MATLAB code

158 % Collects all the simulated intervals

159 T_concat = ’T = [’;

160 X_concat = ’X = [’;

161 for n = 1: length (t) -1

162 T_concat = [T_concat ,’T’,int2str (n),’;’];

163 X_concat = [X_concat ,’X’,int2str (n),’(:,1) ;’];

164 end

165 T_concat = [T_concat ,’];’];

166 X_concat = [X_concat ,’];’];

167

168 eval(T_concat )

169 eval(X_concat )

170

171 G = X/V_G;

172

173 waitbar (1, waitbarHandle);

174 close (waitbarHandle);

A.2.15 GUISimulateCobelliWeek.m

1 function [T,G,t] = GUISimulateCobelliWeek (modelData )

2 % GUISIMULATECOBELLIWEEK models Cobelli for a week

3 % GUISimulateCobelliWeek is used in modelSimulator when simulating

4 % the Cobelli model for a whole week. This isn ’t meant to be run

5 % as a single function , but ONLY as an assistant function for

6 % modelSimulator.

7 %

8 % See also: COBELLI , MODELSIMULATOR

9

10 eatingTime = 30; % [min ]

11

12 % Data is unpacked

13 BW = modelData (1);

14 bHour = modelData (2);

15 bMin = modelData (3);

16 bCHO = 1000* modelData (4) ;

17 bInsulin = 6.67* modelData (5) ;

18 lHour = modelData (6);

19 lMin = modelData (7);

20 lCHO = 1000* modelData (8) ;

21 lInsulin = 6.67* modelData (9) ;

22 dHour = modelData (10);

23 dMin = modelData (11);

24 dCHO = 1000* modelData (12);

25 dInsulin = 6.67* modelData (13);

26 insulinTime = modelData (14);

27

28 % Initialize the time vector , i.e. start and end , plus 3 scheduled

29 % times per meal (3 per day ) in 7 days

30 t = zeros (2+7*3*3 ,1);

31

32 % Relevant moments are calculated

Page 87: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 77

33 t(1) = 0;

34 t(2) = bHour *60+bMin -insulinTime ;

35 t(3) = bHour *60+ bMin;

36 t(4) = bHour *60+ bMin+eatingTime ;

37 t(5) = lHour *60+lMin -insulinTime ;

38 t(6) = lHour *60+ lMin;

39 t(7) = lHour *60+ lMin+eatingTime ;

40 t(8) = dHour *60+dMin -insulinTime ;

41 t(9) = dHour *60+ dMin;

42 t(10) = dHour *60+ dMin+eatingTime ;

43

44 % Sets up the following days

45 for i=11: length (t)

46 t(i) = t(i-9) +24*60;

47 end

48

49 t(end ) = 24*7*60;

50

51 V_G = 1.49; % [dL/kg]

52 k_1 = 0.042; % [min ^-1]

53 k_2 = 0.071; % [min ^-1]

54 V_I = 0.04; % [L/kg]

55 m_1 = 0.379; % [min ^-1]

56 m_2 = 0.673; % [min ^-1]

57 m_4 = 0.269; % [min ^-1]

58 m_5 = 0.0526; % [min *kg/pmol]

59 m_6 = 0.8118; % [-]

60 HE_b = 0.6; % [-]

61 k_max = 0.0465; % [min ^-1]

62 k_min = 0.0076; % [min ^-1]

63 k_abs = 0.023; % [min ^-1]

64 k_gri = 0.0465; % [min ^-1]

65 f = 0.90; % [-]

66 a = 0.00006; % [mg^-1]

67 b = 0.68; % [-]

68 c = 0.00023; % [mg^-1]

69 d = 0.09; % [-]

70 k_p1 = 3.09; % [mg/kg/min ]

71 k_p2 = 0.0007; % [min ^-1]

72 k_p3 = 0.005; % [mg/kg/min per pmol/L]

73 k_p4 = 0.0786; % [mg/kg/min per pmol/kg]

74 k_i = 0.0066; % [min ^-1]

75 F_cns = 1; % [mg/kg/min ]

76 V_m0 = 4.65; % [mg/kg/min ]

77 V_mx = 0.034; % [mg/kg/min per pmol/L]

78 K_m0 = 466.21; % [mg/kg]

79 p_2U = 0.084; % [min ^-1]

80 K = 0.99; % [pmol/kg per mg/dL]

81 alpha = 0.013; % [min ^-1]

82 beta = 0.05; % [pmol/kg/min per mg/dL]

83 gamma = 0.5; % [min ^-1]

84 k_e1 = 0.0007; % [min ^-1]

85 k_e2 = 269; % [mg/kg]

86

Page 88: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

78 MATLAB code

87 p = [V_G ;k_1;k_2;V_I;m_1;m_2 ;m_4;m_5;m_6 ;HE_b;k_max;k_min;k_abs ;

k_gri ;...

88 f;a;b;c;d;k_p1;k_p2;k_p3;k_p4;k_i;F_cns;V_m0;V_mx;K_m0;p_2U;K;alpha

;...

89 beta;gamma;k_e1;k_e2;BW];

90

91 odeOptions = [];

92 options = optimset (’Display ’,’off ’);

93

94 u = 7.15;

95

96 % Calculate the steady state values

97 xStart = zeros (12 ,1) ;

98 xInitial0 = fsolve (@CobelliWrap ,xStart ,options ,u,0,p);

99

100 waitbarHandle = waitbar (0,’Modelling Cobelli , please wait... ’);

101

102 % Midnight to first insulinshot

103 [T1 ,X1]= ode15s (@Cobelli ,[t(1) t(2) ],xInitial0 ,odeOptions ,u,0,p);

104

105 T = T1;

106 X = X1(:,1);

107

108 waitbar (1/65 , waitbarHandle);

109

110 for i=2:9: length (t) -1

111

112 % Insulinshot before breakfast

113 if(i==2)

114 xInitial1 = X1(end ,:) ’;

115 xInitial1 (3) = xInitial1 (3)+bInsulin ;

116 else

117 xInitial1 = X10(end ,:) ’;

118 xInitial1 (3) = xInitial1 (3)+bInsulin ;

119 end

120

121 [T2,X2]= ode15s (@Cobelli ,[t(i) t(i+1) ],xInitial1 ,odeOptions ,u,0,

p);

122

123 % Breakfast start

124 xInitial2 = X2(end ,:) ’;

125 [T3,X3]= ode15s (@Cobelli ,[t(i+1) t(i+2)],xInitial2 ,odeOptions ,u,

bCHO/eatingTime ,p);

126

127 % Breakfast stop

128 xInitial3 = X3(end ,:) ’;

129 [T4,X4]= ode15s (@Cobelli ,[t(i+2) t(i+3)],xInitial3 ,odeOptions ,u

,0,p);

130

131 % Insulinshot before lunch

132 xInitial4 = X4(end ,:) ’;

133 xInitial4 (3) = xInitial4 (3) +lInsulin ;

134 [T5,X5]= ode15s (@Cobelli ,[t(i+3) t(i+4)],xInitial4 ,odeOptions ,u

,0,p);

135

Page 89: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

A.2 GUI 79

136 % Lunch start

137 xInitial5 = X5(end ,:) ’;

138 [T6,X6]= ode15s (@Cobelli ,[t(i+4) t(i+5)],xInitial5 ,odeOptions ,u,

lCHO/eatingTime ,p);

139

140 % Lunch stop

141 xInitial6 = X6(end ,:) ’;

142 [T7,X7]= ode15s (@Cobelli ,[t(i+5) t(i+6)],xInitial6 ,odeOptions ,u

,0,p);

143

144 % Insulinshot before dinner

145 xInitial7 = X7(end ,:) ’;

146 xInitial7 (3) = xInitial7 (3)+ dInsulin ;

147 [T8,X8]= ode15s (@Cobelli ,[t(i+6) t(i+7)],xInitial7 ,odeOptions ,u

,0,p);

148

149 % Dinner start

150 xInitial8 = X8(end ,:) ’;

151 [T9,X9]= ode15s (@Cobelli ,[t(i+7) t(i+8)],xInitial8 ,odeOptions ,u,

dCHO/eatingTime ,p);

152

153 % Dinner stop

154 xInitial9 = X9(end ,:) ’;

155 [T10 ,X10 ]= ode15s (@Cobelli ,[t(i+8) t(i+9)],xInitial9 ,odeOptions ,

u,0,p);

156

157 T = [T;T2;T3;T4;T5;T6;T7;T8;T9;T10 ];

158 X = [X;X2(:,1);X3 (:,1);X4(:,1);X5(:,1);X6(:,1);X7(:,1) ;X8(:,1)

;...

159 X9(:,1);X10 (:,1) ];

160

161 waitbar ((i+9) /65, waitbarHandle);

162 end

163

164 close (waitbarHandle);

165

166 G = X/V_G;

Page 90: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

80 MATLAB code

Page 91: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

Bibliography

Page 92: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

82 BIBLIOGRAPHY

Page 93: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

Bibliography

[1] Netdoktor - Jan Erik Henriksen & Henning Bech-Nielsen (2007):Generelt om sukkersyge (Diabetes)http://www.netdoktor.dk/sygdomme/fakta/diabetes.htm

[2] Torben V. Schroeder, Svend Schulze, Jannik Hilsted, Jan AldershvileHypoglykæmi (s.94-95)Basisbog i medicin og kirurgi, 3rd edition, 4th printing, 2005, danishISBN 87-628-0402-2

[3] Helge Elbrønd Jensen, Poul Hjorth, Steen Markvorsen, Wolfhard KliemLineær differentialligning af første orden (s.1.7-1.8)4rd edition, 2th printing, 2000, danishISBN 87-88764-56-7

[4] Gianni Marchetti, Massimiliano Barolo, Lois Jovanovic, Howard Zisser,Dale E. Seborg (2007):A feedforward–feedback glucose control strategy for type 1 diabetes mellitus

[5] Image: - Insulin and glucagon regulate blood sugar. (2007)http://www.endocrineweb.com/insulin.html

[6] Encyclopædia Britannica Online. 19 May 2008.Encyclopædia Britannica - Pancreashttp://www.britannica.com/eb/article-9058232

[7] Roman Hovorka, Valentina Canonico, Ludovic J Chassin, Ulrich Haueter,Massimo Massi-Benedetti, Marco Orsini Federici, Thomas R Pieber, HelgaC Schaller, Lukas Schaupp, Thomas Vering and Malgorzata E Wilinska(2004):

Page 94: Modeling and Simulation of Glucose-Insulin Dynamicsetd.dtu.dk/thesis/221033/bac08_18.pdf · Modeling and Simulation of Glucose-Insulin ... These two types are known as Diabetes mellitus

84 BIBLIOGRAPHY

Nonlinear model predictive control of glucose concentration in subjects withtype 1 diabeteshttp://www.iop.org/EJ/abstract/0967-3334/25/4/010/

[8] Iva Marija Tolicd, Erik Mosekilde & Jeppe Sturis (2000):Modeling the Insulin Glucose Feedback System: The Significance of Pul-satile Insulin Secretion

[9] Chiara Dalla Man, Robert A. Rizza & Claudio Cobelli (2007):Meal Simulation Model of the Glucose-Insulin SystemIEEE Transactions On Biomedical Engineering, Vol. 54, No. 10, October2007