Clear Filters
Clear Filters

How can I treat the final time of the ode solver a variable? Please read the question for detail.

1 view (last 30 days)
Below is the main file in which I solve an ode. I have attached the parameter (mu, K, d1) files and the file (para_1d.m) in which ode is defined as well. My parameters are available as seasonal averages. So I have 4 average values per year (for spring, summer, fall and winter). I assume that each season is of equal length i.e., 91.25 days. But I have to incorporate the fact that the length of the seasons can vary. For example, there may be some years in which winter is longer but spring/fall is shorter. If suppose my year starts with spring and ends in winter, and if winter is short the length of my year will also be shorter. So I cannot use a fixed length (i.e., 365 days) of each year as well. I am not able to include this flexibility in my code. Any suggestions/comments will be helpful. Thanks!
clear all
p = 20;% indicates the number of years the simulation will run
yearlength = 365;
finaltime = p*yearlength;
t = 0:finaltime;
% calling parameters
mugen;
Kgen;
d1gen;
n = 2;
y0 = 20000; % initial conditions
deltat = 1;
tspan = 0:deltat:finaltime;
options = odeset('RelTol',1e-10,'AbsTol',1e-10);
sol = ode23s(@(t,y)para_1d(t,y,n, mug,Kg,d1g),tspan,y0,options);
X = sol.x;
Y = (sol.y)';
plot(X,Y(:,1),'r-')

Answers (1)

Jeremy D'Silva
Jeremy D'Silva on 18 Aug 2016
Edited: Jeremy D'Silva on 19 Aug 2016
Try using Latin Hypercube sampling from a reasonable range for each season length. In pseudocode:
springRange = [some ball around 91.25] e.g. [81.25,101.25]
summerRange = [some ball around 91.25] e.g. [81.25,101.25]
fallRange = [some ball around 91.25] e.g. [81.25,101.25]
winterRange = [some ball around 91.25] e.g. [81.25,101.25]
ranges = [springRange(1), summerRange(1),fallRange(1), winterRange(1); ...
springRange(2), summerRange(2), fallRange(2), winterRange(2)];
ourHyperCube = lhsdesign(number_of_runs,4); %Makes a Latin Hypercube
for i = 1:number of runs
SeasonLengths_run = ourHyperCube(i,:) * (ranges(2,:) - ranges(1,:)) + ranges(1,:);
% SeasonLengths_run = something of the form [springLength, summerLength, fallLength, winterLength];
sum season lengths to get t_final
simulate ODE over [t_0 t_final]
pass season values to ODE
save run
end
%%%%%%%%%%%%%%
Then, in your ODE file, incorporate time-varying parameters. You can do this in a trivial way by telling it (
if t<startOfSummer
use SpringParams
end
and likewise for the other seasons.
This is a really rough sketch, obviously, but the concept might work. And I'd be interested to see your solution, so let us know if it works!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!