I develop models in Simulink (1), which actually means I create models in one of 4+ languages: Simulink, Stateflow, MATLAB or SimScape. Selecting the “correct” language for the task makes the difference between easy to implementation or complex realizations.
The short answer
Each language has areas that they excel (2) at, in short they are
- Sim(XXX): The SimMechanics, SimFluids,… set of tools is intended for medium / high fidelity physical models (3)
- Stateflow: Modeling of state based events and flow chart logic
- MATLAB: Complex mathematical equations, interfacing with external C code.
- Simulink: Data flow and controls logic
The, longer, real answer
Looking to the right, it will take you a few seconds to translate the diagram to C = sqrt(A^2+B^2), e.g. the Pythagorean theorem, but you were able to do so without undue difficulty. This could very easily be put into a MATLAB block and have the code look exactly as written above.(4) However, at this level it may not be worth doing so due to the transition between modeling languages.
When to switch? (5)
As a rule of thumb I ask the following questions
- What is the total number of blocks / lines of code / states involved in the current language: If the answer is less then 10 blocks, LOC or 3 States then I stick in my current language (assuming it is less in the other language).
- Am I putting it into a subsystem: If I am grouping the portion into a unique subsystem then I will always switch to the “best” language; at that point the “transition jarring” isn’t an issue.
- Am I replicating a built-in function: If the operation you are doing exists as a built in function in a different language then switch now! (6)
There are two driving factors behind modeling language selection; clarity and efficiency. Happily in this case, the clearest representation is almost always the most efficient implementation. (7,8)
- In reality I now advise people on how to develop models; much like I am doing now.
- In the same way that “Excel” is a very good spreadsheet but it should not be used a a programming tool
- We will look at the dividing line between medium/high and high fidelity models in a future post. More often than not this is as far as you need to go for developing your control algorithm.
- If I do need to perform this calculation often I would do that.
- Knowing when to switch is much easier then the wiring diagram for the 3-way switch.
- MATLAB and Simulink have powerful and efficient implementations of many basic algorithmic functions, re-creating them both as a time sync and a potential source of errors.
- In my Pythagorean example the generated code would have been the same in either case
- The reason it is most often the most efficient is because all of the global optimizations built into the selected language, e.g. it is less likely to use unneeded states, variables or operations.