Skip to contents

ODEForwardSensitivityProblem(prob) (or (f, u0, tspan, p)) builds an ODE problem whose state is the original state followed by its sensitivities, solved with solve() like any ODEProblem. extract_local_sensitivities(sol) splits the solution into the values and the sensitivities.

Usage

ODEForwardSensitivityProblem(
  f,
  u0 = NULL,
  tspan = NULL,
  p = NULL,
  sensealg = ForwardSensitivity(),
  du0 = NULL,
  dp = NULL,
  ...
)

extract_local_sensitivities(sol, i = NULL, asmatrix = FALSE, t = NULL)

Arguments

f

An ODEProblem, a complete System, an R function f(u, p, t) or an ODEFunction.

u0, tspan, p

Initial values, time span and parameters (ignored when f is a problem; for a System u0 is the operating point).

sensealg

ForwardSensitivity() or ForwardDiffSensitivity().

du0, dp

Seed matrices (see Details).

...

Further arguments for ODEProblem() when f is a System or function.

sol

A solution of an ODEForwardSensitivityProblem.

i

Index of a saved time; NULL for all saved times.

asmatrix

Return the sensitivities as one n x m matrix (per time) instead of a list with one vector (or time series) per direction.

t

Times to interpolate at instead of i.

Value

An ODEProblem with the augmented state.

Details

With sensealg = ForwardSensitivity() the sensitivities are with respect to the parameters p. ForwardDiffSensitivity() takes seed matrices: du0 (n x m) and dp (length(p) x m) give the derivatives of the initial values and the parameters with respect to m directions (cbind(diag(n), 0) and cbind(0, diag(length(p))) differentiate with respect to both). For a problem built from a System, du0 adds the dependence of the initial values on the parameters through initialization. Events (callbacks of the problem) are differentiated, including the dependence of the event time on the parameters.

Derivatives of the right-hand side come from the System (symbolic Jacobians compiled to R) or, for R functions, from jac_fn or central finite differences.

Examples

prob <- ODEForwardSensitivityProblem(function(u, p, t) -p[1] * u, 1, c(0, 1), 2)
sol <- solve(prob, Tsit5(), reltol = 1e-8, abstol = 1e-10)
s <- extract_local_sensitivities(sol, length(sol$t))
s$du[[1]] + exp(-2)   # d u(1) / dp = -exp(-2)
#>           u1 
#> 3.722506e-11