-
Notifications
You must be signed in to change notification settings - Fork 1
/
generateODEMM.m
1188 lines (1131 loc) · 56.2 KB
/
generateODEMM.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function varargout = generateODEMM(D,M,parameters,conditions,varargin)
% This function generates a file that defines the ODE-constrained mixture model.
%
% USAGE:
% M = generateODEMM(D,M,parameters,conditions,options)
%
% Parameters:
% M: model struct
% D: data struct
% parameters: parameters struct
% conditions: conditions struct obtained by collectConditions.m
% varargin:
% options: (options for the generation)
%
% Required fields of M:
% name: name of the model
% model: simulation file with input (T,theta,u) (e.g., generated by
% amiwrap of the toolbox AMICI), the first output needs to be
% the status of thesimulation (whether it failed or not) the 4th the
% simulation output, and the 6th the sensitivities
% sim_type: simulation type (''RRE' for a mechanistic description of the
% mean, e.g., by reaction rate equations, ''HO'' for a mechanistic description
% of the mean and covariance, e.g., by moment-closure approximation or
% sigma-point approximation)
% n_subpop: number of subpopulations
% distribution{s,e}: distribution assumption \n
% = ''norm'': normal distribution assumption \n
% = ''skew_norm'': skew normal distribution assumption\n
% = ''students_t'': Student's t distribution assumption\n
% = ''neg_binomial'': negative binomial distribution assumption\n
% = ''logn_median'': log-normal distribution assumption when mean of
% simulation linked to median of distribution \n
% = ''logn_mean'': log-normal distribution assumption when mean of
% simulation is linked to mean of distribution \n
% mean_ind{s,e}: indices of simulation output describing the mean of
% the measurand(s) of experiment e
% var_ind{s,e}: indices of simulation output describing the variance (empty
% if RREs used)
% u{s,e}: input vector describing differences between subpopulations
% and experiments
% sym: symbolic description of properties of the model with fields
% * w{s,e}: weights of subpopulation s in experiment e
% * theta: parameter needed for simulation of individual
% subpopulations
% * scaling{r,e}: scaling factor for replicate r in experiment e, if the
% replicates are not considered seperately, use r=1
% * offset{r,e}: offset parameter for replicate r in experiment e
%
% Optional fields of options:
% write_parameter: write parameter definition in file (true by
% default)
% measurement_noise: if measurement noise is included\n
% = true\n
% = false (default)
% replicates: if individual replicates are modeled\N
% = true \n
% = false (default)
% sigmas: parametrization of the variance in case of using only a
% mechanistic description of the mean \n
% = ''condition-dependent'': (default) assign sigma for every time point \n
% = ''time-independent'': sigma stays the same for subpopulation
% and some dosage \n
% = ''only-one'': only one sigma for everything
%
% Required fields of D:
% conditions: obtained by collectConditions.m
% todo: parameters not needed if they are not written in the file
options.write_parameters = true;
options.measurement_noise = false;
options.replicates = false;
options.path = [];
if nargin == 5
options = setdefault(varargin{1},options);
end
%% check M fields
if ~isfield(M,'mean_ind')
error('For M the field mean_ind needs to be defined!');
end
switch M.sim_type
case 'RRE'
for s = 1:M.n_subpop
for e = 1:length(D)
M.w_ind{s,e} = [];
M.var_ind{s,e} = [];
end
end
case 'HO'
if ~isfield(M,'var_ind')
error('For M the field var_ind needs to be defined!');
end
for s = 1:M.n_subpop
for e = 1:length(D)
M.w_ind{s,e} = [];
end
end
case 'MCM'
if ~isfield(M,'w_ind')
error('For M the field w_ind needs to be defined!');
end
if ~isfield(M,'var_ind')
error('For M the field var_ind needs to be defined!');
end
otherwise
error('M.sim_type needs to be ''HO'', ''RRE'' or ''MCM''!');
end
if strcmp(M.distribution{s,e},'students_t')
disp('Extend parameter vector by nu for student''s t distribution.');
parameters.name{end+1} = 'log_{10}(\nu)';
parameters.min(end+1) = log10(2);
parameters.max(end+1) = 2;
parameters.number = parameters.number+1;
nu_ind = parameters.number;
disp('Flag to write parameters in file set to true.');
options.write_parameters = true;
end
disp(['Start generation of ODEMM for ' M.name '...']);
dimension_all = 0;
for e = 1:length(D)
for s = 1:M.n_subpop
num_w = 0;
if strcmp(M.sim_type,'MCM')
num_w = M.n_subpop-1;
end
if length(M.mean_ind{s,e})+length(M.var_ind{s,e})+num_w > dimension_all
dimension_all = length(M.mean_ind{s,e})+...
length(M.var_ind{s,e})+num_w;
end
end
end
xi = sym(zeros(parameters.number,1));
for i = 1:parameters.number
xi(i) = sym(['xi_' num2str(i,'%d')]);
end
x = sym(zeros(dimension_all,1));
dxdxi = sym(zeros(dimension_all,1));
sigma = sym('sigma');
dsigmadxi = sym('dsigmadxi');
rho = sym('rho');
drhodxi = sym('drhodxi');
delta = sym('delta');
ddeltadxi = sym('ddeltadxi');
for i = 1:dimension_all
x(i) = sym(['x_' num2str(i,'%d')]);
dxdxi(i) = sym(['dxdxi_' num2str(i,'%d')]);
end
%% theta
str_theta = ['M.theta = @(xi,u) '];
str_theta = strcat(str_theta,strcat(replace_xi_x_u(M.sym.theta),';'));
%% dthetadxi
M.sym.dthetadxi = jacobian(M.sym.theta, xi);
str_dthetadxi = ['M.dthetadxi= @(xi,u) '];
str_dthetadxi = strcat(str_dthetadxi,strcat(replace_xi_x_u(M.sym.dthetadxi),';'));
for e = 1:length(D)
for s = 1:M.n_subpop
%% initialize sigma/rho
switch M.sim_type
case 'RRE'
switch M.distribution{s,e}
case {'norm','logn_mean','logn_median'}
switch options.sigmas
case 'subpopulation-specific'
if D(e).n_dim == 1
M.sym.sigma{s,e} = 10.^xi(ones(1,numel(D(e).t))*D(e).sigma{s});
else
M.sym.ind{s,e} = sym(D(e).Sigma{s});
end
case 'time-dependent'
if D(e).n_dim == 1
M.sym.sigma{s,e} = 10.^xi(D(e).sigma{s});
else
M.sym.ind{s,e} = sym(D(e).Sigma{s});
end
% case 'condition-dependent'
% if D(e).n_dim == 1
% M.sym.sigma{s,e} = 10.^xi(conditions(D(e).c(s,1)).sigma);
% else
% M.sym.ind{s,e} = sym(conditions(D(e).c(s,1)).Sigma);
% end
case {'time-independent','only-one'}
M.sym.sigma{s,e} = 10.^xi(ones(1,numel(D(e).t))*conditions(D(e).c(s,1)).sigma);
end
case 'neg_binomial'
switch options.rhos
case 'subpopulation-specific'
M.sym.rho{s,e} = xi(ones(1,numel(D(e).t))*D(e).rho{s});
case 'time-dependent'
M.sym.rho{s,e} = xi(D(e).rho{s});
% case 'condition-dependent'
% M.sym.rho{s,e} = xi(conditions(D(e).c(s,1)).rho);
case {'time-independent','only-one'}
M.sym.rho{s,e} = xi(ones(1,numel(D(e).t))*conditions(D(e).c(s,1)).rho);
otherwise
error('case not defined');
end
end
case {'HO','MCM'}
% definition of variance parameters and incorporation of measurement noise
switch M.distribution{s,e}
case 'norm'
if options.measurement_noise
M.sym.sigma{s,e} = sqrt(x(2)+M.sym.sigma_noise{e}.^2);
else
M.sym.sigma{s,e} = sqrt(x(2));
end
case 'logn_median'
if options.measurement_noise
switch options.noise_model
case 'multiplicative'
M.sym.sigma{s,e} = sqrt(log(x(2)/(x(1)^2)+1)+M.sym.sigma_noise{e}.^2);
case 'additive'
M.sym.sigma{s,e} = sqrt(log((x(2)+M.sym.sigma_noise{e}.^2)/(x(1)^2)+1));
end
else
M.sym.sigma{s,e} = sqrt(log(x(2)/(x(1)^2)+1));
end
case 'logn_mean'
if options.measurement_noise
M.sym.sigma{s,e} = sqrt(log(x(2)/(x(1)^2)+1)+M.sym.sigma_noise{e}.^2);
else
M.sym.sigma{s,e} = sqrt(log(x(2)/(x(1)^2)+1));
end
case 'neg_binomial'
if options.measurement_noise
M.sym.rho{s,e} = x(1)/(x(2)+M.sym.sigma_noise{e});
else
M.sym.rho{s,e} = x(1)/x(2);
end
case 'students_t'
M.sym.nu{s,e} = 10.^xi(nu_ind)*ones(size(D(e).t(:)));
% if options.measurement_noise
% error('to do: student t measurement noise')
% end
case 'skew_norm'
%M.sym.delta{s,e} = M.sym.delta{s,e};
% if options.measurement_noise
% error('to do: skew norm measurement noise')
% end
end
end
%% initialize mean parameters
switch M.distribution{s,e}
case {'norm','students_t'}
M.sym.mu{s,e} = x(1);
case 'logn_median'
M.sym.mu{s,e} = log(x(1));
case 'logn_mean'
M.sym.mu{s,e} = log(x(1))-(sigma.^2)/2;
case 'neg_binomial'
M.sym.tau{s,e} = rho.*x(1)./(1-rho);
case 'skew_norm'
M.sym.mu{s,e} = x(1)-sqrt(2/pi)*delta;
end
end
end
%% CONSTRUCT SIMULATION FILE
clear([options.path 'ODEMM_' M.name '.m']);
% Open file
fid = fopen([options.path 'ODEMM_' M.name '.m'],'w');
% Construct string
fprintf(fid,['%%%% Definition of model\n']);
fprintf(fid,['M.name = ''' M.name '''; \n']);
fprintf(fid,['M.n_subpop = ' num2str(M.n_subpop) '; \n']);
fprintf(fid,['M.model = ' char(M.model) '; \n']);
fprintf(fid,[str_theta '\n']);
fprintf(fid,[str_dthetadxi '\n\n']);
%% generate strings
for s = 1:M.n_subpop
for e = 1:length(D)
disp(['...generate definition of subpopulation ' num2str(s) ' in experiment ' num2str(e) '...']);
switch M.sim_type
case {'RRE','HO'}
%% w
str_w =['M.w{s,e} = @(t,x,xi,u) '];
str_w = strcat(str_w,strcat(replace_xi_x_u(M.sym.w{s,e}),';'));
%% dwdxi
str_dwdxi = strcat(['M.dwdxi{s,e} = @(t,x,dxdxi,xi,u)'], getStrDerivative2Terms(M.sym.w{s,e}, x, dxdxi, xi));
str_dwdxi = [str_dwdxi, ';'];
str_dwdxi = regexprep(str_dwdxi,'u_([0-9]+)','u($1)');
case 'MCM'
if s < M.n_subpop
ind_w = length(M.mean_ind{s,e}) + length(M.var_ind{s,e}) +1;
str_w = ['M.w{s,e} = @(t,x,xi,u) x(:,' num2str(ind_w) ');'];
str_dwdxi = ['M.dwdxi{s,e} = @(t,x,dxdxi,xi,u) permute(dxdxi(' num2str(ind_w) ',:,:),[3,2,1]);'];
else
ind_w = length(M.mean_ind{s,e}) + length(M.var_ind{s,e}) +1;
str_w = 'M.w{s,e} = @(t,x,xi,u) 1';
str_dwdxi = 'M.dwdxi{s,e} = @(t,x,dxdxi,xi,u)' ;
for z = 1:M.n_subpop-1
str_w = strcat(str_w,['-x(:,' num2str(ind_w) ')']);
if M.n_subpop > 2
error('TODO')
else
str_dwdxi = strcat(str_dwdxi,['-permute(dxdxi(' num2str(ind_w) ',:,:),[3,2,1])']);
end
ind_w = ind_w+1;
end
str_w = strcat(str_w,';');
str_dwdxi = strcat(str_dwdxi,';');
end
end
if D(e).n_dim == 1 || strcmp(M.distribution{s,e},'students_t') || ...
strcmp(M.distribution{s,e},'skew_norm')
switch M.distribution{s,e}
case {'norm','logn_mean','logn_median'}
%% mu
str_mu =['M.mu{s,e} = @(t,x,sigma,xi,u) ['];
str_temp = regexprep(char(M.sym.mu{s,e}),'xi_([0-9]+)','xi($1)');
str_temp = regexprep(str_temp,'\^','.^');
str_mu = strcat(str_mu,regexprep(str_temp,'x_([0-9]+)','x(:,$1)'));
str_mu = [str_mu '];'];
%% dmudxi
str_dmudxi = getStrDerivative3Terms('dmudxi',M.sym.mu,...
s,e,x,dxdxi,sigma,dsigmadxi,xi,'sigma');
case {'skew_norm'}
str_mu =['M.mu{s,e} = @(t,x,delta,xi,u) ['];
if D(e).n_dim == 1
%% mu
str_temp = regexprep(char(M.sym.mu{s,e}),'xi_([0-9]+)','xi($1)');
str_temp = regexprep(str_temp,'\^','.^');
str_mu = strcat(str_mu,regexprep(str_temp,'x_([0-9]+)','x(:,$1)'));
str_mu = [str_mu '];'];
%% dmudxi
str_dmudxi = getStrDerivative3Terms('dmudxi',M.sym.mu,...
s,e,x,dxdxi,delta,ddeltadxi,xi,'delta');
else
for n = 1:D(e).n_dim
str_mu = [str_mu 'x(:,' num2str(n) ') - sqrt(2/pi)*delta(' num2str(n) ')'];
if n < D(e).n_dim
str_mu = [str_mu ','];
else
str_mu = [str_mu '];\n'];
end
end
%% dmudxi
str_dmudxi = ['M.dmudxi{s,e} = @(t,x,dxdxi,delta,ddeltadxi,xi,u) func_dmudxi_' M.distribution{s,e}...
'(t,x,dxdxi,delta,ddeltadxi,xi,u,' num2str(D(e).n_dim) ');'];
end
case 'neg_binomial'
%% tau
str_tau =['M.tau{s,e} = @(t,x,rho,xi,u) ['];
str_temp = regexprep(char(M.sym.tau{s,e}),'xi_([0-9]+)','xi($1)');
str_temp = regexprep(str_temp,'\^','.^');
str_temp = regexprep(str_temp,'\*','.*');
str_temp = regexprep(str_temp,'\/','./');
str_tau = strcat(str_tau,regexprep(str_temp,'x_([0-9]+)','x(:,$1)'));
str_tau = [str_tau '];'];
%% dtaudxi
str_dtaudxi = getStrDerivative3Terms('dtaudxi', M.sym.tau, ...
s, e, x, dxdxi, rho, drhodxi, xi, 'rho');
%str_dtaudxi = 'M.dtaudxi{s,e} = @(t,x,dxdxi,rho,drhodxi,xi,u) bsxfun(@times,rho.^(-2), bsxfun(@times,permute(dxdxi(1,:,:),[3,2,1]),rho.*(1-rho))-bsxfun(@times,x(:,1),drhodxi));';
case 'students_t'
str_mu =['M.mu{s,e} = @(t,x,Sigma,xi,u) ['];
for n = 1:D(e).n_dim
str_mu = [str_mu 'x(:,' num2str(n) ')'];
if n < D(e).n_dim
str_mu = [str_mu ','];
else
str_mu = [str_mu '];\n'];
end
end
%% dmudxi
str_dmudxi = ['M.dmudxi{s,e} = @(t,x,dxdxi,xi,u) func_dmudxi_' M.distribution{s,e}...
'(t,x,dxdxi,xi,u,' num2str(D(e).n_dim) ');'];
end
else % multivariate
%% mu
str_mu =['M.mu{s,e} = @(t,x,Sigma,xi,u) ['];
switch M.distribution{s,e}
case 'logn_mean'
for n = 1:D(e).n_dim
str_mu = [str_mu 'log(x(:,' num2str(n) ')) - 0.5*Sigma(:,' num2str(n) ',' num2str(n) ')'];
if n < D(e).n_dim
str_mu = [str_mu ',...\n'];
else
str_mu = [str_mu '];\n'];
end
end
case 'logn_median'
for n = 1:D(e).n_dim
str_mu = [str_mu 'log(x(:,' num2str(n) '))'];
if n < D(e).n_dim
str_mu = [str_mu ','];
else
str_mu = [str_mu '];\n'];
end
end
case 'norm'
for n = 1:D(e).n_dim
str_mu = [str_mu 'x(:,' num2str(n) ')'];
if n < D(e).n_dim
str_mu = [str_mu ','];
else
str_mu = [str_mu '];\n'];
end
end
end
%% dmudxi
str_dmudxi = ['M.dmudxi{s,e} = @(t,x,dxdxi,Sigma,dSigmadxi,xi,u) func_dmudxi_' M.distribution{s,e}...
'(t,x,dxdxi,Sigma,dSigmadxi,xi,u,' num2str(D(e).n_dim) ');'];
end
if options.replicates
% consider replicates individually
rs = 1:length(D(e).replicate);
else
% consider scaled and merged replicates
rs = 1;
end
for r = rs
%% s,b
str_s{r} = 'M.scaling{r,e} = @(xi,u) ';
str_s{r} = strcat(str_s{r},strcat(replace_xi_x_u(M.sym.scaling{r,e}),';'));
str_b{r} = 'M.offset{r,e} = @(xi,u) ';
str_b{r} = strcat(str_b{r},strcat(replace_xi_x_u(M.sym.offset{r,e}),';'));
str_dsdxi{r} = 'M.dscalingdxi{r,e} = @(xi,u) ';
str_dbdxi{r} = 'M.doffsetdxi{r,e} = @(xi,u) ';
M.sym.dscalingdxi{r,e} = jacobian(M.sym.scaling{r,e},xi);
M.sym.doffsetdxi{r,e} = jacobian(M.sym.offset{r,e},xi);
str_dsdxi{r} = strcat(str_dsdxi{r},strcat(replace_xi_x_u(M.sym.dscalingdxi{r,e}),';'));
str_dbdxi{r} = strcat(str_dbdxi{r},strcat(replace_xi_x_u(M.sym.doffsetdxi{r,e}),';'));
end
%% dsigma dsigmadxi
if D(e).n_dim > 1 || strcmp(M.distribution{s,e},'students_t') || strcmp(M.distribution{s,e},'skew_norm')
switch M.sim_type
case {'HO','MCM'}
switch M.distribution{s,e}
case {'logn','logn_mean','logn_median'}
if options.measurement_noise
str_noise = '[';
for k = 1:D(e).n_dim
str_noise = strcat(str_noise,regexprep(char(M.sym.sigma_noise{e}(k).^2),'xi_([0-9]+)','xi($1)'));
str_noise = regexprep(str_noise,'u_([0-9]+)','u($1)');
if k < D(e).n_dim
str_noise = [str_noise ';'];
else
str_noise = [str_noise ']'];
end
end
str_dnoisedxi = '[';
M.sym.dnoisedxi{e} = jacobian(M.sym.sigma_noise{e}.^2,xi);
for l = 1:D(e).n_dim
for k = 1:length(M.sym.dnoisedxi{e})
str_dnoisedxi = strcat(str_dnoisedxi,regexprep(char(M.sym.dnoisedxi{e}(l,k)),'xi_([0-9]+)','xi($1)'));
str_dnoisedxi = regexprep(str_dnoisedxi,'u_([0-9]+)','u($1)');
if k<size(M.sym.dnoisedxi{e},2)
str_dnoisedxi = [str_dnoisedxi ','];
elseif k==size(M.sym.dnoisedxi{e},2) && l < D(e).n_dim
str_dnoisedxi = [str_dnoisedxi ';...\n'];
elseif k==size(M.sym.dnoisedxi{e},2) && l == D(e).n_dim
str_dnoisedxi = [str_dnoisedxi ']'];
end
end
end
str_sigma = ['M.Sigma{s,e} = @(t,x,xi,u) func_Sigma_logn(t,x,xi,' num2str(D(e).n_dim)...
',' str_noise ,',''' options.noise_model ''');'];
str_dsigmadxi = ['M.dSigmadxi{s,e} = @(t,x,dxdxi,xi,u) func_dSigmadxi_logn(t,x,dxdxi,xi,' num2str(D(e).n_dim)...
',' str_noise ',' str_dnoisedxi ',''' options.noise_model ''');'];
else
str_sigma = ['M.Sigma{s,e} = @(t,x,xi,u) func_Sigma_logn(t,x,xi,' num2str(D(e).n_dim) ');'];
str_dsigmadxi = ['M.dSigmadxi{s,e} = @(t,x,dxdxi,xi,u) func_dSigmadxi_logn(t,x,dxdxi,xi,' num2str(D(e).n_dim) ');'];
end
case {'norm','students_t'}
if options.measurement_noise
str_noise = '[';
for k = 1:D(e).n_dim
str_noise = strcat(str_noise,regexprep(char(M.sym.sigma_noise{e}(k).^2),'xi_([0-9]+)','xi($1)'));
str_noise = regexprep(str_noise,'u_([0-9]+)','u($1)');
if k < D(e).n_dim
str_noise = [str_noise ';'];
else
str_noise = [str_noise ']'];
end
end
str_dnoisedxi = '[';
M.sym.dnoisedxi{e} = jacobian(M.sym.sigma_noise{e}.^2,xi);
for l = 1:D(e).n_dim
for k = 1:length(M.sym.dnoisedxi{e})
str_dnoisedxi = strcat(str_dnoisedxi,regexprep(char(M.sym.dnoisedxi{e}(l,k)),'xi_([0-9]+)','xi($1)'));
str_dnoisedxi = regexprep(str_dnoisedxi,'u_([0-9]+)','u($1)');
if k<size(M.sym.dnoisedxi{e},2)
str_dnoisedxi = [str_dnoisedxi ','];
elseif k==size(M.sym.dnoisedxi{e},2) && l < D(e).n_dim
str_dnoisedxi = [str_dnoisedxi ';...\n'];
elseif k==size(M.sym.dnoisedxi{e},2) && l == D(e).n_dim
str_dnoisedxi = [str_dnoisedxi ']'];
end
end
end
str_sigma = ['M.Sigma{s,e} = @(t,x,xi,u) func_Sigma_' M.distribution{s,e} '(t,x,xi,' num2str(D(e).n_dim)...
',' str_noise ',''' options.noise_model ''');'];
str_dsigmadxi = ['M.dSigmadxi{s,e} = @(t,x,dxdxi,xi,u) func_dSigmadxi_' M.distribution{s,e} '(t,x,dxdxi,xi,' num2str(D(e).n_dim)...
',' str_noise ',' str_dnoisedxi ',''' options.noise_model ''');'];
else
str_sigma = ['M.Sigma{s,e} = @(t,x,xi,u) func_Sigma_' M.distribution{s,e} '(t,x,xi,' num2str(D(e).n_dim) ');'];
str_dsigmadxi = ['M.dSigmadxi{s,e} = @(t,x,dxdxi,xi,u) func_dSigmadxi_' M.distribution{s,e} '(t,x,dxdxi,xi,' num2str(D(e).n_dim) ');'];
end
if strcmp(M.distribution{s,e},'students_t')
str_nu =['M.nu{s,e} = @(t,x,xi,u) '];
str_nu = strcat(str_nu,strcat(replace_xi_x_u(M.sym.nu{s,e}),';'));
str_dnudxi = strcat(['M.dnudxi{s,e} = @(t,x,dxdxi,xi,u)'], getStrDerivative2Terms(M.sym.nu{s,e}, x, dxdxi, xi));
str_dnudxi = [str_dnudxi, ';'];
str_dnudxi = regexprep(str_dnudxi,'u_([0-9]+)','u($1)');
end
case 'skew_norm'
if options.measurement_noise
str_noise = '[';
for k = 1:D(e).n_dim
str_noise = strcat(str_noise,regexprep(char(M.sym.sigma_noise{e}(k).^2),'xi_([0-9]+)','xi($1)'));
str_noise = regexprep(str_noise,'u_([0-9]+)','u($1)');
if k < D(e).n_dim
str_noise = [str_noise ';'];
else
str_noise = [str_noise ']'];
end
end
str_dnoisedxi = '[';
M.sym.dnoisedxi{e} = jacobian(M.sym.sigma_noise{e}.^2,xi);
for l = 1:D(e).n_dim
for k = 1:length(M.sym.dnoisedxi{e})
str_dnoisedxi = strcat(str_dnoisedxi,regexprep(char(M.sym.dnoisedxi{e}(l,k)),'xi_([0-9]+)','xi($1)'));
str_dnoisedxi = regexprep(str_dnoisedxi,'u_([0-9]+)','u($1)');
if k<size(M.sym.dnoisedxi{e},2)
str_dnoisedxi = [str_dnoisedxi ','];
elseif k==size(M.sym.dnoisedxi{e},2) && l < D(e).n_dim
str_dnoisedxi = [str_dnoisedxi ';...\n'];
elseif k==size(M.sym.dnoisedxi{e},2) && l == D(e).n_dim
str_dnoisedxi = [str_dnoisedxi ']'];
end
end
end
str_sigma = ['M.Sigma{s,e} = @(t,x,delta,xi,u) func_Sigma_skew_norm(t,x,delta,xi,' num2str(D(e).n_dim)...
',' str_noise ',''' options.noise_model ''');'];
str_dsigmadxi = ['M.dSigmadxi{s,e} = @(t,x,dxdxi,delta,ddeltadxi,xi,u) func_dSigmadxi_skew_norm(t,x,dxdxi,delta,ddeltadxi,xi,' num2str(D(e).n_dim)...
',' str_noise ',' str_dnoisedxi ',''' options.noise_model ''');'];
else
str_sigma = ['M.Sigma{s,e} = @(t,x,delta,xi,u) func_Sigma_skew_norm(t,x,delta,xi,' num2str(D(e).n_dim) ');'];
str_dsigmadxi = ['M.dSigmadxi{s,e} = @(t,x,dxdxi,delta,ddeltadxi,xi,u) func_dSigmadxi_skew_norm(t,x,dxdxi,delta,ddeltadxi,xi,' num2str(D(e).n_dim) ');'];
end
str_delta =['M.delta{s,e} = @(t,x,xi,u) '];
str_delta = strcat(str_delta,strcat(replace_xi_x_u(M.sym.delta{s,e}),';'));
str_ddeltadxi = strcat(['M.ddeltadxi{s,e} = @(t,x,dxdxi,xi,u)'], getStrDerivative2Terms(M.sym.delta{s,e}, x, dxdxi, xi));
str_ddeltadxi = [str_ddeltadxi, ';'];
str_ddeltadxi = regexprep(str_ddeltadxi,'u_([0-9]+)','u($1)');
end
case 'RRE'
warning('This case needs to be tested!')
if size(D(e).u,2) == 1
ind = '[';
for l = 1:size(M.sym.ind{s,e},2)
for k = 1:size(M.sym.ind{s,e},1)
ind = strcat(ind,char(M.sym.ind{s,e}(k,l)));
ind = strcat(ind,',');
end
end
ind = strcat(ind,']');
if options.covariance
str_sigma = ['M.Sigma{s,e} = @(t,x,xi,u) func_Sigma_RRE_cov(t,x,xi,' num2str(D(e).n_dim) ', ' ind ' );'];
str_dsigmadxi = ['M.dSigmadxi{s,e} = @(t,x,dxdxi,xi,u) func_dSigmadxi_RRE_cov(t,x,dxdxi,xi,' num2str(D(e).n_dim) ', ' ind ');'];
else
str_sigma = ['M.Sigma{s,e} = @(t,x,xi,u) func_Sigma_RRE(t,x,xi,' num2str(D(e).n_dim) ', ' ind ' );'];
str_dsigmadxi = ['M.dSigmadxi{s,e} = @(t,x,dxdxi,xi,u) func_dSigmadxi_RRE(t,x,dxdxi,xi,' num2str(D(e).n_dim) ', ' ind ');'];
end
else
str_sigma = ['M.Sigma{s,e} = @(t,x,xi,u) '];
str_dsigmadxi = ['M.dSigmadxi{s,e} = @(t,x,dxdxi,xi,u) ' ];
for d = 1:size(D(e).u,2)
str = ['('];
for d_dim = 1:size(D(e).u,1)
str = [str '(u(' num2str(d_dim) ')==' num2str(D(e).u(d_dim,d)) ')'];
if d_dim < size(D(e).u,1)
str = [str ' & '];
end
end
str = [str ')*'];
ind = '[';
if options.covariance
for l = 1:3
ind = strcat(ind,num2str(D(e).Sigma{s}(l,d)));
if l < 3
ind = strcat(ind,',');
end
end
else
for l = 1:2
ind = strcat(ind,num2str(D(e).Sigma{s}(l,d)));
if l < 2
ind = strcat(ind,',');
end
end
end
ind = strcat(ind,']');
if options.covariance
str_sigma = strcat(str_sigma,[str 'func_Sigma_RRE_cov(t,x,xi,' num2str(D(e).n_dim) ', ' ind ' )']);
str_dsigmadxi = strcat(str_dsigmadxi,[str ' func_dSigmadxi_RRE_cov(t,x,dxdxi,xi,' num2str(D(e).n_dim) ', ' ind ')']);
else
str_sigma = strcat(str_sigma,[str 'func_Sigma_RRE(t,x,xi,' num2str(D(e).n_dim) ', ' ind ' )']);
str_dsigmadxi = strcat(str_dsigmadxi,[str ' func_dSigmadxi_RRE(t,x,dxdxi,xi,' num2str(D(e).n_dim) ', ' ind ')']);
end
if d < size(D(e).u,2)
str_sigma = strcat(str_sigma,'+');
str_dsigmadxi = strcat(str_dsigmadxi,'+');
end
end
str_sigma = strcat(str_sigma,';');
str_dsigmadxi = strcat(str_dsigmadxi,';');
end
end
else
switch M.distribution{s,e}
case {'logn_mean','logn_median','norm'}
str_sigma = ['M.sigma{s,e} = @(t,x,xi,u)\t'];
str_dsigmadxi = ['M.dsigmadxi{s,e} = @(t,x,dxdxi,xi,u)\t'];
switch M.sim_type
case {'HO','MCM'}
str_sigma = [str_sigma '['];
str_temp = regexprep(replace_by_bsxfun(char(M.sym.sigma{s,e})),'xi_([0-9]+)','xi($1)');
str_temp = regexprep(str_temp,'\^','.^');
str_temp = regexprep(str_temp,'x_([0-9]+)','x(:,$1)');
str_sigma = strcat(str_sigma,str_temp);
str_sigma = [str_sigma '];'];
% ACHTUNG HARD GECODED
%sw%tch M.distribution{s,e}
% case {'logn_median','logn_mean'}
if options.measurement_noise
str_noise = regexprep(char(M.sym.sigma_noise{e}.^2),'xi_([0-9]+)','xi($1)');
str_dnoisedxi = '[';
M.sym.dnoisedxi{e} = jacobian(M.sym.sigma_noise{e}.^2,xi);
for k = 1:length(M.sym.dnoisedxi{e})
str_dnoisedxi = strcat(str_dnoisedxi,regexprep(char(M.sym.dnoisedxi{e}(k)),'xi_([0-9]+)','xi($1)'));
str_dnoisedxi = regexprep(str_dnoisedxi,'u_([0-9]+)','u($1)');
if k<size(M.sym.dnoisedxi{e},2)
str_dnoisedxi = [str_dnoisedxi ','];
else
str_dnoisedxi = [str_dnoisedxi ']'];
end
end
switch M.distribution{s,e}
case {'logn_median','logn','logn_mean'}
str_dsigmadxi = [str_dsigmadxi 'bsxfun(@rdivide,func_dsigma2dxi_logn(t,x,dxdxi,xi,' str_noise ',' str_dnoisedxi ',''' options.noise_model '''),2*(' str_temp '));'];
case 'norm'
str_dsigmadxi = [str_dsigmadxi 'bsxfun(@rdivide,func_dsigma2dxi_norm(t,x,dxdxi,xi,' str_noise ',' str_dnoisedxi ',''' options.noise_model '''),2*(' str_temp '));'];
end
else
switch M.distribution{s,e}
case {'logn_median','logn','logn_mean'}
str_dsigmadxi = [str_dsigmadxi 'bsxfun(@rdivide,func_dsigma2dxi_logn(t,x,dxdxi,xi),2*(' str_temp '));'];
case 'norm'
str_dsigmadxi = [str_dsigmadxi 'bsxfun(@rdivide,func_dsigma2dxi_norm(t,x,dxdxi,xi),2*(' str_temp '));'];
end
end
case 'RRE'
if size(D(e).u,2) == 1
str_sigma = [str_sigma '['];
for k = 1:length(D(e).t)
str_temp = regexprep(char(M.sym.sigma{s,e}(k)),'xi_([0-9]+)','xi($1)');
str_sigma = strcat(str_sigma,regexprep(str_temp,'x_([0-9]+)','x(:,$1)'));
if k < length(D(e).t)
str_sigma = [str_sigma ';'];
else
str_sigma = [str_sigma '];'];
end
end
str_dsigmadxi = strcat(str_dsigmadxi,getStrDerivative2Terms(M.sym.sigma{s,e},x,dxdxi, xi));
str_dsigmadxi = [str_dsigmadxi, ';'];
else
for d = 1:size(D(e).u,2)
switch options.sigmas
case 'subpopulation-specific'
M.sym.sigma{s,e} = 10.^xi(ones(1,numel(D(e).t))*D(e).sigma{s});
case 'time-dependent'
M.sym.sigma{s,e} = 10.^xi((D(e).sigma{s}(d)));
% case 'condition-dependent'
% M.sym.sigma{s,e} = 10.^xi(conditions(D(e).c(s,d)).sigma);
% case 'time-independent'
% M.sym.sigma{s,e} = 10.^xi(ones(1,numel(D(e).t))*conditions(D(e).c(s,d)).sigma);
case 'only-one'
error('TODO')
end
str = ['('];
for d_dim = 1:size(D(e).u,1)
str = [str '(u(' num2str(d_dim) ')==' num2str(D(e).u(d_dim,d)) ')'];
if d_dim < size(D(e).u,1)
str = [str ' & '];
end
end
str = [str ')*'];
str_sigma = [strcat(str_sigma,str) '['];
str_dsigmadxi = strcat(str_dsigmadxi,str);
str_dsigmadxi = strcat(str_dsigmadxi,getStrDerivative2Terms(M.sym.sigma{s,e},x,dxdxi, xi));
if d < size(D(e).u,2)
str_dsigmadxi = [str_dsigmadxi ' ... \n \t + '];
end
for k = 1:length(D(e).t)
str_temp = regexprep(char(M.sym.sigma{s,e}(k)),'xi_([0-9]+)','xi($1)');
str_sigma = strcat(str_sigma,regexprep(str_temp,'x_([0-9]+)','x(:,$1)'));
if k < length(D(e).t)
str_sigma = [str_sigma ';'];
else
str_sigma = [str_sigma ']'];
end
end
if d < size(D(e).u,2)
str_sigma = [str_sigma ' + ...\n\t'];
else
str_sigma = [str_sigma ';'];
str_dsigmadxi = [str_dsigmadxi ';'];
end
end
end
end
case 'neg_binomial'
str_rho = ['M.rho{s,e} = @(t,x,xi,u)\t'];
str_drhodxi = ['M.drhodxi{s,e} = @(t,x,dxdxi,xi,u)\t'];
switch M.sim_type
case {'HO','MCM'}
str_rho = [str_rho '['];
str_temp = regexprep(replace_by_bsxfun(char(M.sym.rho{s,e})),'xi_([0-9]+)','xi($1)');
str_temp = regexprep(str_temp,'\^','.^');
str_temp = regexprep(str_temp,'x_([0-9]+)','x(:,$1)');
str_rho = strcat(str_rho,str_temp);
str_rho = [str_rho '];'];
% derivative
%str_drhodxi = strcat(str_drhodxi,getStrDerivative2Terms(M.sym.rho{s,e},x,dxdxi,xi));
%str_drhodxi = regexprep(str_drhodxi,'\^','.^');
%str_drhodxi = regexprep(str_drhodxi,'xi_([0-9]+)','xi($1)');
%str_drhodxi = [str_drhodxi, ';'];
if options.measurement_noise
str_noise = '[';
str_noise = strcat(str_noise,regexprep(char(M.sym.sigma_noise{e}),'xi_([0-9]+)','xi($1)'));
str_noise = regexprep(str_noise,'u_([0-9]+)','u($1)');
str_noise = [str_noise ']'];
str_dnoisedxi = '[';
M.sym.dnoisedxi{e} = jacobian(M.sym.sigma_noise{e},xi);
for k = 1:length(M.sym.dnoisedxi{e})
str_dnoisedxi = strcat(str_dnoisedxi,regexprep(char(M.sym.dnoisedxi{e}(k)),'xi_([0-9]+)','xi($1)'));
str_dnoisedxi = regexprep(str_dnoisedxi,'u_([0-9]+)','u($1)');
if k<size(M.sym.dnoisedxi{e},2)
str_dnoisedxi = [str_dnoisedxi ','];
elseif k==size(M.sym.dnoisedxi{e},2)
str_dnoisedxi = [str_dnoisedxi ']'];
end
end
str_drhodxi = ['M.drhodxi{s,e} = @(t,x,dxdxi,xi,u) func_drhodxi(t,x,dxdxi,xi,' ...
str_noise ',' str_dnoisedxi ',''' options.noise_model ''');'];
else
str_drhodxi = ['M.drhodxi{s,e} = @(t,x,dxdxi,xi,u) func_drhodxi(t,x,dxdxi,xi);'];
end
case 'RRE'
str_rho = [str_rho '['];
for k = 1:length(D(e).t)
str_temp = regexprep(char(M.sym.rho{s,e}(k)),'xi_([0-9]+)','xi($1)');
str_rho = strcat(str_rho,regexprep(str_temp,'x_([0-9]+)','x(:,$1)'));
if k < length(D(e).t)
str_rho = [str_rho ';'];
else
str_rho = [str_rho '];'];
end
end
str_drhodxi = strcat(str_drhodxi,getStrDerivative2Terms(M.sym.rho{s,e},x,dxdxi, xi));
str_drhodxi = [str_drhodxi, ';'];
end
case 'students_t'
error('case should be covered with ndim>1')
case 'skew_norm'
error('case should be covered with ndim>1')
otherwise
error(['Check distribution assumption, provided assumption ''' ...
M.distribution{s,e} ''' not covered. Only '...
'''neg_binomial'',''students_t'',''logn'',''norm'',''skew_norm'''])
end
end
str_distribution = ['M.distribution{s,e} = ''' M.distribution{s,e} ''''];
fprintf(fid,['%% Subpopulation ' num2str(s) ' \n']);
fprintf(fid,['%% Experiment ' num2str(e) ' \n']);
fprintf(fid,['s=' num2str(s) '; e=' num2str(e) ';\n']);
str_meanind = ['M.mean_ind{s,e} = [' ];
str_varind = ['M.var_ind{s,e} = [' ] ;
str_wind = ['M.w_ind{s,e} = [' ] ;
for k = 1:length(M.mean_ind{s,e})
str_meanind = strcat(str_meanind, num2str(M.mean_ind{s,e}(k)));
if k < length(M.mean_ind{s,e})
str_meanind = [str_meanind, ','];
else
str_meanind = [str_meanind, '];'];
end
end
% var_ind
if isempty(M.var_ind{s,e})
str_varind = [str_varind '];'];
else
for k = 1:length(M.var_ind{s,e})
str_varind = strcat(str_varind, num2str(M.var_ind{s,e}(k)));
if k < length(M.var_ind{s,e})
str_varind = [str_varind, ','];
else
str_varind = [str_varind, '];'];
end
end
end
% w_ind
if isempty(M.w_ind{s,e})
str_wind = [str_wind '];'];
else
for k = 1:length(M.w_ind{s,e})
str_wind = strcat(str_wind, num2str(M.w_ind{s,e}(k)));
if k < length(M.w_ind{s,e})
str_wind = [str_wind, ','];
else
str_wind = [str_wind, '];'];
end
end
end
fprintf(fid,[str_meanind '\n']);
fprintf(fid,[str_varind '\n']);
fprintf(fid,[str_wind '\n']);
switch M.distribution{s,e}
case {'norm','logn_median','logn_mean','students_t','skew_norm'}
if strcmp( M.distribution{s,e},'skew_norm')
fprintf(fid,[str_delta '\n']);
fprintf(fid,[str_ddeltadxi '\n\n']);
end
fprintf(fid,[str_mu '\n']);
fprintf(fid,[str_dmudxi '\n\n']);
fprintf(fid,[str_sigma '\n']);
fprintf(fid,[str_dsigmadxi '\n\n']);
if strcmp( M.distribution{s,e},'students_t')
fprintf(fid,[str_nu '\n']);
fprintf(fid,[str_dnudxi '\n\n']);
end
case 'neg_binomial'
fprintf(fid,[str_tau '\n']);
fprintf(fid,[str_dtaudxi '\n\n']);
fprintf(fid,[str_rho '\n']);
fprintf(fid,[str_drhodxi '\n\n']);
end
fprintf(fid,[str_w '\n']);
fprintf(fid,[str_dwdxi '\n']);
fprintf(fid,[str_distribution ';\n\n']);
fprintf(fid,['M.u{s,e} = [']);
for k = 1:length(M.u{s,e})
fprintf(fid,num2str(M.u{s,e}(k)));
if k < length(M.u{s,e})
fprintf(fid,';');
else
fprintf(fid,'];\n');
end
end
if options.replicates
% consider replicates individually
rs = 1:length(D(e).replicate);
else
% consider scaled and merged replicates
rs = 1;
end
for r=rs
fprintf(fid,['r=' num2str(r) ';\n']);
fprintf(fid,[str_s{r} '\n']);
fprintf(fid,[str_b{r} '\n']);
fprintf(fid,[str_dsdxi{r} '\n']);
fprintf(fid,[str_dbdxi{r} '\n']);
end
end
end
%% write parameter names in file
if options.write_parameters
fprintf(fid, '\n\nparameters.name = {');
str_max = 'parameters.max = [';
str_min = 'parameters.min = [';
str_constr_A = 'parameters.constraints.A = [';
for k = 1:length(parameters.name)
fprintf(fid,['''' regexprep(parameters.name{k},'\\','\\\\') '''']);
str_max = strcat(str_max,num2str(parameters.max(k)));
str_min = strcat(str_min,num2str(parameters.min(k)));
if isfield(parameters,'constraints')
str_constr_A = strcat(str_constr_A,num2str(parameters.constraints.A(k)));
end
if k < length(parameters.name)
fprintf(fid,',...\n');
str_max = [str_max ';'];
str_min = [str_min ';'];
str_constr_A = [str_constr_A ','];
else
fprintf(fid,'};\n');
str_max = [str_max '];'];
str_min = [str_min '];'];
str_constr_A = [str_constr_A '];'];
end
end
fprintf(fid, 'parameters.number = length(parameters.name);\n');
fprintf(fid,[str_max '\n']);
fprintf(fid,[str_min '\n']);
if isfield(parameters,'constraints')
fprintf(fid,[str_constr_A '\n']);
str_constr_b = ['parameters.constraints.b = [' num2str(parameters.constraints.b) '];\n'];
fprintf(fid,[str_constr_b '\n']);
end
end
% Close file
fclose(fid);
% Rehash to ensure that function is known / used
rehash
disp('File generation done!')
% assing ouput
if nargout >= 1
varargout{1} = M;
end
if nargout >= 2
varargout{2} = conditions;
end
end
function retstr = replace_xi_x_u(symexpr)
retstr = '[';
for s1 = 1:size(symexpr,1)
for s2 = 1:size(symexpr,2)
retstr = strcat(retstr,regexprep(char(symexpr(s1,s2)),'xi_([0-9]+)','xi($1)'));
retstr = regexprep(retstr,'u_([0-9]+)','u($1)');
retstr = regexprep(retstr,'x_([0-9]+)','x($1)');
if s2 < size(symexpr,2)
retstr = [retstr ', '];
end
end
if s1 < size(symexpr,1)
if size(symexpr,2) > 1
retstr = [retstr ';...\n\t'];
else
retstr = [retstr ';'];
end
end
end
retstr = [retstr ']'];
end
function str_dzdxi = getStrDerivative2Terms(sym_expr, x, dxdxi, xi)
str_dzdxi = 'bsxfun(@plus, [';
%% first term of the derivative
first_expr = jacobian(sym_expr,x)*dxdxi;
for k = 1:length(first_expr)
str_temp = replace_by_bsxfun(char(first_expr(k)));
str_temp = regexprep(str_temp,'u_([0-9]+)','u(:,$1)');
str_temp = regexprep(str_temp,'x_([0-9]+)','x(:,$1)');
str_dzdxi = strcat(str_dzdxi, regexprep(str_temp,'dxdxi_([0-9]+)','permute(dxdxi($1,:,:),[3,2,1])'));
if k < length(first_expr)
str_dzdxi = [str_dzdxi '; '];
end
end
str_dzdxi = [str_dzdxi '],...\n\t '];
second_expr = jacobian(sym_expr,xi);
str_dzdxi = strcat(str_dzdxi,replace_xi_x_u(second_expr));
str_dzdxi = [str_dzdxi ')'];
end
% function str_dzdxi = getStrDerivative3Terms(deriv_name, sym_expr, s, e, x, dxdxi, sigma, dsigmadxi, xi)
%
% str_dzdxi = ['M.' deriv_name '{s,e} = @(t,x,dxdxi,sigma,dsigmadxi,xi,u) bsxfun(@plus, ['];
% %% first term of derivative
% first_expr = jacobian(sym_expr{s,e},x)*dxdxi;
% for k = 1:length(first_expr)
% str_temp = replace_by_bsxfun(char(first_expr(k)));
% str_temp = regexprep(str_temp,'x_([0-9]+)','x(:,$1)');
% str_dzdxi = strcat(str_dzdxi, regexprep(str_temp,'dxdxi_([0-9]+)','permute(dxdxi($1,:,:),[3,2,1])'));
% if k < length(first_expr)
% str_dzdxi = [str_dzdxi '; '];