我有一个非 ode 函数,它根据时间向量和空间变量向量的前一个时间步长进行计算。
此函数中的参数之一是常量。但是,我想将该参数设为变量,该变量将在给定时间发生变化。
这是一些示例代码,可以清楚地说明这一点。
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
for i = 1:(numel(t)-1)
for j = 2:(numel(x)-1)
k(j,i+1) = 5*2+c(j+1,i)+param*c(j,1)+param
end
end
如果 param 保持不变,这将没有问题。我想要做的是有一个参数的随机数向量,它在随机时间输入参数的随机数。随机时间由 strtime 确定。
So for example, if strtime vector = [2 4 9 10], and
param vector = [.21 .01 .25 .05]
First, I want an initial param value at time 0 = .2 (arbitrary). As soon as my
time vector matches with my strtime vector, which in this example is 2, then
my param value is updated to .21. .21 will be used at each time step until my
time vector matches my strtime vector again, which in this case, would be 4. By
time step 4, .01 is used as my param value, and so on.
我无法真正弄清楚如何解决这个问题,因为该函数是以这种方式索引的。有没有办法做到这一点,同时保持函数的索引方式,只是以某种方式将参数更新为由 strtime 向量确定的新值?
谢谢,希望我足够清楚。