# Composing functions in Modeling Toolkit

**URL:** <https://discourse.julialang.org/t/composing-functions-in-modeling-toolkit/128116>\
**Category:** New to Julia\
**Tags:** modelingtoolkit\
**Created:** [April 16, 2025, 9:32am UTC](https://discourse.julialang.org/t/composing-functions-in-modeling-toolkit/128116 "2025-04-16T09:32:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JaviJR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/javijr/32/218294_2.png) [@JaviJR](https://discourse.julialang.org/u/JaviJR)\
**Post date:** [April 16, 2025, 9:32am UTC](https://discourse.julialang.org/t/composing-functions-in-modeling-toolkit/128116/1 "2025-04-16T09:32:22Z")

</div>

Hello,  
I’m exploring the ModelingToolkit environment and I’ve encountered a question when composing systems. When I define a complete model using a function, it works without any issues. However, when I split the model into two submodels, I get the following error:

"MethodError: no method matching -(::SymbolicUtils.BasicSymbolic{Real}, ::ModelingToolkit.ODESystem)

The function `-` exists, but no method is defined for this combination of argument types."

I’m attaching both versions of the code in case you can help me identify the error causing this behavior.

Working code:

```julia
function Sistema(data, time; name)
    @named src = Interpolation(LinearInterpolation, data, time)
    @named clk = ContinuousClock()
	@named transfer = TransferFunction(b=[1 2], a=[1 3 2])
    eqs = [connect(clk.output,src.input)
		  connect(src.output,transfer.input)]
 
    ODESystem(eqs, t,[], []; name, systems = [src, clk,transfer])
end

@named system1 = Sistema(1,2,3,4,5,6,7,8,9,10], [0,1,2,3,4,5,6,7,8,9]))

sys1 = structural_simplify(system1)

prob1 = ODEProblem(sys1, [], (0,9))
sol1 = solve(prob1)
plot(sol1)

```

**Not working code:**

```julia
function SistemaImplementar(; name)
	@named input = RealInput()
    @named transfer = TransferFunction(b = [1, 2], a = [1, 3, 2])

    
    # Create an input variable that will be exposed
    eqs = [

        transfer.input ~ input.u # Expose input
    ]
    
    # Add input to the list of inputs for this system
    ODESystem(eqs, t,[], []; name, systems = [transfer,input])
end

function SistemaCompleto(data, time; name)
    @named src = Interpolation(LinearInterpolation, data, time)
    @named clk = ContinuousClock()
	@named sistemaexterno = SistemaImplementar()
	
    eqs = [connect(clk.output,src.input)
		  connect(src.output,sistemaexterno.input)]
 
    ODESystem(eqs, t; name, systems = [src, clk,sistemaexterno])
end

@named systemaprueba = SistemaCompleto(1,2,3,4,5,6,7,8,9,10], [0,1,2,3,4,5,6,7,8,9]))

sys2bis = structural_simplify(systemaprueba)

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [April 16, 2025, 9:55am UTC](https://discourse.julialang.org/t/composing-functions-in-modeling-toolkit/128116/2 "2025-04-16T09:55:12Z")

</div>

> [@JaviJR](#):
>
> `transfer.input ~ input.u`

Should be either of

```julia-auto
connect(transfer.input, input)
transfer.input.u ~ input.u 

```

---

<div class="post-metadata">

**Author:** ![JaviJR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/javijr/32/218294_2.png) [@JaviJR](https://discourse.julialang.org/u/JaviJR)\
**Post date:** [April 16, 2025, 10:15am UTC](https://discourse.julialang.org/t/composing-functions-in-modeling-toolkit/128116/3 "2025-04-16T10:15:13Z")

</div>

What a simple mistake!  
Thank you very much \<3

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [April 16, 2025, 10:36am UTC](https://discourse.julialang.org/t/composing-functions-in-modeling-toolkit/128116/4 "2025-04-16T10:36:21Z")

</div>

I have made the same mistake several times, so I’m an expert at detecting them 😉 The key clue is that you try to perform an arithmetic operation (`-`) on `::ODESystem`, this is because `input` is a connector, and thus an `ODESystem`, while `input.u` is the inner variable on which you can call `-`.

---

<div class="post-metadata">

**Author:** ![JaviJR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/javijr/32/218294_2.png) [@JaviJR](https://discourse.julialang.org/u/JaviJR)\
**Post date:** [April 16, 2025, 11:44am UTC](https://discourse.julialang.org/t/composing-functions-in-modeling-toolkit/128116/5 "2025-04-16T11:44:24Z")

</div>

And would it be possible to mix elements defined using `@mtkmodel` and elements defined using functions? That is, for example, by defining the first function in my code as an `@mtkmodel` environment."

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [April 16, 2025, 11:45am UTC](https://discourse.julialang.org/t/composing-functions-in-modeling-toolkit/128116/6 "2025-04-16T11:45:36Z")

</div>

> [@JaviJR](#):
>
> And would it be possible to mix elements defined using `@mtkmodel` and elements defined using functions?

Yes, that should be no problem.
