I've been modeling different possibilities of obtaining a decision in form of an Enum to compare them, expecting library functions of Simulink performing better than implementing the same functionality calling Matlab functions.
According to this Matlab article interpreted Matlab functions usually perform worse than their library counterparts due to passing through multiple software layers.
For my tests I modeled two systems calculating an output value based on an input signal and ran them for the same number of time steps with a Profiler attached.
The implementation using Matlab function took 16s to execute, while the implementation using library blocks took 566s.
Why does the simulink model using the if-else block run significantly slower? Or is there something wrong with my profiling approach?
I'd be grateful for any insights.
Related
I've read that I can generate code from Simulink models/block diagrams. I am curious whether Simulink always converts a model to (c/c++/java) code prior to running a simulation in the Simulink software, and then execute that code? I mean, whenever I'm running a simulation is Matlab converting the block diagram to (c/c++/java) code and running that code behind the scene. In this case, simulation in Simulink directly depends on running some code; this information is important to me in some way.
Generating and running code for a complete model seems plausible, as we can write s-functions using C/Matlab code and use them as custom blocks. So simulating a model involves running code in some degree. Again, since we can write Matlab code as well, simulation may involve interpreting Matlab code in some environment. It makes me curious whether these information are available - how tightly running a simulation in Simulink depends on executing native code in user's machine.
I did some search before posting and found this SO question: How does simulation engine work? Discussion in this question does not answer my question directly.
The answer depens on which mode you chose.
In the normal mode Simulink will run the model as it is primarily using the MATLAB execution engine. No code is generated. Native implemented parts (e.g. S-Functions) are used as individual binaries called by the MATLAB interpreter.
In the accelerator mode Simulink generates model code. This means your full model (except parts where code generation is impossible) is generated and compiled into one binary.
In the rapid accelerator mode not only your model but also the solver is generated and compiled into one binary, now running in a separate process.
For more details refer to the official documentation
I believe that it is possible to transfer MATLAB codes to Simulink. When we program in MATLAB, I know that it will execute from top to bottom line by line. On the other hand, Simulink deals with blocks that are connected to each others. There might be feedback signals. There might be subsystems whose outputs are inputs to other blocks and so on...Suppose we have 3 subsystem blocks connected to each other and the last block's output is fed into the first, which block does Simulink start with?
My question might be a foolish one especially after this long of playing with MATLAB and Simulink but I've not come to know the answer for this yet!
This depends on the actual simulation model. Before the simulation starts, Simulink analyzes the model (which blocks are connected in which direction, are there algebraic loops, etc.). The result is the so-called sorted order of the blocks, which is then used to actually execute the blocks' code.
See the documentation for details.
Is there any way I could program the Matlab/Simulink to be able to automatically generate circuits on its own? I am using PLECS blockset (Piece-wise Linear Electrical Circuit Simulation ) embedded in Simulink.
For example, I need to have hundreds of identical block in a single .mdl file, instead of inserting one by one by myself by calling the block which I previously saved in Simulink library, is it possible that Simulink can be programmed to automatically generate hundreds of blocks by itself?
The only way I was told is by "using vectorization for most components. Most components are vectorized if they have a vectorized input signal or if one of their parameter is specified as a vector." However, I could not find any further information/details, appreciate if anyone of you could give opinion on this?
I just want to know if this is possible? Else, I would have to try another approach?
Thanks!
edited on 10 July 2013: Further to my question, I have confirmed with Plexim that there isn't such features ( add_block and add_line) in Plecs (Piece-wise Linear Electrical Circuit Simulation), does anyone know if there is any way I could automate the Plecs model? Appreciate any suggestion...Thanks
You can probably use functions like add_block and add_line to automate the creation of your Simulink model from a library.
I have a large simulink model with many source and sink blocks, many with only elementary arithmetic operations in between. I have been asked to document the equations behind the model. I am currently doing this manually and I am finding it rather slow and there is a relatively high chance of errors in the process.
Is there any way for Simulink to generate the equations (in MATLAB syntax for example) automatically?
There is no utility in MATLAB/Simulink that can do exactly what you are looking for (and I personally don't know of any third-party tools that can do this, either).
However, I think that your best bet might be to make use of Simulink Coder. This will allow you to convert your Simulink model to C code. From that code, you may be able to extract the equivalent equations more easily than you can by analyzing the Simulink model by hand.
The catch, though, is that Simulink Coder is an add-on package to base Simulink, so you may or may not have this tool available to you.
Simulink has a module called "Matlab Function," which allows you to create a custom function in a Simulink flow diagram.
I implemented a simple function in a Simulink Matlab Function module. My function contains a call to Matlab's built-in rms(). When I run the Simulink model, I get the following error:
The function 'rms' not supported for standalone code generation
If I remove rms from my Matlab Function in the Simulink model, the error goes away and the model runs flawlessly.
Questions:
Is there a way to use Matlab's rms in Simulink?
Are there many other native Matlab calls that can't be used inside Simulink?
I just wanted to clarify and expand upon some points made in learnvst's answer.
Even if you are simply trying to simulate a model containing a MATLAB Function block and are not explicitly attempting to perform code generation, you will still get the not supported for standalone code generation error.
As learnvst indicated, there are multiple restrictions on functions that can be used with code generation. However, if you just want to simulate your model, Simulink allows you to do this if you signify these "black-listed" functions as being extrinsic. This lets Simulink know that the functions will be used for simulation purposes only and won't be part of code generation.
In your particular case, add the following line of code somewhere before your call to rms:
coder.extrinsic('rms');
Declaring a function as extrinsic in a MATLAB Function is often useful even when you are performing code generation. For example, you may want to visualize your data using the plot command during simulation, but obviously would not need the plot command to be part of generated code.
Refer to this doc for more info on declaring functions to be extrinsic.
The not supported for standalone code generation part of the error suggests to me that you are trying to use a product like Matlab Coder to make an executable or native code. If this is the case, there are many naive calls that cannot be used directly in both core Matlab and the toolboxes. The coder products only support a subset of the language. More information can be found here . . .
http://www.mathworks.co.uk/products/matlab-coder/description2.html
As for your call to rms, it is only calculating the root of the mean of the squares. Try creating an alternative using something like . . .
sqrt(mean(x.^2))
...where x is the signal.