# Equation handling in a MTK

**URL:** <https://discourse.julialang.org/t/equation-handling-in-a-mtk/111348>\
**Category:** General Usage\
**Tags:** modelingtoolkit\
**Created:** [March 8, 2024, 11:26am UTC](https://discourse.julialang.org/t/equation-handling-in-a-mtk/111348 "2024-03-08T11:26:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)\
**Post date:** [March 8, 2024, 11:26am UTC](https://discourse.julialang.org/t/equation-handling-in-a-mtk/111348/1 "2024-03-08T11:26:12Z")

</div>

Hello,  
I am playing with a toy problem. I apologies in advance if it is a trivial question.  
I am curious on how the equations of a model are handled.  
For example the following code gives expected results:

```julia
using ModelingToolkit
using DifferentialEquations: solve
@variables t
@mtkmodel LinearEq begin
	@parameters begin
        
     end
  @variables begin
    x(t)
    y(t)
  end
  @equations begin
    10 ~ x + y
    15 ~ x
  end
end

@mtkbuild eq = LinearEq()
prob = ODEProblem(eq,[],(0,1),[])
sol = solve(prob)

```

while the following give error:

```julia
using ModelingToolkit
using DifferentialEquations: solve
@variables t
@mtkmodel LinearEq begin
	@parameters begin
        
     end
  @variables begin
    x(t)
    y(t)
  end
  @equations begin
    10 ~ x + y
    15 ~ x - y
  end
end

@mtkbuild eq = LinearEq()
prob = ODEProblem(eq,[],(0,1),[])
sol = solve(prob)

```

the error message being:

```julia
ERROR: LoadError: ArgumentError: SymbolicUtils.BasicSymbolic{Real}[y(t)] are missing from the variable map.

```

So in the first case the solver identified that the actual equation to solve was only for `y` by substituting the value of `x`. While in the second case it cannot exactly identify that it is a linear system which can be solved by substitutions.

It is possible to explain why the second case does not work while the first one works? And is there a way for the second case to work?

Thank you

---

<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:** [March 8, 2024, 12:08pm UTC](https://discourse.julialang.org/t/equation-handling-in-a-mtk/111348/2 "2024-03-08T12:08:02Z")

</div>

This is not on v9?

---

<div class="post-metadata">

**Author:** ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)\
**Post date:** [March 8, 2024, 1:21pm UTC](https://discourse.julialang.org/t/equation-handling-in-a-mtk/111348/3 "2024-03-08T13:21:06Z")

</div>

I was using 8.75  
I have now updated to 9.1.0 and I have the same issue.

---

<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:** [March 8, 2024, 1:27pm UTC](https://discourse.julialang.org/t/equation-handling-in-a-mtk/111348/4 "2024-03-08T13:27:51Z")

</div>

The new initialization was added in 9.3. I would upgrade to that and follow the tutorial

---

<div class="post-metadata">

**Author:** ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)\
**Post date:** [March 8, 2024, 2:40pm UTC](https://discourse.julialang.org/t/equation-handling-in-a-mtk/111348/5 "2024-03-08T14:40:21Z")

</div>

Hello,  
I get the same issue. I have tried 9.3.0 and 9.4.0

---

<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:** [March 9, 2024, 1:35pm UTC](https://discourse.julialang.org/t/equation-handling-in-a-mtk/111348/6 "2024-03-09T13:35:10Z")

</div>

You should have a new error now that mentions that you’re missing a guess on y?

---

<div class="post-metadata">

**Author:** ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)\
**Post date:** [March 9, 2024, 2:23pm UTC](https://discourse.julialang.org/t/equation-handling-in-a-mtk/111348/7 "2024-03-09T14:23:07Z")

</div>

I still get the same error. The version I am using now is v9.3.0

`ERROR: LoadError: ArgumentError: SymbolicUtils.BasicSymbolic{Real}[y(t)] are missing from the variable map.`

---

<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:** [March 9, 2024, 4:44pm UTC](https://discourse.julialang.org/t/equation-handling-in-a-mtk/111348/8 "2024-03-09T16:44:17Z")

</div>

I’ll look into why this isn’t giving the better error message. The issue of course is that you’re missing guesses for the nonlinear solver:

```julia
using ModelingToolkit
using DifferentialEquations: solve
@variables t
@mtkmodel LinearEq begin
	@parameters begin
        
     end
  @variables begin
    x(t)
    y(t)
  end
  @equations begin
    10 ~ x + y
    15 ~ x - y
  end
end

@mtkbuild eq = LinearEq()
prob = ODEProblem(eq,[],(0,1),[], guesses = [eq.x => 0.0, eq.y => 0.0])
sol = solve(prob)

```

But why it gives the less informative error is worth investigating. This is the expected error:

```julia
julia> prob = ODEProblem(eq,[],(0,1),[], guesses = [eq.x => 0.0])
ERROR: Initial condition underdefined. Some are missing from the variable map.
Please provide a default (`u0`), initialization equation, or guess
for the following variables:

SymbolicUtils.BasicSymbolic{Real}[y(t)]

Stacktrace:
  [1] _varmap_to_vars(varmap::Dict{…}, varlist::Vector{…}; defaults::Dict{…}, check::Bool, toterm::typeof(ModelingToolkit.default_toterm), initialization_phase::Bool)
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/variables.jl:198
  [2] varmap_to_vars(varmap::Vector{…}, varlist::Vector{…}; defaults::Dict{…}, check::Bool, toterm::Function, promotetoconcrete::Nothing, tofloat::Bool, use_union::Bool)
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/variables.jl:152
  [3] get_u0(sys::NonlinearSystem, u0map::Vector{Pair{Num, Float64}}, parammap::Vector{Any}; symbolic_u0::Bool)
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:0
  [4] get_u0
    @ ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:815 [inlined]
  [5] process_NonlinearProblem(constructor::Type, sys::NonlinearSystem, u0map::Vector{…}, parammap::Vector{…}; version::Nothing, jac::Bool, checkbounds::Bool, sparse::Bool, simplify::Bool, linenumbers::Bool, parallel::Symbolics.SerialForm, eval_expression::Bool, use_union::Bool, tofloat::Bool, kwargs::@Kwargs{…})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/nonlinear/nonlinearsystem.jl:365
  [6] process_NonlinearProblem
    @ ~/.julia/dev/ModelingToolkit/src/systems/nonlinear/nonlinearsystem.jl:350 [inlined]
  [7] (NonlinearProblem{…})(sys::NonlinearSystem, u0map::Vector{…}, parammap::Vector{…}; check_length::Bool, kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/nonlinear/nonlinearsystem.jl:401
  [8] (NonlinearProblem{true})(sys::NonlinearSystem, u0map::Vector{Pair{Num, Float64}}, parammap::Vector{Any})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/nonlinear/nonlinearsystem.jl:395
  [9] NonlinearProblem(::NonlinearSystem, ::Vector{Pair{Num, Float64}}, ::Vararg{Any}; kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/nonlinear/nonlinearsystem.jl:392
 [10] NonlinearProblem(::NonlinearSystem, ::Vector{Pair{Num, Float64}}, ::Vararg{Any})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/nonlinear/nonlinearsystem.jl:391
 [11] ModelingToolkit.InitializationProblem{…}(sys::ODESystem, t::Int64, u0map::Vector{…}, parammap::Vector{…}; guesses::Vector{…}, check_length::Bool, warn_initialize_determined::Bool, kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1601
 [12] kwcall(::NamedTuple, ::Type{…}, sys::ModelingToolkit.AbstractODESystem, args::Vararg{…})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1558 [inlined]
 [13] #_#762
    @ ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1536 [inlined]
 [14] kwcall(::NamedTuple, ::Type{…}, sys::ModelingToolkit.AbstractODESystem, args::Vararg{…})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1535 [inlined]
 [15] #InitializationProblem#760
    @ ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1524 [inlined]
 [16] kwcall(::NamedTuple, ::Type{…}, sys::ModelingToolkit.AbstractODESystem, args::Vararg{…})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1523 [inlined]
 [17] process_DEProblem(constructor::Type, sys::ODESystem, u0map::Vector{…}, parammap::Vector{…}; implicit_dae::Bool, du0map::Nothing, version::Nothing, tgrad::Bool, jac::Bool, checkbounds::Bool, sparse::Bool, simplify::Bool, linenumbers::Bool, parallel::Symbolics.SerialForm, eval_expression::Bool, use_union::Bool, tofloat::Bool, symbolic_u0::Bool, u0_constructor::typeof(identity), guesses::Vector{…}, t::Int64, warn_initialize_determined::Bool, kwargs::@Kwargs{…})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:886
 [18] process_DEProblem
    @ ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:844 [inlined]
 [19] (ODEProblem{…})(sys::ODESystem, u0map::Vector{…}, tspan::Tuple{…}, parammap::Vector{…}; callback::Nothing, check_length::Bool, warn_initialize_determined::Bool, kwargs::@Kwargs{…})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1046
 [20] (ODEProblem{true})(::ODESystem, ::Vector{Any}, ::Vararg{Any}; kwargs::@Kwargs{guesses::Vector{Pair{Num, Float64}}})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1023
 [21] ODEProblem(::ODESystem, ::Vector{Any}, ::Vararg{Any}; kwargs::@Kwargs{guesses::Vector{Pair{Num, Float64}}})
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/systems/diffeqs/abstractodesystem.jl:1012
 [22] top-level scope
    @ REPL[2]:1
Some type information was truncated. Use `show(err)` to see complete types.

```

---

<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:** [March 9, 2024, 4:45pm UTC](https://discourse.julialang.org/t/equation-handling-in-a-mtk/111348/9 "2024-03-09T16:45:09Z")

</div>

> [@Sushrut\_Deshpande](#):
>
> It is possible to explain why the second case does not work while the first one works? And is there a way for the second case to work?

The first one works because it analytically solves the equation, while the second one hits the nonlinear solver.
