-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContinum.m
1355 lines (1223 loc) · 51.3 KB
/
Continum.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
classdef Continum < handle
%% Continum: Software for Numerical Continuation and Dynamic Analysis
% Requires: --- Symbolic math toolbox
% --- Optimization toolbox
% --- Functions in (provided) 'Aux' folder must be in path
properties (Access = public)
model, ssmodel, massMatrix, vIndexVar
valueX, valueP, lowerX, lowerP, upperX, upperP, fixedP, tagX, tagP, tagXR, tagPR
vars, nX, pars, nP, ds, orbitgrid
ExplicitAE, HiddenAE, index, npol
hopfX, hopfP, cputime, objval, objcount, shape
time, solutionMatrix, eigenValues
report, keepX = [], keepP = [], trinity, ssBranch = {}, pBranch = {}, hBranch = {}
notSS = 0, penalty = 1
end
properties (Access = public)
ssopt = optimoptions(@fsolve,'Algorithm','levenberg-marquardt','Display','iter',...
'FunctionTolerance',1e-10,'StepTolerance',1e-10)
dynopt = struct('abstol', 1e-10, 'reltol', 1e-8, 'solver', 'dasslc')
optopt = struct('algorithm', 'simplex', 'tolfun', 1e-10, 'tolx', 1e-10, 'display',...
'iter', 'maxiter', 1e3, 'maxfunevals', 1e5, 'objectivelimitdet', 1e-9, 'nger', 100, 'npas', 100,...
'objectivelimitstoc', 0.0099,'objectiveshape','absolute')
conopt = struct('kmax',1000,'ds',0.01,'ncp',80,'algorithm','levenberg-marquardt','fixedDs',true);
end
methods (Access = public)
%% Constructor
function obj = Continum(model, vars, pars, massMatrix, vIndex)
if ~exist('labudde.m','file') && isfolder('Aux')
addpath('Aux')
end
if ~exist('cstr_simple.m','file') && isfolder('Models')
addpath('Models')
end
obj.model = model;
obj.ssmodel = @(x,p) model([],x,p);
obj.vars = vars;
obj.pars = pars;
obj.nX = length(vars);
obj.nP = length(pars);
group = '[^\|\s!@#\$%&\*\(\)\-_\+={}\[\]\^~\\/\.\,;:\?ºª<>]';
for i = obj.nX:-1:1
obj.valueX(i,1) = vars(i).value;
obj.lowerX(i,1) = vars(i).lower;
obj.upperX(i,1) = vars(i).upper;
obj.tagX{i,1} = vars(i).tag;
obj.tagXR{i,1} = vars(i).tag(regexp(vars(i).tag, group));
end
if length(unique(obj.tagX)) < obj.nX
error('Check for duplicated variable tags.')
end
for i = obj.nP:-1:1
obj.valueP(i,1) = pars(i).value;
obj.lowerP(i,1) = pars(i).lower;
obj.upperP(i,1) = pars(i).upper;
obj.fixedP(i,1) = pars(i).fixed;
obj.tagP{i,1} = pars(i).tag;
obj.tagPR{i,1} = pars(i).tag(regexp(pars(i).tag, group));
end
if length(unique(obj.tagP)) < obj.nP
error('Check for duplicated parameter tags.')
end
obj.massMatrix = eye(obj.nX);
if nargin > 3 && ~isempty(massMatrix)
obj.massMatrix = massMatrix;
end
obj.vIndexVar = [];
if nargin > 4 && ~isempty(vIndex)
obj.vIndexVar = vIndex;
end
if exist('dasslc','file') ~= 3
obj.dynopt.solver = 'ode15i';
end
obj.initialSetup;
[err, message] = obj.check;
if (err)
error(message);
end
if obj.npol < 2
warning('This is a first order system. No complex dynamic behavior can be observed.');
end
obj.reportBack('constructor');
end
%% Edition methods
% function [p,par] = RetrieveOrbits(obj)
% if ~isempty(obj.orbits)
% p = obj.orbits;
% par = obj.pBranch{2}(:,1);
% end
% end
% Fix a parameter
function FixParameter(obj, par)
if strcmp(par.getType,'var')
error('Can oly fix parameters.')
end
n = length(par);
for i = 1:n
obj.fixedP(strcmp(obj.tagP,par(i).tag)) = true;
par(i).fixed = true;
par(i).Show;
end
obj.reportBack('setfunc');
end
% Free a parameter
function FreeParameter(obj, par)
if strcmp(par.getType,'var')
error('Can oly fix parameters.')
end
n = length(par);
for i = 1:n
obj.fixedP(strcmp(obj.tagP,par(i).tag)) = false;
par(i).fixed = false;
par(i).Show;
end
obj.reportBack('setfunc');
end
% Set lower bound
function SetLower(obj, par, val)
n = length(par);
m = length(val);
if n ~= m
error('Different input lengths.')
end
for i = 1:n
if strcmp(par(i).getType,'var')
obj.lowerX(strcmp(obj.tagX,par(i).tag)) = val(i);
else
obj.lowerP(strcmp(obj.tagP,par(i).tag)) = val(i);
end
par(i).lower = val(i);
par(i).Show;
end
obj.reportBack('setfunc');
end
% Set upper bound
function SetUpper(obj, par, val)
n = length(par);
m = length(val);
if n ~= m
error('Different input lengths.')
end
for i = 1:n
if strcmp(par(i).getType,'var')
obj.upperX(strcmp(obj.tagX,par(i).tag)) = val(i);
else
obj.upperP(strcmp(obj.tagP,par(i).tag)) = val(i);
end
if par(i).value > val
par(i).value = val;
end
par(i).upper = val(i);
par(i).Show;
end
obj.reportBack('setfunc');
end
% Set value
function SetValue(obj, par, val)
n = length(par);
m = length(val);
if n ~= m
error('Different input lengths.')
end
for i = 1:n
if strcmp(par(i).getType,'var')
obj.valueX(strcmp(obj.tagX,par(i).tag)) = val(i);
else
obj.valueP(strcmp(obj.tagP,par(i).tag)) = val(i);
end
if par(i).value < val
par(i).value = val;
end
par(i).value = val(i);
par(i).Show;
end
obj.reportBack('setfunc');
end
% Keep bifurcation point
function KeepBP(obj)
if isempty(obj.hopfP)
error('No active search')
end
if isempty(obj.keepX) || ~any(sum(obj.hopfP' == obj.keepP,2) == obj.nP)
obj.keepX(end+1,:) = obj.hopfX';
obj.keepP(end+1,:) = obj.hopfP';
else
warning('The active point is already listed.')
end
obj.reportBack('keep')
end
% Get bifurcation point
function [x,p] = GetBP(obj, n)
if nargin < 2
error('Must specify a point to get.')
elseif ~isfield(obj.report,'bplist')
error('Must have at least one kept point.')
elseif n > max(obj.report.bplist)
warning('Specified point does not exist. Returning the first one.')
n = 1;
end
x = obj.keepX(n,:);
p = obj.keepP(n,:);
end
function ShiftToBP(obj, n)
if nargin < 2
error('Must specify a point to get.')
elseif ~isfield(obj.report,'bplist')
error('Must have at least one kept point.')
elseif n > max(obj.report.bplist)
warning('Specified point does not exist. Shifting to the first one.')
n = 1;
end
obj.valueP = obj.keepP(n,:)';
obj.valueX = obj.keepX(n,:)';
for i = 1:obj.nX
obj.vars(i).value = obj.valueX(i);
end
for i = 1:obj.nP
obj.pars(i).value = obj.valueP(i);
end
obj.reportBack('setfunc')
end
%% Solver methods
% Steady State Solver
function [x,f,exf,out,J] = SteadyState(obj, par)
if nargin < 2
par = obj.valueP;
end
[x,f,exf,out,J] = fsolve(obj.ssmodel,obj.valueX,obj.ssopt,par);
if exf <= 0
[x,f,exf,out,J] = hfsolve(obj.ssmodel,x,obj.ssopt,5,false,par);
if exf <= 0
[x,f,exf,out,J] = hfsolve(obj.ssmodel,x,obj.ssopt,25,false,par);
end
end
if exf > 0
obj.valueX = x;
elseif obj.notSS > 100
warning('Could not find steady-state several times.')
obj.notSS = 0;
else
obj.notSS = obj.notSS + 1;
end
end
% Dynamic Solver
function [t,y] = Simulate(obj, it, y0, yp0, par, vIndex)
if nargin < 2 || isempty(it)
error('Need integration interval.')
end
if nargin < 3 || isempty(y0)
y0 = obj.valueX;
end
if nargin < 5 || isempty(par)
par = obj.valueP;
end
if nargin < 4 || isempty(yp0)
yp0 = obj.model(0, y0, par);
end
if nargin < 6
vIndex = obj.vIndexVar;
end
opt = odeset('reltol',obj.dynopt.reltol,'abstol',obj.dynopt.abstol);
B = obj.massMatrix;
modelEq = obj.model;
if strcmpi(obj.dynopt.solver,'ode15s') || strcmpi(obj.dynopt.solver,'ode23t')
if rank(B) < obj.nX
opt.Mass = B;
end
[t,y] = feval(obj.dynopt.solver,obj.model, it, y0, opt, par);
elseif strcmpi(obj.dynopt.solver,'ode15i')
[t,y] = ode15i(@resmodel, it, y0, yp0, opt);
elseif strcmpi(obj.dynopt.solver,'dasslc')
[t,y] = dasslc(@resmodel, it, y0, yp0, [], obj.dynopt.reltol, obj.dynopt.abstol, vIndex);
else
error('Unsupported integrator.')
end
obj.time = t;
obj.solutionMatrix = y;
function [res,ires] = resmodel(~, y, yp)
res = B*yp - modelEq([], y, par);
ires = 0;
end
end
% Hopf Search Solver
function [x,p] = HopfSearch(obj, alg)
function stop = outfun(~, op, ~, ~)
if op.fval < objmin
stop = true;
else
stop = false;
end
end
optfmin = optimset('Display',obj.optopt.display,'TolFun',obj.optopt.tolfun,'TolX',obj.optopt.tolx,...
'MaxFunEvals',obj.optopt.maxfunevals,'MaxIter',obj.optopt.maxiter/5, 'Outputfcn', @outfun);
objmin = obj.optopt.objectivelimitdet;
tic
obj.objcount = 0;
obj.penalty = 1;
if nargin < 2
alg = obj.optopt.algorithm;
end
swarmdisp = ~strcmpi(obj.optopt.display,'none');
obj.valueP(obj.valueP > obj.upperP) = obj.upperP(obj.valueP > obj.upperP);
obj.valueP(obj.valueP < obj.lowerP) = obj.lowerP(obj.valueP < obj.lowerP);
switch lower(alg)
case 'swarm'
[p, obj.objval, obj.objcount] = argeSwarm(@objective, obj.valueP, obj.lowerP, obj.upperP,...
obj.optopt.nger, obj.optopt.npas, obj.optopt.objectivelimitstoc, ...
obj.optopt.maxfunevals, swarmdisp, obj);
case 'simplex'
p0 = obj.valueP;
for i = 1:5
[p, obj.objval,~,out] = fminsearch(@objective,p0,optfmin,obj);
obj.objcount = out.funcCount + obj.objcount;
p0 = p;
if obj.objval < obj.optopt.objectivelimitdet
break
end
end
case 'hybrid'
[p0, obj.objval, obj.objcount] = argeSwarm(@objective, obj.valueP, obj.lowerP, obj.upperP,...
obj.optopt.nger, obj.optopt.npas, obj.optopt.objectivelimitstoc, ...
obj.optopt.maxfunevals, swarmdisp, obj);
for i = 1:5
[p, obj.objval,~,out] = fminsearch(@objective,p0,optfmin,obj);
obj.objcount = out.funcCount + obj.objcount;
p0 = p;
if obj.objval < obj.optopt.objectivelimitdet
break
end
end
case 'levenberg-marquardt'
optlvm = optimoptions(@fsolve,'Algorithm',obj.optopt.algorithm,'Display',obj.optopt.display,...
'TolFun',obj.optopt.tolfun,'TolX',obj.optopt.tolx,'MaxFunEvals',obj.optopt.maxfunevals,...
'MaxIter',obj.optopt.maxiter);
[p, obj.objval,~,out] = fsolve(@objective,obj.valueP,optlvm);
obj.objcount = out.funcCount;
otherwise
warning('Solver not recognized. Using "simplex" instead!')
p0 = obj.valueP;
for i = 1:5
[p, obj.objval,~,out] = fminsearch(@objective,p0,optfmin,obj);
obj.objcount = out.funcCount + obj.objcount;
p0 = p;
if obj.objval < obj.optopt.objectivelimitdet
break
end
end
end
obj.hopfP = obj.valueP;
obj.hopfP(obj.fixedP == false) = p(obj.fixedP == false);
for i = 1:obj.nP
obj.pars(i).value = obj.hopfP(i);
end
obj.valueP = obj.hopfP;
[obj.hopfX,~,~,~,J] = obj.SteadyState;
for i = 1:obj.nX
obj.vars(i).value = obj.hopfX(i);
end
obj.cputime = toc;
ev = eig(J,obj.massMatrix);
obj.eigenValues = ev(~isinf(ev));
obj.reportBack('hopfsearch')
x = obj.hopfX;
p = obj.hopfP;
end
function ContinueHopf2Par(obj,par1,par2,dp)
if nargin < 3
error('At least two parameters must be given.')
end
if nargin < 4
dp = [0.1, 0.1]';
end
if length(dp) < 2
dp(2) = dp(1);
end
% Save initial state
initialState = obj.fixedP;
initialDisplay = obj.optopt.display;
intialValueP = obj.valueP;
intialValueX = obj.valueX;
% Do some setup
obj.optopt.display = 'none';
dir = 1; n = 10000; h = 1; i = 1;
xp = zeros(n+1,obj.nP);% f = zeros(n+1,1);
xp(1,:) = obj.valueP';
pos1 = strcmp(par1.tag,obj.tagP);
pos2 = strcmp(par2.tag,obj.tagP);
obj.fixedP = ~strcmp(par1.tag,obj.tagP);
% Calculate
for k = 1:size(obj.keepP,1)
obj.ShiftToBP(k)
ok = true;
changeDir = false;
while ok
while i < n
if any(obj.valueP <= obj.lowerP) || any(obj.valueP >= obj.upperP)
obj.valueP = lastP;
h = 1;
if changeDir
ok = false;
end
changeDir = true;
break
end
[~, xp(i,:)] = obj.HopfSearch('simplex');
if obj.objval < 1e-7
fprintf('iter: %4d\t fval: %2.4e\t %s: %2.4e\t %s: %2.4e\n',i,obj.objval,par1.tag,xp(i,pos1),par2.tag,xp(i,pos2))
lastX = obj.valueX;
lastP = obj.valueP;
obj.valueP(pos2) = obj.valueP(pos2) + dir*dp(2);
i = i+1;
else
obj.valueX = lastX;
obj.valueP = lastP;
obj.valueP(pos2) = obj.valueP(pos2) + dir*dp(2)*2^-h;
h = h+1;
if h > 10
h = 1;
if changeDir
ok = false;
end
changeDir = true;
break
end
end
end
if changeDir
obj.ShiftToBP(k)
dir = -dir;
xp(i,:) = nan;
i = i+1;
end
end
end
xp(i:end,:) = [];
obj.fixedP = initialState;
obj.valueP = intialValueP;
obj.valueX = intialValueX;
obj.optopt.display = initialDisplay;
obj.hBranch{end+1} = par1.tag;
obj.hBranch{end+1} = par2.tag;
obj.hBranch{end+1} = xp;
end
function PeriodicContinuation(obj,par,vIndex,vars)
if nargin < 2
error('One parameter must be given')
end
if nargin < 3 || isempty(vIndex)
vIndex = obj.vIndexVar;
end
if nargin < 4
vec = 1:obj.nX;
end
if nargin == 4
nv = length(vars);
vec = zeros(nv,1);
for j = 1:nv
posj = find(strcmp(vars(j).tag,obj.tagX));
if ~isempty(posj)
vec(j) = posj;
else
error('Problem does not contain variable "%s".', vars(j).tag)
end
end
end
if ~ismember(obj.hopfP',obj.keepP,'rows')
obj.ShiftToBP(1)
end
% The problem function
function F = theProblem(x)
% Alocate the "variables"
Y = reshape(x(1:n*m),n,m);
T = x(n*m+1);
if ~initialOrbitSearch
newpar(pos) = x(n*m+2);
end
% Discretization
for i = m:-1:1
Fk(:,i) = cModel([], Y(:,i), newpar);
end
R = (B*Y)*Lt - T*Fk;
% The equation's residuals
R = R(n+1:end)';
% The boundary condition
P = Y(:,1) - Y(:,end);
% The inflated integral
I1 = theta(1)*trapz(tgrid,sum((Y(vec,:)-Y0(vec,:)).*Ydot(vec,:))) + theta(2)*(T-T0)*Tdot + theta(3)*(newpar(pos) - par0)*pardot - deltaS;
% The phase condition
if ~initialOrbitSearch
I2 = trapz(tgrid,sum(Y(vec,:).*dY0(vec,:)));
F = [R; I1; I2; P];
else
F = [R; I1; P];
end
end
% The initial parameters
B = obj.massMatrix;
n = obj.nX;
m = obj.conopt.ncp;
cModel = obj.model;
newpar = obj.valueP;
deltaS = obj.conopt.ds;
ds0 = deltaS;
pos = strcmp(par.tag,obj.tagP);
theta = [1 1 1];
% The collocation stuff
[L, ~, tgrid] = collocation(m-2, '2*x-1', [0 1]);
Lt = L';
obj.orbitgrid = tgrid;
% Solve the problem at current HBP
[xi,~,~,~,J] = obj.SteadyState(newpar);
% Find the initial period
evals = eig(J,B);
evals(isinf(evals)) = [];
epair = evals(abs(real(evals)) < 1e-6 & abs(imag(evals)) > 1e-7);
T0 = 2*pi/unique(abs(imag(epair)));
% Solver setup
opt = obj.ssopt;
opt.Display = 'iter';
opt.Algorithm = obj.conopt.algorithm;
opt.MaxFunEvals = 2e5;
opt.MaxIter = 2e4;
% Find the initial orbit
initialOrbitSearch = true;
% Problem function parameters
% Ydot = zeros(n,m);
% for j = 1:m
% Ydot(:,j) = expm((J*T0)*tgrid(j))*xi; % change to a general analytic solution. Now -> ODE only
% end
Y0 = xi.*ones(n,m);
Y0a = Y0;
Tdot = 0;
pardot = 0;
par0 = newpar(pos);
function [res,ires] = linprob_fi(~,y,yp)
res = B*yp - J*y;
ires = 0;
end
function dy = linprob(~,y)
dy = J*y;
end
if ~isempty(vIndex)
xpi = -linprob_fi(0,xi,zeros(obj.nX,1));
[~,Ydot2] = dasslc(@linprob_fi,tgrid*T0, xi, xpi, [], obj.dynopt.reltol, obj.dynopt.abstol, vIndex);
else
[~,Ydot2] = ode15s(@linprob,tgrid*T0, xi, odeset('abstol',obj.dynopt.abstol,'reltol',obj.dynopt.reltol,'Mass',B));
end
Ydot = Ydot2';
% The initial guess
x0 = xi.*ones(n,m);
x0 = reshape(x0,n*m,1);
x0(n*m+1) = T0;
% Solver call
fprintf('Searching for initial orbit...\n\n')
[x,f] = fsolve(@theProblem, x0, opt);
if norm(f) > 1e-4
error('No initial orbit detected.')
end
% The results
po = reshape(x(1:n*m),n,m);
% Begin continuation
initialOrbitSearch = false;
% Insert the continuation parameter in the variable's vector
x(n*m+2) = newpar(pos);
% Continuation parameters
kend = obj.conopt.kmax;
parspace = zeros(kend,1);
maxsol = zeros(kend,obj.nX);
parspace(1) = newpar(pos);
maxsol(1,:) = max(po,[],2)';
opt.Display = 'none';
orbits = zeros(n,m,kend);
orbits(:,:,1) = po;
periods = zeros(kend,1);
periods(1) = x(n*m+1);
floquet = zeros(obj.npol,kend);
[~,~,~,~,Jss] = obj.SteadyState();
[V,U,i1] = pencan(obj.massMatrix,Jss);
KCF = U*Jss*V;
KCF_J = KCF(1:i1,1:i1);
floquet(:,1) = eig(expm(KCF_J*x(n*m+1)));
iterate = true;
k = 2;
cont = 1;
sf = sprintf('\nRuning periodic continuation...\n\n');
fprintf([sf repmat(' ',1,50)])
while iterate
Y0 = po;
dY0 = (Y0(:,2:end) - Y0(:,1:end-1))./(tgrid(2:end)' - tgrid(1:end-1)');
dY0(:,m) = dY0(:,m-1);
Tdot = 1/deltaS*(x(n*m+1) - T0);
pardot = 1/deltaS*(x(n*m+2) - par0);
T0 = x(n*m+1);
par0 = x(n*m+2);
Ydot = 1/deltaS*(Y0 - Y0a);
Y0a = Y0;
% theta = 1./[norm(Ydot)^2 Tdot^2 pardot^2+sqrt(eps)];
[x,f] = fsolve(@theProblem, x, opt);
po = reshape(x(1:n*m),n,m);
if norm(f) < 1e-8
parspace(k) = x(n*m+2);
maxsol(k,:) = max(po,[],2)';
orbits(:,:,k) = po;
periods(k) = x(n*m+1);
[~,~,~,~,Jss] = obj.SteadyState();
[V,U,i1] = pencan(obj.massMatrix,Jss);
KCF = U*Jss*V;
KCF_J = KCF(1:i1,1:i1);
floquet(:,k) = eig(expm(KCF_J*x(n*m+1)));
sf = sprintf('Iter: %4d\t par: %2.6e\t norm: %2.6e\n',k,parspace(k),norm(f));
fprintf([repmat('\b',1,50) sf])
k = k + 1;
cont = 1;
if ~obj.conopt.fixedDs
deltaS = min(deltaS+ds0, 8*ds0);
end
if k > kend
iterate = false;
end
else
if ~obj.conopt.fixedDs
deltaS = max(deltaS/2, ds0/10);
end
cont = cont + 1;
if cont > 10
warning('Could not converge in continuation procedure.')
sf = sprintf('Iter: %4d\t par: %2.6e\t norm: %2.6e\n',k-1,parspace(k-1),norm(f));
fprintf([repmat('\b',1,50) sf])
iterate = false;
end
end
end
fprintf('\nFinished!')
if k < kend
parspace(k:end) = [];
maxsol(k:end,:) = [];
orbits(:,:,k:end) = [];
periods(k:kend) = [];
floquet(:,k:kend) = [];
end
if isempty(obj.pBranch)
obj.pBranch{1} = par.tag;
obj.pBranch{2} = [parspace, maxsol];
obj.pBranch{3} = orbits;
obj.pBranch{4} = periods;
obj.pBranch{5} = floquet;
else
obj.pBranch{end+1} = par.tag;
obj.pBranch{end+1} = [parspace, maxsol];
obj.pBranch{end+1} = orbits;
obj.pBranch{end+1} = periods;
obj.pBranch{end+1} = floquet;
end
end
function SSContinuation(obj,par,n,it)
if nargin < 2
error('You need to specify the parameter.')
end
if length(par)>1
error('Only one parameter is allowed.')
end
if nargin < 3 || isempty(n)
n = 1000;
end
if nargin < 4
it = [par(1).lower, par(1).upper];
end
B = obj.massMatrix;
parspace = linspace(it(1),it(2),n)';
pos = strcmp(par.tag,obj.tagP);
x = zeros(n,obj.nX);
lambda = zeros(n,obj.nX);
newpar = obj.valueP;
sf = sprintf('\nRuning steady-state continuation...\n\n');
fprintf([sf repmat(' ',1,31)])
for i = 1:n
newpar(pos) = parspace(i);
[x(i,:),f,~,~,J] = obj.SteadyState(newpar);
lambda(i,:) = eig(J,B);
sf = sprintf('Iter: %4d\t norm: %2.6e\n',i,norm(f));
fprintf([repmat('\b',1,31) sf])
end
fprintf('\nFinished!')
r = real(lambda);
stableC = nan(n,obj.nX);
unstableC = stableC;
for i = 1:n
if any(r(i,~isinf(r(i,:)))>0)
unstableC(i,:) = x(i,:);
else
stableC(i,:) = x(i,:);
end
end
if isempty(obj.ssBranch)
obj.ssBranch{1} = par.tag;
obj.ssBranch{2} = [parspace, stableC, unstableC];
obj.ssBranch{3} = lambda;
else
obj.ssBranch{end+1} = par.tag;
obj.ssBranch{end+1} = [parspace, stableC, unstableC];
obj.ssBranch{end+1} = lambda;
end
end
%% Report method
function ShowReport(obj)
fprintf('\n\n==================== Reports ====================\n\n')
fprintf('* Initial Setup\n\n')
fprintf(' Model ........................................... %s\n',[func2str(obj.model) '.m']);
fprintf(' Variables ....................................... ')
for i = 1:obj.nX
fprintf('%s = %d, ', obj.tagXR{i}, obj.report.x0(i));
end
fprintf('\b\b\n')
fprintf(' Search Parameters ............................... ')
for i = 1:obj.nP
if ~obj.fixedP(i)
fprintf('%s = %d, ', obj.tagPR{i}, obj.report.par0(i));
end
end
fprintf('\b\b\n')
if any(obj.fixedP)
fprintf(' Fixed Parameters ................................ ')
for i = 1:obj.nP
if obj.fixedP(i)
fprintf('%s = %d, ', obj.tagPR{i}, obj.report.par0(i));
end
end
fprintf('\b\b\n\n')
else
fprintf('\n')
end
fprintf('* Model Analysis\n\n')
fprintf(' Differential Index .............................. %d\n', obj.report.DAE_Index);
fprintf(' Explicit Algebraic Eqs .......................... %d\n', obj.report.ExplicitAE);
fprintf(' Hidden Algebraic Eqs ............................ %d\n', obj.report.HiddenAE);
fprintf(' Dynamic Degrees of Freedom ...................... %d\n\n', obj.report.DDoF);
if isfield(obj.report,'x')
fprintf('* Active Search\n\n')
fprintf(' Hopf found? ..................................... ')
if obj.report.objval <= obj.optopt.objectivelimitdet
fprintf('Yes.\n\n')
elseif obj.report.objval > obj.optopt.objectivelimitdet && obj.report.objval <= obj.optopt.objectivelimitdet*100
fprintf('Yes. But more refinment may be needed.\n\n')
else
fprintf('No.\n\n')
end
fprintf(' x-values ........................................ ')
for i = 1:obj.nX
fprintf('%s = %d, ', obj.tagXR{i}, obj.report.x(i));
end
fprintf('\b\b\n');
fprintf(' par-values ...................................... ')
for i = 1:obj.nP
if ~obj.fixedP(i)
fprintf('%s = %d, ', obj.tagPR{i}, obj.report.par(i));
end
end
fprintf('\b\b\n');
fprintf(' Eigenvalues ..................................... (')
for i = 1:obj.npol
fprintf('%.4e %+.4ei, ', real(obj.report.eigenValues(i)), imag(obj.report.eigenValues(i)))
end
fprintf('\b\b)\n\n');
fprintf(' CPU Time ........................................ %d s\n', obj.report.cputime);
fprintf(' Obj Function Value .............................. %d\n', obj.report.objval);
fprintf(' Function Count .................................. %d\n\n', obj.report.objcount);
end
if isfield(obj.report,'keepX')
fprintf('* Kept Bifurcation Points\n\n')
for i = 1:length(obj.report.bplist)
fprintf(' %d: x = (',i)
fprintf('%d, ', obj.report.keepX(i,:))
fprintf('\b\b)\n')
fprintf(' \t p = (')
fprintf('%d, ', obj.report.keepP(i,:))
fprintf('\b\b)\n\n')
end
end
fprintf('\n================= End of Report =================\n\n')
end
%% Plot methods
function PlotTimeSeries(obj,var)
if nargin < 2
error('Must specify a variable')
end
if any(~strcmp(var.getType,'var'))
error('Only variables can be plotted.')
end
for i = length(var):-1:1
pos(1,i) = find(strcmp(obj.tagX,var(i).tag));
end
figured;
plot(obj.time, obj.solutionMatrix(:,pos),'linewidth',1.5)
xlabel('Time')
if length(var) > 1
legend(var.tag,'Location','Best')
ylabel('Variables')
else
ylabel(var.tag)
end
end
function PlotPhasePlane(obj,var1,var2)
if nargin < 3
error('Must specify a pair of variables.')
end
if length(var1) > 1 || length(var2) > 1
error('Both inputs must be scalars.')
end
figured;
plot(obj.solutionMatrix(:,strcmp(obj.tagX,var1.tag)),obj.solutionMatrix(:,strcmp(obj.tagX,var2.tag)),'linewidth',1.5)
hold on
plot(obj.solutionMatrix(1,strcmp(obj.tagX,var1.tag)),obj.solutionMatrix(1,strcmp(obj.tagX,var2.tag)),...
's','MarkerFaceColor',[0.85,0.325,0.098],'MarkerSize',12,'MarkerEdgeColor',[0.85,0.325,0.098])
plot(obj.solutionMatrix(end,strcmp(obj.tagX,var1.tag)),obj.solutionMatrix(end,strcmp(obj.tagX,var2.tag)),...
'o','MarkerFaceColor',[0.929,0.694,0.125],'MarkerSize',10,'MarkerEdgeColor',[0.929,0.694,0.125])
xlabel(var1.tag)
ylabel(var2.tag)
legend('Trajectory','Start','End','Location','Best')
end
function PlotEigenMap(obj, par, n, it)
if nargin < 2
error('You need to specify a parameter.')
end
if nargin < 3 || isempty(n)
n = 100;
end
if nargin < 4
it = [par.lower, par.upper];
end
parspace = linspace(it(1),it(2),n)';
pos = strcmp(par.tag,obj.tagP);
newpar = obj.valueP;
for i = n:-1:1
newpar(pos) = parspace(i);
[obj.valueX,~,~,~,Jss] = obj.SteadyState(newpar);
eigs = eig(Jss,obj.massMatrix)';
lambda(i,:) = eigs(~isinf(eigs));
end
realL = real(lambda);
complexL = imag(lambda);
obj.trinity = [parspace, realL, complexL];
newFig = figured;
plot(realL,complexL,'LineWidth',1,'LineStyle','-','Marker','.','MarkerSize',10);
ax = gca;
line([0,0],ax.YLim,'color','k','linestyle',':')
line(ax.XLim,[0,0],'color','k','linestyle',':')
set(gca,'YGrid','on','Box','on','FontSize',18)
ylabel('Im(\lambda)','FontWeight','bold','FontName','Palatino','fontsize',18)
xlabel('Re(\lambda)','FontWeight','bold','FontName','Palatino','fontsize',18)
cursorMode = datacursormode(newFig);
hPlot = get(gca,'Children');
nLines = length(hPlot);
hDatatip{nLines-2,1} = 0;
XData{nLines-2,1} = 0;
YData{nLines-2,1} = 0;
set(cursorMode,'UpdateFcn',{@eigenCursor,[parspace, lambda]},'DisplayStyle','datatip')
k = 1;
rec = 0;
for i = 1:nLines
if length(hPlot(i).XData) > 2
XData{k} = hPlot(i).XData;
YData{k} = hPlot(i).YData;
hDatatip{k} = cursorMode.createDatatip(hPlot(i));
set(hDatatip{k}, 'Marker','o', 'MarkerSize',10, 'MarkerFaceColor',hPlot(i).Color,...
'MarkerEdgeColor','w', 'OrientationMode','auto',...
'Position', [hPlot(i).XData(1) hPlot(i).YData(1) 0])
updateDataCursors(cursorMode)
drawnow;
k = k+1;
end
end
set(gca,'FontSize',16,'Position',[0.13 0.11 0.775 0.815])
rec = 1;
ind = 1;
%
function txt = eigenCursor(~,event_obj,data)
% cursorMode = datacursormode(gcf);
pos = get(event_obj,'Position');
I = get(event_obj, 'DataIndex');
txt = {['Re : ',sprintf('% 2.4g',pos(1))],...
['Im : ',sprintf('% 2.4g',pos(2))],...
['Par: ',sprintf('% 2.8g',data(I,1))]};
if rec == 1
rec = 0;
ind = I;
for j = 1:nLines - 2
hDatatip{j}.Cursor.Position = [XData{j}(ind) YData{j}(ind) 0];
drawnow;
end
rec = 1;
end
end
end
function PlotObjectiveFunction(obj, par, n, it)
if nargin < 2
error('You need to specify the parameter.')
end
if length(par)>2
error('Maximum of two parameters is allowed.')