# build a non autonomous model
nVar = 4
dMax = 3
omega = 0.2
gamma = 0.05
KDf=matrix(0, nrow = d2pMax(nVar = nVar, dMax = dMax), ncol = nVar)
KDf[11,1] = 1
KDf[2,2] = 1
KDf[5,2] = 1
KDf[11,2] = -gamma
KDf[35,2] = -1
KDf[2,3] = NA
KDf[2,4] = NA
visuEq(K = KDf, substit = c('x', 'y', 'u', 'v'))
#
# Prepare the external forcing
# number of integration time step
Istep <- 500
# time step
smpl <- 1 / 20
# output time vector
dater <- (0:Istep) * smpl
# hald step time vector (for Runge-Kutta integration)
daterdbl <- (0:(Istep*2 + 1)) * smpl / 2
# generate the forcing (here variables u and v)
extF = cbind(daterdbl, -0.1 * cos(daterdbl * omega), 0.05 * cos(daterdbl * 16/3*omega))
#
# Initial conditions to be used (external variables can be set to 0)
etatInit <- c(-0.616109362 , -0.126882584 , 0, 0)
#
# Numerical integration
reconstr2 <- ode(etatInit, dater, derivODEwMultiX,
KDf, extF = extF, method = 'rk4')
# Reconstruction of the output
nVarExt <- dim(extF)[2] - 1
reconstr2[,(nVar - nVarExt + 2):(nVar + 1)] <- extF[(0:Istep+1)*2, 2:(nVarExt+1)]
Run the code above in your browser using DataLab