-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbdEvolve.m
More file actions
176 lines (158 loc) · 6.72 KB
/
bdEvolve.m
File metadata and controls
176 lines (158 loc) · 6.72 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
%bdEvolve Evolve an initial-value problem using the Brain Dynamics Toolbox
%Usage:
% [sys,sol] = bdEvolve(sys)
% [sys,sol] = bdEvolve(sys,rep)
% [sys,sol] = bdEvolve(sys,rep,tspan)
% [sys,sol] = bdEvolve(sys,rep,tspan,@solverfun)
% [sys,sol] = bdEvolve(sys,rep,tspan,@solverfun,solvertype)
%where
% sys is a system struct describing the dynamical system
% rep=1 is the number of repeat simulation runs to perform (optional)
% tspan=[0 100] is the time span of the integration (optional)
% @solverfun is a function handle to an ode/dde/sde solver (optional)
% solvertype is a string describing the type of solver (optional).
%
% The tspan, @solverfun and solvertype arguments are all optional.
% If tspan is omitted then it defaults to sys.tspan.
% If @solverfun is omitted then it defaults to the first solver in sys.
% If @solverfun is supplied but it is not known to the sys struct then
% you must also supply the solvertype string ('odesolver', 'ddesolver'
% or 'sdesolver').
%
%RETURNS
% sys is updated with new initial conditions that correspond to the final
% conditions of the last simulation run.
% sol is the solution structure. It has the same format as that returned
% by the matlab ode45 solver. Use the bdEval function to extract the
% results from sol.
%
%EXAMPLE
% sys = WilsonCowan; % Wilson-Cowan Equations
% sys.pardef = bdSetValue(sys.pardef,'Je',2); % Set parmater Je=2
% rep = 10; % number of repeat runs
% [sys,sol] = bdEvolve(sys,rep); % evolve the system
% plot(sol.y(1,:),sol.y(2,:)); % plot the phase portrait
% xlabel('E'); ylabel('I');
%
%AUTHORS
% Stewart Heitmann (2018b)
% Copyright (C) 2016-2019 QIMR Berghofer Medical Research Institute
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions
% are met:
%
% 1. Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the
% distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
function [sys,sol] = bdEvolve(sys,rep,tspan,solverfun,solvertype)
% check the number of output variables
if nargout>2
error('Too many output variables');
end
% check the number of input variables
if nargin<1
error('Not enough input parameters');
end
% check the validity of the sys struct and fill missing fields with default values
try
sys = bd.syscheck(sys);
catch ME
throwAsCaller(ME);
end
% use defaults for missing input parameters
switch nargin
case 1
% Caller specified bdEvolve(sys).
% Perform one siimulation run only.
rep = 1;
% Get tspan from sys.tspan
tspan = sys.tspan;
% Get solverfun and solvertype from sys
if isfield(sys,'odesolver')
solverfun = sys.odesolver{1};
solvertype = 'odesolver';
end
if isfield(sys,'ddesolver')
solverfun = sys.ddesolver{1};
solvertype = 'ddesolver';
end
if isfield(sys,'sdesolver')
solverfun = sys.sdesolver{1};
solvertype = 'sdesolver';
end
case 2
% Caller specified bdEvolve(sys,rep).
% Get tspan from sys.tspan
tspan = sys.tspan;
% Get solverfun and solvertype from sys
if isfield(sys,'odesolver')
solverfun = sys.odesolver{1};
solvertype = 'odesolver';
end
if isfield(sys,'ddesolver')
solverfun = sys.ddesolver{1};
solvertype = 'ddesolver';
end
if isfield(sys,'sdesolver')
solverfun = sys.sdesolver{1};
solvertype = 'sdesolver';
end
case 3
% Caller specified bdEvolve(sys,rep,tspan).
% Get solverfun and solvertype from sys
if isfield(sys,'odesolver')
solverfun = sys.odesolver{1};
solvertype = 'odesolver';
end
if isfield(sys,'ddesolver')
solverfun = sys.ddesolver{1};
solvertype = 'ddesolver';
end
if isfield(sys,'sdesolver')
solverfun = sys.sdesolver{1};
solvertype = 'sdesolver';
end
case 4
% Caller specified bdEvolve(sys,rep,tspan,solverfun).
% Get solvertype from sys
if isfield(sys,'odesolver')
solvertype = 'odesolver';
end
if isfield(sys,'ddesolver')
solvertype = 'ddesolver';
end
if isfield(sys,'sdesolver')
solvertype = 'sdesolver';
end
end
try
% for each repeat simulation
for r=1:rep
% Call the appropriate solver
sol = bd.solve(sys,tspan,solverfun,solvertype);
% Update the initial conditions
sys.vardef = bdSetValues(sys.vardef,sol.y(:,end));
end
catch ME
throwAsCaller(ME);
end
end