我正在尝试建立一个ODE系统,其中变量(FA2)随每个时间步长而变化。我有以下代码。
注意:FA2定义为FA2 = b + a * i,并且不能大于90
模型功能:
var x = 0;
var int = 1000; //1000ms interval
function Start(){
setInterval(function(){
console.log(x += 1); //increasing x value by 1 every int miliseconds
console.log("Interval: " + int); //outputting interval value (in my case, it is always 1000ms, i want it to be for example: 1000ms, 950ms, 900ms, 850ms, ... , 15ms, 10ms, 5ms, 0ms)
}, int); //how to increase/decrease this 'int' value gradually - (for example: 1000ms, 950ms, 900ms, 850ms, ... , 15ms, 10ms, 5ms, 0ms)
}
输出:
model <- function(times, y, pars) {
with(as.list(c(pars, y)), {
# set up vector of FA2 values
FA2_1=c()
for (i in 1:90){
FA2_1[i]=b+i*a
}
FA2_1=c(b,FA2_1)
# make ifelse statement
FA=ifelse(FA2_1>=90,90,FA2_1)
FA2=c()
for (i in 1:length(times)){
FA2[i]=ifelse(times[i]>TR,FA[i],FA[1])
}
# the ode system
dS <- -k1*S-(1/T1s)*S-S*(1-cos(FA1*pi/180))/TR
dP <- k1*S-(1/T1p)*P-P*(1-cos(FA2*pi/180))/TR
list(c(dS,dP))
})
}
y = c(S = 1 ,P=0) # initial values
parms=c(k1=0.016,T1p=57,T1s=36) # pars
times=c(0,2,4,6,8,10) # time vector
a=1 # can be any positive value this is just an example
b=12 # can be any positive value this is just an example
TR=2 # can be any positive value
FA1=1 # cen be any positive value
现在的问题似乎是求解器忽略了 out1=ode(y, times, model,parms)
out1=data.frame(out1)
语句中FA2的不同值,而只是从FA2向量中选取了ifelse
的第一个元素。
我只是尝试给函数声明
FA2[1]=12
代替函数中发布的语句为:
FA2=ifelse(times>=2,FA[2],FA[1])
和小的FA2=c()
for (i in 1:length(times)){
FA2[i]=ifelse(times[i]>TR,FA[i],FA[1])
}
语句而没有循环似乎使求解器对改变a的值作出反应。但是,当时间步长变化时,我不仅需要FA向量的第一个值。有谁知道如何解决这个问题?