-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_code.m
More file actions
290 lines (247 loc) · 5.98 KB
/
main_code.m
File metadata and controls
290 lines (247 loc) · 5.98 KB
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
function Trabajo_Final_GildeTomas_pruebas1New
clear all
close all
clc
%datos
mumax=1/240;
Ks=20;
Ki=100;
Y=0.67;
X0=2000;
S0=200;
tf=50;
%funcionesi
Cinit=[X0 S0];
tr=linspace(0,tf,100);
opciones=odeset('abstol', 1e-5, 'reltol', 1e-5);
[t,var]=ode45(@andrews,tr,Cinit,opciones); %llamar a la ODE
Xp=var(:,1); Sp=var(:,2);
mu=(mumax.*Sp)./(Sp+Ks+((Sp.^2)/Ki));
dXdtn=mu.*Xp;
OURn=((1-Y)/Y)*dXdtn;
error=0.03;
OURdata=(OURn+randn(size(OURn))*error); %Fake del our experimental
%iter number of mesurments
for j=1:10
OURexp2=OURdata(1:j:end);
tr2=tr(1:j:end);
N=length(tr2);
Q=norm(error*OURdata).^2/(N-3);
texp_n=linspace(0,30);
for i=1:length(texp_n)
Qour_real(i)=0.20;
end
for a=1:length(OURdata)
error_(a)=(0.20-OURdata(a))^2;
end
sse=cumsum(error_)
p=3
for b=1:length(OURdata)
N_=b
SSE_=sse(b)
ssquared(b)=SSE_/(N_-p)
Q_(b)=1/ssquared(b)
end
%Figure 3
texpn=linspace(0,30);
for i=1:length(texpn)
Qour_real(i)=0.20;
end
OUR_exp=normrnd(Qour_real,error)
%Grafica X %figura 3
%Figure 3 - COMPROBAR!!
texpn=linspace(0,30);
for i=1:length(texpn)
Qour_real(i)=0.20;
end
errorplot=0.01;
OUR_exp=normrnd(Qour_real,errorplot);
%DATOS EXPERIMENTALES
texp=linspace(0,50);
%Parámetros óptimos
[pop]=runnested([1/240 20 100],OURexp2)
mumax=pop(1);
Ks=pop(2);
Ki=pop(3);
mumax_(j)=mumax;
Ks_(j)=Ks;
Ki_(j)=Ki;
OURmod=((1-Y)/Y)*dXdtn;
%%
%Sensibilidad
%%
%mumax
tsens=linspace(0,50);
mumaxi=mumax;
mumax=mumaxi*1.01;
[t,yh]=ode45(@andrews,tsens,Cinit,opciones);
OURh=calculateOUR(yh);
mumax=mumaxi*0.99;
[t,yl]=ode45(@andrews,tsens,Cinit,opciones);
OURy=calculateOUR(yl);
mumax=mumaxi;
SensMU=(OURh(:,1)-OURy(:,1))/(mumax*0.02);
%%
%ks
Ksi=Ks;
Ks=Ksi*1.01;
[t,yh]=ode45(@andrews,tsens,Cinit,opciones);
OURh=calculateOUR(yh);
Ks=Ksi*0.99;
[t,yl]=ode45(@andrews,tsens,Cinit,opciones);
OURy=calculateOUR(yl);
Ks=Ksi;
SensKS=(OURh(:,1)-OURy(:,1))/(Ks*0.02);
%%
%ki
Kii=Ki;
Ki=Kii*1.01;
[t,yh]=ode45(@andrews,tsens,Cinit,opciones);
OURh=calculateOUR(yh);
Ki=Kii*0.99;
[t,yl]=ode45(@andrews,tsens,Cinit,opciones);
OURy=calculateOUR(yl);
Ki=Kii;
SensKi=(OURh(:,1)-OURy(:,1))/(Ki*0.02);
%%
%Calculate the sensitibity matrix
sens=[SensMU SensKS SensKi];
%FIM
%Dos columnas,seis filas
% 6. Calculation of the FIM ( Fisher Matrix Information)
%to do --> sens
fim=zeros(3,3);
for i=1:length(sens)
fim=fim+sens(i,:)'*1./Q*sens(i,:);
end
%mumax,ks,ki
standard_error=abs(sqrt(diag(inv(fim))))
frac_mu(j)=(standard_error(1)/mumax)*100
frac_ks(j)=(standard_error(2)/Ks)*100
frac_ki(j)=(standard_error(3)/Ki)*100
error_mu(j)=standard_error(1);
error_ks(j)=standard_error(2);
error_ki(j)=standard_error(3);
end
%%
%definición de función objetivo
function fval=runnested(p,OUR_c)
fval=fminsearch(@fobj_nest,p);
function fval=fobj_nest(p)
mumax=p(1); Ks=p(2); Ki=p(3);
[tm,varm]=ode45(@andrews,tr2,Cinit,opciones);
Xp_opt=varm(:,1); Sp_opt=varm(:,2);
mu_opt=(mumax.*Sp_opt)./(Sp_opt+Ks+((Sp_opt.^2)/Ki));
dXdtn_opt=mu_opt.*Xp_opt;
OURmod=((1-Y)/Y)*dXdtn_opt;
fval=norm(OURmod-OUR_c);
end
end
function f=fobj(p)
mumax=p(1); Ks=p(2); Ki=p(3);
[tm,varm]=ode45(@andrews,texp,Cinit,opciones);
Xp_opt=varm(:,1); Sp_opt=varm(:,2);
mu_opt=(mumax.*Sp_opt)./(Sp_opt+Ks+((Sp_opt.^2)/Ki));
dXdtn_opt=mu_opt.*Xp_opt;
OURmod=((1-Y)/Y)*dXdtn_opt;
f=norm(OURmod-OURdata);
end
function [OUR]=calculateOUR(var)
%Calculate the our for each andrews
Xp=var(:,1); Sp=var(:,2);
mu=(mumax.*Sp)./(Sp+Ks+((Sp.^2)/Ki));
dXdtn=mu.*Xp;
OUR=((1-Y)/Y)*dXdtn;
end
% funcion Andrews
function [dvar OUR]=andrews(t,var)
X=var(1);
S=var(2);
mu=(mumax*S)/(S+Ks+((S^2)/Ki));
dvar=zeros(2,1);
dvar(1)=mu*X;
dvar(2)=(-1/Y)*dvar(1);
end
%%
%if needed
ERROR=OURdata-OURmod;
SSE=(sum(ERROR))^2;
N=length(ERROR);
p=3;
s2=(SSE)/(N-p);
S2_IN=1/s2;
medidas=linspace(0,5,10);
one_medidas=ones(length(medidas));
mumax_real=6;
mumax_real=mumax_real*one_medidas;
ks_real=20;
ks_real=ks_real*one_medidas;
ki_real=100;
ki_real=ki_real*one_medidas;
%
mumax_=mumax_*(60*24)
Ks_;
%hacemos vectores tan largos como m=10
ki_realsum=(ki_real(1)+ki_real(2)+ki_real(3)+ki_real(4)+ki_real(5)+ki_real(6)+ki_real(7)+ki_real(8)+ki_real(9)+ki_real(10));
%%
%PLOTS
figure(1)
plot(t,OURn)
figure(2)
plot(t,Sp,'r')
title('')
figure(3)
plot(t,Xp,'g')
%FIGURE 2
figure(4)
line(t,OURn)
hold on
plot(t,OURdata,'.b')
title('OUR profiles: reference (solid line) and experimental (dots)')
xlabel('Time (min)')
ylabel('OUR (mgDO/(L*min)')
%FIGURE 3
figure(5)
plot(texpn, Qour_real, texpn, OUR_exp, '--')
xlim([0 30])
ylim([0.10 0.30])
title('Example of Qour estimation')
xlabel('Time (min)')
ylabel('OUR (mgDO/(L*min)')
%FIGURE 4 left
figure(6)
plot(medidas,mumax_real,'k')
hold on
plot(medidas,mumax_,'*k')
plot(medidas,ks_real,'b')
plot(medidas,Ks_,'*b')
plot(medidas,ki_real,'r')
plot(medidas,Ki_,'*r')
title('Influence of the measurement on the estimated parameter value')
legend('Real mumax', 'mumax','Real Ks', 'Ks', 'Real Ki', 'Ki')
xlabel('Measurement interval (min)')
ylabel('Parameter value')
%FIGURE 4 right
figure(7)
plot(medidas,frac_mu,'k*')
hold on
plot(medidas,frac_ks,'r*')
plot(medidas,frac_ki,'b*')
title('Confidence Interval Assessment')
legend('mumax','Ks','Ki')
xlabel('Measurement interval (min)')
ylabel('σ(θ)/θ')
%Figure 5
figure(8)
plot(tsens, SensKi, tsens, SensKS)
title('Sensitivity Functions of OUR with respect to Ks and Ki')
legend('Ks', 'Ki')
xlabel('Time (min)')
ylabel('Ymumax')
figure(9)
plot(tsens, SensMU)
title('Sensitivity Functions of OUR with respect to mumax')
legend('mumax')
xlabel('Time (min)')
ylabel('Ymumax')
end