-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiao_ODE.m
364 lines (305 loc) · 11.1 KB
/
Miao_ODE.m
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
%% Mapping from filenames to exercise number
clear; clc; close all;
% 'Exercise A,B' = exercise 2 on/off. Velocity = 13.4 Km/hr
% 'Exercise C,D' = exercise 3 on/off. velocity = 14.4 Km/hr
% 'Exercise E,F' = exercise 4 on/off. velocity = 15.7 Km/hr
wkdir = 'S1_Dataset';
%{
Exercise 2
HR2_on = importdata(fullfile(wkdir, 'Exercise A.txt'));
HR2_off = importdata(fullfile(wkdir, 'Exercise B.txt'));
% Exercise 3
HR3_on = importdata(fullfile(wkdir, 'Exercise C.txt'));
HR3_off = importdata(fullfile(wkdir, 'Exercise D.txt'));
% Exercise 4
HR4_on = importdata(fullfile(wkdir, 'Exercise E.txt'));
HR4_off = importdata(fullfile(wkdir, 'Exercise F.txt'));
%}
HR2_on = importdata('Exercise A.txt');
HR2_off = importdata('Exercise B.txt');
HR3_on = importdata('Exercise C.txt');
HR3_off = importdata('Exercise D.txt');
HR4_on = importdata('Exercise E.txt');
HR4_off = importdata('Exercise F.txt');
%%% Retrieve initial heart rates for solving ODE for each exercise
HR2_on_initcond = HR2_on(1,2);
HR3_on_initcond = HR3_on(1,2);
HR4_on_initcond = HR4_on(1,2);
HR2_off_initcond = HR2_off(1,2);
HR3_off_initcond = HR3_off(1,2);
HR4_off_initcond = HR4_off(1,2);
%%% Define oxygen demand (these are taken from table 1)
D2_on = 156;
D2_off = 72;
D3_on = 166;
D3_off = 72;
D4_on = 175;
D4_off = 70; % The paper says this is non-constant. Maybe try adjusting this?
%%% Define time span for each exercise (taken from S1_dataset)
HR2on_tend = find_tend(HR2_on);
HR2off_tend = find_tend(HR2_off);
HR3on_tend = find_tend(HR3_on);
HR3off_tend = find_tend(HR3_off);
HR4on_tend = find_tend(HR4_on);
HR4off_tend = find_tend(HR4_off);
%%
%import txt files
a = importdata('Exercise A.txt');
b = importdata('Exercise B.txt');
c = importdata('Exercise C.txt');
d = importdata('Exercise D.txt');
e = importdata('Exercise E.txt');
f = importdata('Exercise F.txt');
g = importdata('Exercise G.txt');
h = importdata('Exercise H.txt');
%resize matrix to remove zeros
at = a(1:962,1);
ahr = a(1:962,2);
bt = b(1:705,1);
bhr = b(1:705,2);
ct = c(1:922,1);
chr = c(1:922,2);
dt = d(1:734,1);
dhr = d(1:734,2);
et = e(1:933,1);
ehr = e(1:933,2);
ft = f(1:786,1);
fhr = f(1:786,2);
gt = g(1:860,1);
ghr = g(1:860,2);
ht = h(1:748,1);
hhr = h(1:748,2);
%add time from exercise time vector to recovery vector
btnew = bt + at(end,1);
dtnew = dt+ct(end,1);
ftnew = ft + et(end,1);
htnew = ht + gt(end,1);
%append time and heart rate vectors
ex2t = [at; btnew];
ex2hr = [ahr; bhr];
ex3t = [ct; dtnew];
ex3hr = [chr; dhr];
ex4t = [et; ftnew];
ex4hr = [ehr; fhr];
ex5t = [gt; htnew];
ex5hr = [ghr; hhr];
%% Solve ODE for exercise 2
%%% Solve for on state
tspan = [0, HR2on_tend];
x0 = HR2_on_initcond; % CHANGE THIS VARIABLE FOR EXERCISES
D = D2_on; % CHANGE THIS VARIABLE FOR EXERCISES
[t_on,HR2on_fit] = ode23(@(t,x) odeFun(t,x,D), tspan, x0);
[t_on2,HR2on_fit_newb] = ode23(@(t,x) odeFun2(t,x,D), tspan, x0); %with changed b value
%%% Solve for off state
tspan = [HR2on_tend, HR2off_tend];
x0 = HR2on_fit(end); % set this equal to the final value of ON state
D = D2_off; % CHANGE THIS VARIABLE FOR EXERCISES
[t_off,HR2off_fit] = ode23(@(t,x) odeFun(t,x,D), tspan, x0);
[t_off2,HR2off_fit_newb] = ode23(@(t,x) odeFun2(t,x,D), tspan, x0); %with changed b value
%%% Plot ODE solution for exercise 2
figure;
% Plot ON state
plot(t_on, HR2on_fit, 'LineWidth', 3); hold on;
% Overlay OFF state
plot(t_off, HR2off_fit, 'LineWidth', 3);
%Plot Raw Data
%plot(ex2t, ex2hr);
%Plot On state with changed b value
plot(t_on2, HR2on_fit_newb, 'LineWidth', 3);
%Plot Off state with changed b value
plot(t_off2, HR2off_fit_newb, 'LineWidth', 3);
legend('On-Transient Normal', 'Off-Transient Normal', 'On-Transient Increased C','Off-Transient Increased C')
xlabel('Time (s)','FontSize',14);
ylabel('Heart Rate (bpm)','FontSize',14);
title('Exercise 2 - Fitted ODE');
grid on;
xlim([t_on(1), t_off(end)]);
%% Solve ODE for exercise 3
%%% Solve for on state
tspan = [0, HR3on_tend];
x0 = HR3_on_initcond; % CHANGE THIS VARIABLE FOR EXERCISES
D = D3_on; % CHANGE THIS VARIABLE FOR EXERCISES
[t_on,HR3on_fit] = ode23(@(t,x) odeFun(t,x,D), tspan, x0);
[t_on2,HR3on_fit_newb] = ode23(@(t,x) odeFun2(t,x,D), tspan, x0); %with changed b value
%%% Solve for off state
tspan = [HR3on_tend, HR3off_tend];
x0 = HR3on_fit(end); % set this equal to the final value of ON state
D = D3_off; % CHANGE THIS VARIABLE FOR EXERCISES
[t_off,HR3off_fit] = ode23(@(t,x) odeFun(t,x,D), tspan, x0);
[t_off2,HR3off_fit_newb] = ode23(@(t,x) odeFun2(t,x,D), tspan, x0); %with changed b value
%%% Plot ODE solution for exercise 2
figure;
% Plot ON state
plot(t_on, HR3on_fit, 'LineWidth', 3); hold on;
% Overlay OFF state
plot(t_off, HR3off_fit, 'LineWidth', 3);
% Plot Raw Data
plot(ex3t,ex3hr);
%Plot On state with changed b value
plot(t_on2, HR3on_fit_newb, 'LineWidth', 3);
%Plot Off state with changed b value
plot(t_off2, HR3off_fit_newb, 'LineWidth', 3);
legend('On-Transient Normal', 'Off-Transient Normal', 'Raw Data', 'On-Transient Decreased B','Off-Transient Decreased B')
xlabel('Time (s)','FontSize',14);
ylabel('Heart Rate (bpm)','FontSize',14);
title('Exercise 3 - Fitted ODE');
grid on;
xlim([t_on(1), t_off(end)]);
%% Solve ODE for exercise 4
%%% Solve for on state
tspan = [0, HR4on_tend];
x0 = HR4_on_initcond; % CHANGE THIS VARIABLE FOR EXERCISES
D = D4_on; % CHANGE THIS VARIABLE FOR EXERCISES
[t_on,HR4on_fit] = ode23(@(t,x) odeFun(t,x,D), tspan, x0);
[t_on2,HR4on_fit_newb] = ode23(@(t,x) odeFun2(t,x,D), tspan, x0); %with changed b value
[t_on3,HR4on_fit_newc] = ode23(@(t,x) odeFun3(t,x,D), tspan, x0); %with changed c value
%%% Solve for off state
tspan = [HR4on_tend, HR4off_tend];
x0 = HR4on_fit(end); % set this equal to the final value of ON state
D = D4_off; % CHANGE THIS VARIABLE FOR EXERCISES
[t_off,HR4off_fit] = ode23(@(t,x) odeFun(t,x,D), tspan, x0);
[t_off2,HR4off_fit_newb] = ode23(@(t,x) odeFun2(t,x,D), tspan, x0); %with changed b value
[t_off3,HR4off_fit_newc] = ode23(@(t,x) odeFun3(t,x,D), tspan, x0); %with changed c value
%%% Plot ODE solution for exercise 2
figure;
% Plot ON state
plot(t_on, HR4on_fit,'b', 'LineWidth', 3); hold on;
% Overlay OFF state
plot(t_off, HR4off_fit,'--b', 'LineWidth', 3);
% Plot Raw Data
%plot(ex4t,ex4hr);
%Plot On state with changed b value
plot(t_on2, HR4on_fit_newb,'r', 'LineWidth', 3);
%Plot Off state with changed b value
plot(t_off2, HR4off_fit_newb,'--r', 'LineWidth', 3);
plot(t_on3, HR4on_fit_newc,'k', 'LineWidth', 3);
plot(t_off3, HR4off_fit_newc,'--k', 'LineWidth', 3);
legend('On-Transient Normal', 'Off-Transient Normal', 'On-Transient Decreased B', 'Off-Transient Decreased B','On-Transient Increased C','Off-Transient Increased C','FontSize', 14);
%legend('On-Transient Normal', 'Off-Transient Normal', 'On-Transient Decreased B','Off-Transient Decreased B','On-Transient Increased C','Off-Transient Increased C','FontSize', 14);
xlabel('Time (s)','FontSize',20);
ylabel('Heart Rate (bpm)','FontSize',20);
title('Heart Rate Reponse to Exercise with Different B and C Values','FontSize', 20);
grid on;
xlim([t_on(1), t_off(end)]);
%% Eigenvalue 2
% Define constants (non-normalized)
A = 3.217e-8; % ( (beats/min)^(-3.38) ) / minute
B = 1.63; % slope for leaving/approaching HR_min (dimensionless)
C = 1.75; % slope for approaching/leaving HR_max (dimensionless)
E = 1.0; % gives plateu shape (dimensionless)
% Define HRmin and HRmax from data
HRmin = 40;
HRmax = 185;
%%% Define D(v) between min/max HR
D = linspace(HRmin, HRmax, 100);
% Define eigenvalue for HRmin < D < HRmax
Lbound = -A.*((D-HRmin).^B).*((HRmax-D)).^C;
% Plot eigenvalue
figure;
plot(D, Lbound);
xlabel('Oxygen Demand (D)');
ylabel('Eigenvalue');
title({'Eigenvalue','HRmin < D < HRmax'});
%%% Define D(v) > max HR
D = linspace(HRmax, (HRmax+10), 100);
% Define eigenvalue for D > HRmax
Lunbound = -A.*((D-HRmin).^B).*((HRmax-D)).^C;
% Define real and imaginary components
Lreal = real(Lunbound);
Limag = imag(Lunbound);
% Plot real and imaginary components of eigenvalue
figure;
scatter(Lreal, Limag, 'g*');
xlabel('Real');
ylabel('Imaginary');
title({'Eigenvalue','D > HRmax'});
% Plot eigenvalue
% figure;
% plot(D, Lunbound);
% xlabel('Oxygen Demand (D)');
% ylabel('Eigenvalue');
% title({'Eigenvalue','D > HRmax'});
%% Phase portrait of the ODE.
%{
% Create meshgrid to sweep HR and D
[X1,X2] = meshgrid(0:0.05:1);
% Apply HR,D to ODE
xs = arrayfun(@(x,y) {odeFun([],[x,y])}, X1, X2);
% Solve for normalized HR
HRs = cellfun(@(x) x(1), xs);
% Plot the solved HR values against X2
figure;
quiver(X1, HRs);
xlabel('HR(v,t) normalized')
ylabel('D(v,t) normalized')
axis tight equal;
% xticks([0,1]);
% yticks([0,1]);
%}
%% Define ODE
function [dhrdt] = odeFun(~, x, D)
% Input ~: this is a dummy variable that Matlab requires as a placeholder
% Input x: this is A array
% x(1) = heart rate (HR) (beats/minute)
% x(2) = oxygen demand (D(v,t)) (beats/minute)
% Define absolute HR min/max
HRmin = 40;
HRmax = 185;
% Define constants (non-normalized)
A = 3.217e-8; % ( (beats/min)^(-3.38) ) / minute
B = 1.63; % slope for leaving/approaching HR_min (dimensionless)
C = 1.75; % slope for approaching/leaving HR_max (dimensionless)
E = 1.0; % gives plateu shape (dimensionless)
%%% Define non-normalized ODE
% d/dt(hr) = A * [hr - HRmin]^B * [HRmax - hr]^C * [D(v,t) - hr]^E
HR = x(1);
dhrdt = A.*(( HR-HRmin ).^(B)) .* ((HRmax-x(1)).^C) .* ((D-HR).^E);
end
%% Define ODE for Changed B value
function [dhrdt] = odeFun2(~, x, D)
% Input ~: this is a dummy variable that Matlab requires as a placeholder
% Input x: this is A array
% x(1) = heart rate (HR) (beats/minute)
% x(2) = oxygen demand (D(v,t)) (beats/minute)
% Define absolute HR min/max
HRmin = 40;
HRmax = 185;
% Define constants (non-normalized)
A = 3.217e-8; % ( (beats/min)^(-3.38) ) / minute
B = 1.53; % slope for leaving/approaching HR_min (dimensionless)
C = 1.75; % slope for approaching/leaving HR_max (dimensionless)
E = 1.0; % gives plateu shape (dimensionless)
%%% Define non-normalized ODE
% d/dt(hr) = A * [hr - HRmin]^B * [HRmax - hr]^C * [D(v,t) - hr]^E
HR = x(1);
dhrdt = A.*(( HR-HRmin ).^(B)) .* ((HRmax-x(1)).^C) .* ((D-HR).^E);
end
%% Define ODE with changed C
function [dhrdt] = odeFun3(~, x, D)
% Input ~: this is a dummy variable that Matlab requires as a placeholder
% Input x: this is A array
% x(1) = heart rate (HR) (beats/minute)
% x(2) = oxygen demand (D(v,t)) (beats/minute)
% Define absolute HR min/max
HRmin = 40;
HRmax = 185;
% Define constants (non-normalized)
A = 3.217e-8; % ( (beats/min)^(-3.38) ) / minute
B = 1.63; % slope for leaving/approaching HR_min (dimensionless)
C = 1.85; % slope for approaching/leaving HR_max (dimensionless)
E = 1.0; % gives plateu shape (dimensionless)
%%% Define non-normalized ODE
% d/dt(hr) = A * [hr - HRmin]^B * [HRmax - hr]^C * [D(v,t) - hr]^E
HR = x(1);
dhrdt = A.*(( HR-HRmin ).^(B)) .* ((HRmax-x(1)).^C) .* ((D-HR).^E);
end
%% Find the last recording for each dataset
function [tend] = find_tend(recording)
% data: the raw dataset
% this will be the first zero element
[~,ind] = min(recording(:,2));
% the previous index is the final data point
ind = ind-1;
% take respective timestamp
tend = recording(ind,1);
end