Build a symbolic system from a numerical problem
Source:R/modelingtoolkitize-trace.R
modelingtoolkitize.Rdmodelingtoolkitize(prob) traces the function of a numerical problem
with symbolic arguments and returns the equivalent System, so the
symbolic tools (Jacobians, simplification, index reduction) apply to code
written for the numerical solvers. Methods exist for ODEProblem (f(u, p, t)), NonlinearProblem and NonlinearLeastSquaresProblem (f(u, p)), OptimizationProblem (objective f(u, p), constraints, bounds) and
SDEProblem (drift and diffusion).
Usage
modelingtoolkitize(prob, ...)
# S3 method for class 'ODEProblem'
modelingtoolkitize(
prob,
u_names = NULL,
p_names = NULL,
...,
return_symbolic_u0_p = FALSE
)
# S3 method for class 'AbstractNonlinearProblem'
modelingtoolkitize(prob, u_names = NULL, p_names = NULL, ...)
# S3 method for class 'NonlinearProblem'
modelingtoolkitize(prob, u_names = NULL, p_names = NULL, ...)
# S3 method for class 'OptimizationProblem'
modelingtoolkitize(prob, u_names = NULL, p_names = NULL, ...)
# S3 method for class 'SDEProblem'
modelingtoolkitize(prob, u_names = NULL, p_names = NULL, ...)Arguments
- prob
A problem.
- ...
Passed to
System(), for examplename.- u_names, p_names
Names of the unknowns and parameters.
- return_symbolic_u0_p
Internal: also return the traced variables.
Details
The function is called with symbolic vectors for u and p (and the
time t): indexing (u[1], p["k"], p$k), arithmetic, math functions,
sum(), prod() and c() work as for numeric vectors, and the function
must return its result (a vector built with c() or a list).
Branching on symbolic values cannot be traced.
Unknowns are named from u_names, the system the problem was built from,
the names of u0, or x₁, x₂, ...; parameters from p_names, the
system, the names of p, or α₁, α₂, ... (α for a single number). The
values of u0 and p become initial conditions of the system.
Examples
lotka <- function(u, p, t) {
x <- u[1]; y <- u[2]
c(p[1] * x - p[2] * x * y, -p[3] * y + p[4] * x * y)
}
prob <- ODEProblem(lotka, c(1, 1), c(0, 10), c(1.5, 1, 3, 1))
sys <- modelingtoolkitize(prob, name = "lv")
equations(sys)
#> [[1]]
#> Derivative(x₁(t), t) ~ α₁*x₁(t) - α₂*x₁(t)*x₂(t)
#>
#> [[2]]
#> Derivative(x₂(t), t) ~ -α₃*x₂(t) + α₄*x₁(t)*x₂(t)
#>