-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
220 lines (158 loc) · 4.82 KB
/
main.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
clear all
close all
%% Task 1.1.
% load parameters
load('p.mat', 'p')
time = [0 10];
% definition of only R1_0 as we need the initCond vector and the output
% vector to be the same length because of the ode function
y0 = [3];
% calculation using ODE function
[time, R1_ode] = ode45(@(t,initCond) model1(t,initCond,p), time, y0);
% calculation using Euler_method
% new timeRange needed to make sure R1_euler and time are the same length
% [R1_euler, timeRange] = euler_ode_solv(time, 40, y0, p); --> included in
% following test
% testing if higher N improve the result
for i = 1:5
[R1_euler{i}, timeRange{i}] = euler_ode_solv(time, (i*20), y0, p);
end
% quantifying the error
% scanning of different cell elemtents (different N)
for i = 1:5
% scanning timeRange for timepoints 1 to 10 and saving as idx_euler
for j = 1:10
idx_euler = find(timeRange{i} == j);
for m = 1:49
%scanning time for timepoints
if time(m)> timeRange{i}(idx_euler)
% calculating error
error(i,j) = (R1_euler{i}(idx_euler) - ((R1_ode(m-1)+R1_ode(m))/2))/((R1_ode(m-1)+R1_ode(m))/2);
break
end
end
end
end
% testing for large values of t
% numerical approach
largeTimeRange = linspace(0,1000,5000);
[largeR_euler, largeTime] = euler_ode_solv(largeTimeRange, 1000, y0, p);
% Equilibrium calculation
R1Eq = equilibriumCalc(1,y0,p);
% plotting
figure(1)
subplot(2,1,1)
plot(time, R1_ode(:,1), "k-", ...
timeRange{1}, R1_euler{1}, 'r--', ...
timeRange{2}, R1_euler{2}, 'g--', ...
timeRange{3}, R1_euler{3}, 'b--', ...
timeRange{4}, R1_euler{4}, 'c--', ...
timeRange{5}, R1_euler{5}, 'm--')
hold on
plot(10, largeR_euler(end), "ro", MarkerSize=10)
plot(10, R1Eq, "g+", MarkerSize=10)
title('Gene Expression')
xlabel('Time [min]')
ylabel('Gene Expression')
xlim([0 11])
ylim([0 12])
legend('R1 - ode45', ...
'R1 - euler (N = 20)', ...
'R1 - euler (N = 40)', ...
'R1 - euler (N = 60)', ...
'R1 - euler (N = 80)', ...
'R1 - euler (N = 100)', ...
'Prediction for large t', ...
'Solution for large t', ...
'Location', 'eastoutside');
subplot(2,1,2)
bars = bar(error);
title('Error quantification')
xlabel('Time [min]')
ylabel('Error [%]')
legend('R1 - euler (N = 20)', ...
'R1 - euler (N = 40)', ...
'R1 - euler (N = 60)', ...
'R1 - euler (N = 80)', ...
'R1 - euler (N = 100)', ...
'Location', 'eastoutside')
% making sure plots and bars have the same colors
colors = [1 0 0;
0 1 0;
0 0 1;
0 1 1;
1 0 1];
for i = 1:height(colors)
bars(i).FaceColor = colors(i,:);
end
%% Task 1.2.
% define initial conditions
p1 = 0.1:0.1:20;
% calculation using ode45
for i = 1:length(p1);
[time_odeU{i}, R1_odeU{i}] = ode45(@(t, y) submodel1(t, y, p, p1(i)), time, y0);
end
% calculation using Euler's method
for i = 1:length(p1);
[R1_eulerU{i}, timeRangeU{i}] = subtask1(time, 40, y0, p, p1(i));
end
% creating a dynamic plot with dynamic labels
figure(2)
set(gcf, 'Position', [100, 100, 1000, 600]);
subplot(2,1,1)
plot(time_odeU{1}, R1_odeU{1});
label_ode{1} = sprintf('p1 = %d', p1(1));
hold on
for i = 1:length(p1)/20;
plot(time_odeU{i*20}, R1_odeU{i*20});
hold on
label_ode{i+1} = sprintf('p1 = %d', p1(i*20));
end
legend(label_ode, 'Location', 'eastoutside');
xlabel('Time');
ylabel('Gene Expression');
title('ode45 calculation for different p1')
subplot(2,1,2)
plot(time_odeU{1}, R1_odeU{1});
label_euler{1} = sprintf('p1 = %d', p1(1));
hold on
for i = 1:length(p1)/20;
plot(timeRangeU{i*20}, R1_eulerU{i*20});
label_euler{i+1} = sprintf('p1 = %d', p1(i*20));
end
legend(label_euler, 'Location', 'eastoutside');
xlabel('Time');
ylabel('Gene Expression');
title('Euler calculation for different p1')
%% Task 2.1.
% Time range
timeR2 = [0 10];
% Initial conditions for R1 and R2
y0 = [3, 0];
% Calculation using ODE function
[time_odeR2, R] = ode45(@(t, y) task2model(t, y, p), timeR2, y0);
% Extract R2 from the ODE solution
R2_ode = R(:, 2);
% Calculation using Euler method (task2euler)
[R_euler, timeRange2] = task2euler(timeR2, 40, y0, p);
% Extract R2 from Euler method results (the second column)
R2_euler = R_euler(:, 2);
% Plot the results
figure(3);
plot(time_odeR2, R2_ode, 'r-', timeRange2, R2_euler, 'b--');
xlabel('Time');
ylabel('Gene Expression');
xlim([0 10]);
ylim([0 20]);
legend('R - ode45', 'R - Euler');
title('Comparison of R: ODE45 vs Euler Method');
%%
% in Aufgabe 3 reicht es nur ODE zu nutzen
% code muss nicht in den Bericht
% Ergebnisse müssen interpreteiert werden
% Präsentation so strukturieren, dass es am meisten Sinn macht, nicht so,
% wie die Aufgaben sortiert sind
% Im dePrüfung geht es mehr um die mathematischen Zusammenhänge nicht um
% Code
% Bei Task 3 Euler weglassen
% Bei Aufgabe 3.2 kann durch Ausprobieren gezeigt werden