# MATLAB/Simulink model simulation and modeling with s-functions and variable-step solver

**URL:** <https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 25, 2023, 12:27pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220 "2023-09-25T12:27:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![alparg-mv](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@alparg-mv](https://discourse.julialang.org/u/alparg-mv)\
**Post date:** [September 25, 2023, 12:27pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/1 "2023-09-25T12:27:11Z")

</div>

As a 10+ year old company working on HVAC system modeling on MATLAB/Simulink, we would like to transition to Julia for faster simulation and modeling of our Simulink model, where we also plan to accelerate the Simulation via surrogates.

However, our Simulink model contains s-functions and the variable-step solver as shown in the figure below:

![MicrosoftTeams-image](https://global.discourse-cdn.com/julialang/original/3X/c/0/c09fd967075837e9d82b74f89165f9a778ccd08e.png)

Hence, we currently have two limitations:

- Due to the presence of s-functions, the exported FMUs run slow in Python  
-The exported FMUs can only be run if the hardware contains matlab license.

Before transitioning to Julia, I therefore would like to ask:

1. Does Julia support s-functions and variable-step solver when exporting and importing a model as FMU?
2. Is a Matlab license needed to run the FMU if the FMU contains s-functions?

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 25, 2023, 12:35pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/2 "2023-09-25T12:35:14Z")

</div>

Well, if you continue to use Matlab solvers you need a Matlab license. But you will get the advantages of Julia only if you do NOT use the Matlab solvers but the DifferentialEquations.jl package. There are two different types of FMUs, FMU for co-simulation and FMU for model export ([FMI Standard: Co-Simulation vs. Model Exchange FMUs](https://modelon.com/blog/fmi-functional-mock-up-unit-types/)). You will get the advantages of Julia only if you use an FMU for model export, because only then the Julia solvers will be used.

I cannot comment on your question regarding the s-functions.

I re-wrote a Simulink model (three A3 pages in printed form) in ModelingToolkit (Julia) and it had only  
a length of half an A4 page and was 1000 times faster.

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [September 25, 2023, 12:37pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/3 "2023-09-25T12:37:36Z")

</div>

I looked around for an s-function API and couldn’t find one. I think that it is short for “Simulink Function” and I think that MathWorks intended it to be a MEX interface. [This is](https://www.mathworks.com/help/simulink/sfg/what-is-an-s-function.html) pretty much most of what I found that was useful.

I imagine that if you wanted to keep your existing s-function source code, that you’d need to write a wrapper layer for it, however, porting it to Julia will likely make your life much easier in the long run.

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [September 25, 2023, 1:28pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/4 "2023-09-25T13:28:51Z")

</div>

Hello @ufechner7 I’ve been looking at moving my current Simulink model over to Julia’s ecosystem and I was wondering if you know of any resources/guides to help me get started, considering that you have gone through that experience.

What I’m most concerned about actually is how can I visualize the output of ModellingToolkit similar to how Simulink displays its model. I’m perfectly fine with using text to code up my models, but I’d really like the ability to graphical visualize the models I create.

---

<div class="post-metadata">

**Author:** ![DrPapa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drpapa/32/6835_2.png) [@DrPapa](https://discourse.julialang.org/u/DrPapa)\
**Post date:** [September 25, 2023, 1:35pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/5 "2023-09-25T13:35:42Z")

</div>

> I’ve been looking at moving my current Simulink model over to Julia’s ecosystem and I was wondering if you know of any resources/guides to help me get started, considering that you have gone through that experience.

Same here. I am especially interested in using FMI for model export of Simulink models for simulation in Julia.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 25, 2023, 1:37pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/6 "2023-09-25T13:37:39Z")

</div>

Did you look at [GitHub - ThummeTo/FMI.jl: FMI.jl is a free-to-use software library for the Julia programming language which integrates FMI (fmi-standard.org): load or create, parameterize, differentiate and simulate FMUs seamlessly inside the Julia programming language!](https://github.com/ThummeTo/FMI.jl) ?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 25, 2023, 1:42pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/7 "2023-09-25T13:42:28Z")

</div>

> [@acxz](#):
>
> how can I visualize the output of ModellingToolkit

Well, I just plot the output using one of the many plotting packages. Usually I write custom plot functions that plot for example four line plots in one diagram similar to the Simulink oscilloscope.

I had hardly any need yet to have a continues update of the output because Julia is so fast that the complete output is ready within a few seconds, but when I needed it for some real-time simulation with real-time input I used Makie.jl. But then you have to write your own, custom viewer like I did here:

> **[GitHub - ufechner7/KiteViewer: 3D viewer and simulator for airborne wind...](https://github.com/ufechner7/KiteViewer)**
>
> 3D viewer and simulator for airborne wind energy systems - GitHub - ufechner7/KiteViewer: 3D viewer and simulator for airborne wind energy systems

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 25, 2023, 1:45pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/8 "2023-09-25T13:45:13Z")

</div>

A simple example for plotting the result of a ModelingToolkit model can be found here: [Step response using ModelingToolkit](https://discourse.julialang.org/t/step-response-using-modelingtoolkit/99846)

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [September 25, 2023, 1:47pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/9 "2023-09-25T13:47:28Z")

</div>

Thanks for the link to FMI.jl, will definitely need that to import the exported Simulink FMI’s into Julia.

Sorry I should have been more clear. I was asking more along the lines of visualizing the associated graphical model that is modelled in ModelingToolkit, akin to how we edit model files in Simulink.

---

<div class="post-metadata">

**Author:** ![DrPapa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drpapa/32/6835_2.png) [@DrPapa](https://discourse.julialang.org/u/DrPapa)\
**Post date:** [September 25, 2023, 1:51pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/10 "2023-09-25T13:51:13Z")

</div>

I looked through the docs, but it was not immediately clear if `fmiSimulate` was only applicable for co-simulation or if it interfaces more generally to the SciML/DiffEq ecosystem. Looking at the src, I now see that it is.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 25, 2023, 2:36pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/11 "2023-09-25T14:36:54Z")

</div>

I use [Control Block Diagram with TikZ - Overleaf, Online LaTeX Editor](https://www.overleaf.com/latex/examples/control-block-diagram-with-tikz/jfdcfxhgjmtz) , but that is time consuming. Good for scientific papers, not for daily engineering work.

I think [Home Â· JuliaSimControl](https://help.juliahub.com/juliasimcontrol/dev/) has a graphical model editor, but I never used it.

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [September 25, 2023, 3:02pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/12 "2023-09-25T15:02:43Z")

</div>

Thanks for the JuliaSim link.

Another thought considering that I’d like a graphical view of ModelingToolkit, was if I can graphically visualize an FMU that is exported from ModelingToolkit?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 25, 2023, 3:10pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/13 "2023-09-25T15:10:37Z")

</div>

What do you want to visualize? The inner working of the FMU, or the inputs and outputs?

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [September 25, 2023, 3:14pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/14 "2023-09-25T15:14:48Z")

</div>

Really just the inputs and outputs so that I can understand the flow of data and requirements of each block.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 25, 2023, 4:03pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/15 "2023-09-25T16:03:55Z")

</div>

I do not think that this exists in the Julia ecosystem. You could create a feature request in fmi.jl. But you can also use any other tool for this purpose, because the input/output definition of an FMI is just an xml file. Perhaps there exists a Python tool that can read this xml file and create a nice graph?

---

<div class="post-metadata">

**Author:** ![bilderbuchi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bilderbuchi/32/13562_2.png) [@bilderbuchi](https://discourse.julialang.org/u/bilderbuchi)\
**Post date:** [September 25, 2023, 4:56pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/16 "2023-09-25T16:56:08Z")

</div>

OMSimulator might be able to help visualize/simulate connected FMUs. [7. Graphical Modelling — OMSimulator v2.1.1.post191-gf9c815c documentation](https://openmodelica.org/doc/OMSimulator/master/html/OMEdit.html#dual-mass-oscillator-example)

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [September 25, 2023, 4:56pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/17 "2023-09-25T16:56:17Z")

</div>

Done: [Ability to graphically visualize FMUs · Issue #195 · ThummeTo/FMI.jl · GitHub](https://github.com/ThummeTo/FMI.jl/issues/195)

Thanks for linking OMSimulator, @bilderbuchi !

---

<div class="post-metadata">

**Author:** ![bilderbuchi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bilderbuchi/32/13562_2.png) [@bilderbuchi](https://discourse.julialang.org/u/bilderbuchi)\
**Post date:** [September 25, 2023, 4:59pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/18 "2023-09-25T16:59:58Z")

</div>

Also, there is a visualizer/editor for MTK models, but I think this is still in an early phase: [GitHub - bradcarman/ModelingToolkitDesigner.jl: A helper tool for visualizing and editing a ModelingToolkit.jl system connections](https://github.com/bradcarman/ModelingToolkitDesigner.jl)

---

<div class="post-metadata">

**Author:** ![alparg-mv](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@alparg-mv](https://discourse.julialang.org/u/alparg-mv)\
**Post date:** [September 25, 2023, 5:10pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/19 "2023-09-25T17:10:39Z")

</div>

Thanks for your helpful answer. When re-writing the Simulink model, did you use JuliaSim, too, or plain Julia?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 25, 2023, 5:25pm UTC](https://discourse.julialang.org/t/matlab-simulink-model-simulation-and-modeling-with-s-functions-and-variable-step-solver/104220/20 "2023-09-25T17:25:44Z")

</div>

I just write the differential equations in ModelingToolkit without using components like this:

```julia
using ModelingToolkit

# Bessel filter with step signal as input
function model_bessel(step_time, step_size; w1=3.14, y0=0.0, yd0=0.0)
    @variables t u(t)=0 y(t)=y0 yd(t)=yd0
    @parameters w=w1
    
    D = Differential(t)
    eqs = [D(y) ~ yd,
           D(yd) ~ 1/0.618 * (w*w * u - 1.3617w * yd - w*w*y),
           u ~ if_else(t > step_time, step_size, 0.0)]
    
    @named sys = ODESystem(eqs, t)
    
    # sys = structural_simplify(sys)
    sys, u, y
end

```

But you will need to use components if you have more than (10…100?) equations…
