我试图在微分方程组中求解并优化参数和初始条件值。但是,由于错误消息,我无法运行代码(当FS作为等式给出时,此代码工作正常)。由于数据点有限,我希望能够针对整个系统的E值进行优化(使用等式F计算)。我已经使代码工作到LLode功能,因此优化根本不会发生。 由于论坛回复(Lyngbakr),我设法解决了这个错误:
initSim<-ode(initCond, tspan, hormonesode, params, method="ode45",atol = 1e-10, rtol = 1e-10,hmax=NULL,maxsteps=100000000000000000)
警告讯息:
在rk(y,times,func,parms,method =&#34; ode45&#34;,...)中: 在t = 0时,时间步数1超过最大步数
我将不胜感激。
Malgosia
我的代码如下:
tspan<-c(0,1,2,3,4,5)#,6,7,8,9,16,17,18,19)
E<-c(0.303,0.205,0.205,0.381,0.272,0.188)#,0.317,0.274,0.106,0.161,0.947,2.722,4.701,0.24)
names(df)=c("tspan","E")
require(deSolve)
#initial parameter values
#k1<-1.062169#0.370 7.754
k2<-1.908891#-0.00727284 0.022
#k3<-0.321
k4<-2.14
k5<-10.7
#k6<-1.07
A0<-1.38 #15.47
B0<-0.61 #0.298
C0<-0.28
#F0<-0.303#3.28803757 3.434
#define a weight vector outside LLODE function
wts<-sqrt(sqrt(E))
#combine parameters into a vector
params<-c(k2,k4,k5,A0,B0,C0)
names(params)<-c("k2","k4","k5","A0","B0","C0")
#ode function
hormonesode<-function(t,x,params){
A<-x[1]
B<-x[2]
C<-x[3]
D<-x[4]
E<-x[5]
F<-x[6]
# k1<-params[1]
k2<-params[1]
#k3<-params[3]
k4<-params[2]
k5<-params[3]
A0<-params[4]
B0<-params[5]
C0<-params[6]
P<-3.02796-3.1867*cos(0.314159*t)-0.55332*cos(2*0.314159*t)+0.362678*cos(3*0.314159*t)+0.486708*cos(4*0.314159*t)-0.10133*cos(5*0.314159*t)-0.21977*cos(6*0.314159*t)-0.08926*cos(7*0.314159*t)+0.222292*cos(8*0.314159*t)-1.05119*sin(0.314159*t)+0.855633*sin(2*0.314159*t)+0.176677*sin(3*0.314159*t)-0.05658*sin(4*0.314159*t)-0.34108*sin(5*0.314159*t)-0.15718*sin(6*0.314159*t)+0.397642*sin(7*0.314159*t)-0.0986*sin(8*0.314159*t)
FS<-0.1944+0.002017*cos(0.261799*t)+0.009603*cos(2*0.261799*t)+0.01754*cos(3*0.261799*t)+0.106208*cos(4*0.261799*t)+0.020423*cos(5*0.261799*t)+0.015417*cos(6*0.261799*t)+0.01079*cos(7*0.261799*t)+0.115042*cos(8*0.261799*t)+0.008853*sin(0.261799*t)+0.013523*sin(2*0.261799*t)+0.012254*sin(3*0.261799*t)+0.026053*sin(4*0.261799*t)+0.000957*sin(5*0.261799*t)-0.001*sin(6*0.261799*t)+0.002374*sin(7*0.261799*t)+0.026775*sin(8*0.261799*t)
dA<-1.0621*1/(1+(1/5*P)^5)*(1/3*(F^10)/(1+(F)*1/3)^10)-2.14*A;
dB<-75*(((A/5)^10)/(1+(A/5)^10))-8.56*B;
dC<-1.909*FS+0.321*FS*C- 0.749*C;
dD<-0.749*C- 0.749*D+0.214*FS*D^2;
dE<-0.749*D-0.749*E+0.214*B*E^2;
dF<-k2 + k4*D + k5*E-1.07*F;
output<-c(dA, dB, dC, dD, dE, dF)
list(output)
}
#Initial conditions
A0<-2500#1.0038#2.794
B0<-105#25.0061#6.13
C0<-0.018#0.02#0.06126
D0<-0
E0<-0
F0<-0.303#3.28803757 3.434
initCond<-c(A0, B0, C0, D0, E0, F0)
initCond
#run ode with initial guesses
initSim<-ode(initCond, tspan, hormonesode, params, method="ode45",atol = 1e-1, rtol = 1e-1,hmax=NULL, maxsteps=100000)
plot(tspan,initSim[,7], type="l", lty="dashed")
points(tspan,E)
initSim
LLode<-function(params){
A0<-params[4]
B0<-params[5]
C0<-params[6]
D0<-0
E0<-0
F0<-0.303
initCond<-c(A0, B0, C0, D0, E0, F0)
#Run the ODE
odeOut<-ode(initCond,tspan,hormonesode,params,method="ode45",atol = 1e-1, rtol = 1e-1,hmax=NULL, maxsteps=100000)
if(attr(odeOut,"istate")[1]!=0){
#Check if satisfactory 2 indicates perceived success of method 'lsoda', 0 for
#'ode45'. Other integrators may have different indicator codes
cat("Integration possibly failed\n")
LL<-.Machine$double.xmax*1e-06#indicate failure
}else{
y<-odeOut[,7] #measurement variable
wtDiff1<-(y-E)*wts#weighted difference
LL<-as.numeric(crossprod(wtDiff1))#Sum of squares
}
LL
}
# optimize uzing optim()
MLoptres<-optim(params,LLode,method="Nelder-Mead",
control=list(trace=0,maxit=500))
MLoptres
require(optimx)
optxres<-optimx(params,LLode,lower=rep(0,6),upper=rep(200,6),
method=c("nmkb","bobyqa"),
control=list(usenumDeriv=TRUE, maxit=500,trace=0))
summary(optxres,order=value)
bestpar<-coef(summary(optxres,order=value)[1,])
cat("best parameters:")
print(bestpar)
#dput(optxres,file='includes/C20bestpar.dput')
bpSim<-ode(initCond,tspan,hormonesode,bestpar,method="ode45")
X11()
plot(tspan,initSim[,7],type="l",lty="dashed")
points(tspan,E)
#points(tspan,MLoptres[,]/k,type="l",lty="twodash")
points(tspan,bpSim[,7],type="l")
title(main="Improved fit using optimx")