variables - Update parameter based on random time vector matlab -
i have non ode function calculates based on previous time step of time vector , spatial variable vector.
one of parameters in function constant. however, want make parameter variable, change based @ given time.
here example code make clear.
nstrains = 10; param = .3*rand(1,nstrains); strtime = rand(1,nstrains); strtime = round(strtime); strtime = sort(strtime); initialconds = .5 t=0:.1:10; %time vector x=1:.1:10; %spatial vector k = zeros(numel(x),numel(t)) k = zeros(numel(x)/2,1) = initialconds = 1:(numel(t)-1) j = 2:(numel(x)-1) k(j,i+1) = 5*2+c(j+1,i)+param*c(j,1)+param end end
if param held constant, no problem. want have vector of random numbers param, enter random number param @ random times. random time determined strtime.
so example, if strtime vector = [2 4 9 10], , param vector = [.21 .01 .25 .05] first, want initial param value @ time 0 = .2 (arbitrary). time vector matches strtime vector, in example 2, param value updated .21. .21 used @ each time step until time vector matches strtime vector again, in case, 4. time step 4, .01 used param value, , on.
i cannot figure out how down since function indexed in such way. there way whilst keeping function indexed way is, somehow updating param new value determined strtime vector?
thanks, , hope clear enough.
create vector, same size time vector t
, appropriate parameter value each time.
taking example data, here 1 way it:
first, @ beginning of strtime , param vectors, add initial conditions (t>=0, param starts .2).
strtime = [0 2 4 9 10];
param = [.2 .21 .01 .25 .05];
next, each element in t
, find largest element in strtime
less-than-or-equal-to time point, , use index find appropriate param. here, i've done arrayfun
rather implementing for
loop, though conceptually same.
p = arrayfun(@(x) param(find(strtime<=x,1,'last')), t);
now you've got vector, p
, can index i
(replace param
in loop section p(i)
).
Comments
Post a Comment