-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample-6.10-compartment_response.py
85 lines (69 loc) · 2.13 KB
/
example-6.10-compartment_response.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# example-6.7-compartment_response.py - Compartment model response
# RMM, 24 Nov 2024
#
# Figure 6.10: Response of a compartment model to a constant drug
# infusion. A simple diagram of the system is shown in (a). The step
# response (b) shows the rate of concentration buildup in compartment 2. In
# (c) a pulse of initial concentration is used to speed up the response.
import control as ct
import numpy as np
import matplotlib.pyplot as plt
ct.use_fbs_defaults()
#
# System dynamics
#
# Parameter settings for the model
k0 = 0.1
k1 = 0.1
k2 = 0.5
b0 = 1.5
# Compartment model definition
Ada = np.array([[-k0 - k1, k1], [k2, -k2]])
Bda = np.array([[b0], [0]])
Cda = np.array([[0, 1]])
compartment = ct.ss(Ada, Bda, Cda, 0);
# Set up the plotting grid to match the layout in the book
fig = plt.figure(constrained_layout=True)
gs = fig.add_gridspec(4, 3)
#
# (a) Step input
#
timepts = np.linspace(0, 50)
input = np.ones(timepts.size) * 0.1
response = ct.forced_response(compartment, timepts, input)
ax = fig.add_subplot(gs[0, 1])
ax.set_title("(b) Step input")
ax.plot(response.time, response.outputs)
ax.axhline(response.outputs[-1], color='k', linewidth=0.5)
ax.set_ylabel("Concentration $C_2$")
ax.axis('tight')
ax.axis([0, 50, 0, 2])
ax = fig.add_subplot(gs[1, 1])
ax.plot(response.time, response.inputs)
ax.set_xlabel("Time $t$ [min]")
ax.set_ylabel("Input dosage")
ax.axis('tight')
ax.axis([0, 50, 0, 0.4])
#
# (b) Pulse input
#
timepts = np.linspace(0, 50, 200)
input = np.ones(timepts.size) * 0.1
input[:20] = 0.3 # Increase value for first 5 seconds
response = ct.forced_response(compartment, timepts, input)
ax = fig.add_subplot(gs[0, 2])
ax.set_title("(c) Pulse input")
ax.plot(response.time, response.outputs)
ax.axhline(response.outputs[-1], color='k', linewidth=0.5)
ax.set_ylabel("Concentration $C_2$")
ax.axis('tight')
ax.axis([0, 50, 0, 2])
ax = fig.add_subplot(gs[1, 2])
ax.plot(response.time, response.inputs)
ax.set_xlabel("Time $t$ [min]")
ax.set_ylabel("Input dosage")
ax.axis('tight')
ax.axis([0, 50, 0, 0.4])
# Save the figure
fig.align_ylabels()
plt.savefig("figure-6.10-compartment_response.png", bbox_inches='tight')