Part 3 of 3 of the Artificial Pancreas project. Parts 1 & 2 (system identification with Neural Networks and ANFIS) are available at glucose-prediction-bergman-model.
This repository implements a Model Predictive Controller (MPC) for automatic insulin delivery in patients with Type 1 Diabetes Mellitus (T1DM), using the Bergman minimal model as the system dynamics.
The controller is designed to keep blood glucose within the safe physiological range of 70–180 mg/dL, tracking a reference of 100 mg/dL, while handling meal disturbances and respecting physical constraints on insulin delivery.
The Bergman minimal model describes glucose-insulin dynamics through three coupled ODEs:
dG/dt = -p1·(G - Gb) - X·G + D(t)
dX/dt = -p2·X + p3·(I - Ib)
dI/dt = -p4·(I - Ib) + u(t)
| Variable | Description | Units |
|---|---|---|
| G(t) | Blood glucose concentration | mg/dL |
| X(t) | Remote insulin action in tissues | min⁻¹ |
| I(t) | Plasma insulin concentration | mU/L |
| u(t) | Insulin infusion rate (control input) | mU/min |
| D(t) | Meal disturbance (exogenous) | — |
Model parameters: p1 = 0.01, p2 = 0.015, p3 = 2×10⁻⁶, p4 = 0.2, Gb = 80 mg/dL, Ib = 7 mU/L.
| Parameter | Value |
|---|---|
| Sampling time T | 5 min |
| Prediction horizon N | 12 steps (60 min) |
| State weight matrix Q | diag(1, 0, 0) |
| Control weight R | 0.01 |
| Optimization solver | IPOPT via CasADi |
| Internal model | Full nonlinear Bergman model |
Constraints enforced:
| Variable | Lower bound | Upper bound |
|---|---|---|
| G | 70 mg/dL | 180 mg/dL |
| X | 0 | ∞ |
| I | 0 | ∞ |
| u | 0 mU/min | 5 mU/min |
-
Initial condition: G₀ = 80 mg/dL (basal), X₀ = 0, I₀ = 7 mU/L
-
Reference: Gref = 100 mg/dL
-
Meal disturbance D = 2.0 applied between t = 60 and t = 120 min
-
Simulation time: 300 min
-
- Initial condition: G₀ = 60 mg/dL (below safe limit of 70 mg/dL)
- Reference: Gref = 100 mg/dL
- No meal disturbance
- The controller saturates at u = 0 (cannot administer glucagon); recovery is driven by the model's natural dynamics toward Gb = 80 mg/dL
-
- MATLAB R2021a or later
- CasADi (tested with v3.6.x) — add to MATLAB path before running
addpath('/path/to/casadi')% Simply run the main script
MPC.mTwo figure windows will appear:
- Panel MPC — G(t), X(t), I(t) and u(t) for the meal disturbance scenario
- Escenario Hipoglucemia — G(t), X(t) and u(t) for the hypoglycemia scenario
glucose-mpc-bergman/
│
├── README.md
├── MPC.m ← main controller script
│
├── report/
│ └── reporte_tecnico.pdf ← 2-page technical report
│
└── results/
├── escenario1_comida.png ← Scenario 1 figures
└── escenario2_hypo.png ← Scenario 2 figures