Get the ODE function from ModelingToolkit

Get the ODE function from ModelingToolkit#

f = ODEFunction(sys) could be useful in visualizing vector fields.

using ModelingToolkit
using OrdinaryDiffEq
using Plots

@independent_variables t
@variables x(t) RHS(t)
@parameters τ
D = Differential(t)
Differential(t)

Equations in MTK use the tilde character (~) as equality. Every MTK system requires a name. The @named macro simply ensures that the symbolic name matches the name in the REPL.

eqs = [
    RHS  ~ (1 - x)/τ,
    D(x) ~ RHS
]

@mtkbuild fol_separate = ODESystem(eqs, t)
f = ODEFunction(fol_separate)
(::SciMLBase.ODEFunction{true, SciMLBase.AutoSpecialize, ModelingToolkit.GeneratedFunctionWrapper{(2, 3, true), RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:__mtk_arg_1, :___mtkparameters___, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x2a59dbb7, 0xf194f59f, 0x6922c903, 0xbdd1941b, 0xafd92ca4), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :__mtk_arg_1, :___mtkparameters___, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x71ded026, 0x11a0f069, 0xcbd60c33, 0xdad174c8, 0xbf458f73), Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ModelingToolkit.ODESystem}, Nothing, ModelingToolkit.ODESystem, Nothing, Nothing}) (generic function with 3 methods)

f(u, p, t) returns the value of derivatives

f([0.0], [1.0], 0.0)
1-element Vector{Float64}:
 1.0

If you already have the ODEproblem, the function is prob.f.

prob = ODEProblem(fol_separate, [x => 0.0], (0.0, 1.0), [τ => 1.0]);
f = prob.f
f([0.0], [1.0], 0.0)
1-element Vector{Float64}:
 1.0

This notebook was generated using Literate.jl.