Define compiled code for ordinary differential equation.
specify_ode(code, fname = NULL, pname = c("dy", "x", "y", "p"))
string with the body of the function definition (see details)
Optional name of the exported C++ function
Vector of variable names (results, inputs, states, parameters)
pointer (externalptr) to C++ function
The model (code
) should be specified as the body of of C++ function.
The following variables are defined bye default (see the argument pname
)
dyVector with derivatives, i.e. the rhs of the ODE (the result).
xVector with the first element being the time, and the following elements additional exogenous input variables,
yVector with the dependent variable
pParameter vector
\(y'(t) = f_{p}(x(t), y(t))\) All variables are treated as Armadillo (http://arma.sourceforge.net/) vectors/matrices.
As an example consider the Lorenz Equations \(\frac{dx_{t}}{dt} = \sigma(y_{t}-x_{t})\) \(\frac{dy_{t}}{dt} = x_{t}(\rho-z_{t})-y_{t}\) \(\frac{dz_{t}}{dt} = x_{t}y_{t}-\beta z_{t}\)
We can specify this model as
ode <- 'dy(0) = p(0)*(y(1)-y(0));
dy(1) = y(0)*(p(1)-y(2));
dy(2) = y(0)*y(1)-p(2)*y(2);'
dy <- specify_ode(ode)
As an example of model with exogenous inputs consider the following ODE:
\(y'(t) = \beta_{0} + \beta_{1}y(t) + \beta_{2}y(t)x(t) + \beta_{3}x(t)\cdot t\)
This could be specified as
mod <- 'double t = x(0);
dy = p(0) + p(1)*y + p(2)*x(1)*y + p(3)*x(1)*t;'
dy <- specify_ode(mod)
##'
solve_ode