Writing equations in MATLAB function vs using Simulink Blocks
14 views (last 30 days)
What are the pros and cons of writing complex mathematical equations in embedded MATLAB function in Simulink vs creating the same equation using the Simulink blocks? Writing in MATLAB function block makes it easier to read and also to edit in future but am I losing performance if I do not use the Simulink math blocks?
Prasanth Sunkara on 12 Feb 2019
As you mentioned the equations were slightly "complex", I would presonally prefer using the MATLAB Fcn. With that said there wont be any major significant performance issues using MATLAB Fcn block. In fact MATLAB Fcn block supports generating efficient C/C++ code for your custom algorithms.
You may try implementing the same using basic Simuink blocks with the above mentioned benefits, but I am afraid you may run into some real implementation/debugging issues when you scale up your mathematical equations.
Raj on 12 Feb 2019
I am not sure based on what you said "Writing in MATLAB function block makes it easier to read and also to edit in future". The prime reason for wide usage and popularity of Model based design using Simulink is that it makes algorithms easier to read, debug, modify and maintain commonality. So using simulink blocks definitely has an edge over writing equations manually. However, in my applications, I have found that many Simulink blocks like switch, if-else etc create problems during autocode generation. Simple blocks like "divide" are not robust to handle situations like "divide by zero" condition and can cause havoc in safety critical applications. Using your own mathematical equation gives you a lot of flexibility to add tolerances and robustness.
So all in all both ways have their own plus and minus. I would recommend you to pick a method based on your application and judgement.
Sulaymon Eshkabilov on 4 May 2019
One of the most common practice that I take very often to reduce the number of blocks in my Simulink models instead of [MATLAB Fcn] is [MATLAB Interpreted Fcn] (with [Mux] block for signal re-routing) that is much faster than [MATLAB Fcn]. Moreover, the used expressions via this block are clear and easier to edit rather than many substitute blocks. in fact, sometimes if the expression gets large, a pen and paper will be needed though ... :).