# Extracting ODEProblem formulas and passing them to JuMP NLExpressions

**URL:** https://discourse.julialang.org/t/extracting-odeproblem-formulas-and-passing-them-to-jump-nlexpressions/92910
**Category:** Modelling & Simulations
**Created:** [January 13, 2023, 6:52am UTC](https://discourse.julialang.org/t/extracting-odeproblem-formulas-and-passing-them-to-jump-nlexpressions/92910 "2023-01-13T06:52:11Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![zornsllama](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zornsllama/32/24636_2.png) [@zornsllama](https://discourse.julialang.org/u/zornsllama)
#### Post date: [January 13, 2023, 6:52am UTC](https://discourse.julialang.org/t/extracting-odeproblem-formulas-and-passing-them-to-jump-nlexpressions/92910/1 "2023-01-13T06:52:11Z")

</div>

I am trying to write a code that extracts a model’s equations from a DiffEq.jl ODEProblem or ModelingToolkit.jl ODESystem and copies them into JuMP.jl NLExpressions for use in nonlinear programming. In particular, I need each coordinate function of the vector field separately, ideally as a Julia expression in the variables x[i] and p[i]. Is there any interface in the SciML ecosystem that would facilitate this? It would be nice to be able to write nicely annotated vector field functions (e.g. using variable names other than x and p) and then automatically generate the less readable version as “copyable Julia code” (as opposed to a compiled ODEFunction). Any help is much appreciated!

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [January 13, 2023, 9:33am UTC](https://discourse.julialang.org/t/extracting-odeproblem-formulas-and-passing-them-to-jump-nlexpressions/92910/2 "2023-01-13T09:33:36Z")

</div>

If you just trace it with symbolic expressions and build the OptimizationSystem, you’ll get what you’re looking for.

---

<div class="post-metadata">

### Author: ![zornsllama](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zornsllama/32/24636_2.png) [@zornsllama](https://discourse.julialang.org/u/zornsllama)
#### Post date: [January 13, 2023, 3:23pm UTC](https://discourse.julialang.org/t/extracting-odeproblem-formulas-and-passing-them-to-jump-nlexpressions/92910/3 "2023-01-13T15:23:56Z")

</div>

Would you mind elaborating slightly? In particular, some specific questions:

Does “trace it with symbolic expressions” just mean passing Symbolics.jl variables as input to the vector field?

Once the OptimizationSystem is built, will [`OptimizationProblemExpr`](https://docs.sciml.ai/ModelingToolkit/stable/systems/OptimizationSystem/#ModelingToolkit.OptimizationProblemExpr) enable recovery of the equations in the desired format?

Also, how does one use the latter function? When I run it on the example system on the OptimizationSystem page (`OptimizationProblemExpr(os,DiffEqBase.NullParameters())`), I receive `ERROR: MethodError: no method matching getindex(::Nothing, ::Int64)`. I’m assuming `NullParameters()` isn’t the right thing to pass as second argument there, so what is?

---

<div class="post-metadata">

### Author: ![Vaibhavdixit02](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vaibhavdixit02/32/2916_2.png) [@Vaibhavdixit02](https://discourse.julialang.org/u/Vaibhavdixit02)
#### Post date: [January 16, 2023, 4:23am UTC](https://discourse.julialang.org/t/extracting-odeproblem-formulas-and-passing-them-to-jump-nlexpressions/92910/4 "2023-01-16T04:23:11Z")

</div>

You don’t need to use `OptimizationProblemExpr` just `OptimizationProblem`. See the example here hopefully that clears things up [Symbolic Problem Building with ModelingToolkit · Optimization.jl](https://docs.sciml.ai/Optimization/stable/tutorials/symbolic/) and [Modeling Optimization Problems · ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/stable/tutorials/optimization/)

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [January 16, 2023, 3:52pm UTC](https://discourse.julialang.org/t/extracting-odeproblem-formulas-and-passing-them-to-jump-nlexpressions/92910/5 "2023-01-16T15:52:21Z")

</div>

> **[Some Fun With Julia Types: Symbolic Expressions in the ODE Solver -...](https://www.stochasticlifestyle.com/fun-julia-types-symbolic-expressions-ode-solver/)**
>
> In Julia, you can naturally write generic algorithms which work on any type which has specific “actions”. For example, an “AbstractArray” is a type which has a specific set of functions implemented. This means that in any generically-written...

That’s a very old blog post, and uses SymPy instead of symbolics, but you’d just do that with Symbolics and get expressions from the ODE solver for the update equations out.
