Is it possible to have slight differences when running the same Simulink simulation twice? - simulation

Assume that you have a Simulink simulation. You run it twice and consider a plot of a certain signal in time. Is it possible that there are small differences between the two signals?
A possible cause could be a variable-step solver.

Assuming all simulation parameters are the same between runs, if your simulation is complex, and uses custom blocks, such a thing is possible, if there is a bug in the initialization code.
With S-Functions, for example, you can chose if and how to reset internal state between simulation runs. With C code, it is easy enough to forget to reset something altogether, and end up with some uninitialized variable that contains garbage.
Another possibility is that something gets written into the workspace from the simulation, and is fed back into the simulation when it is initialized the next time.

Related

How to take values generated from a MATLAB program and display them in a Simulink block?

I have a working MATLAB program hooked up to an Arduino and rotary sensor that displays the current angle. I would like to display this angle on a Simulink model, so I can control a motor based off the current angle. Is there any way to do this without creating an angle sensor in Simulink and just using my MATLAB code that already works?
Your question is not really clear and it depends if you are trying to do it before simulation or during simulation.
If you want to do it before the simulation, just create a constant block with a variable name that is taken from the workspace, and let your function to set that variable in the workspace.
If you want to do it during simulation, it is a little more difficult but still possible. Lets create a sample a MVCE. The simulink model mask.slx contains a constant the is set to 0, and this constant value is displayed in the display block on the right. The simulation time is set to inf, so when you play it, you must stop it manually.
It is possible to checge the value of the constant while the simulation is running using this simple Matlab call:
set_param('mask/Value_to_set', 'Value', '10')
you should also consider that the constanta must be a Tunable gain (it is by default).
(as you can see the simulation is running).
There are some additional (and surely better) solution you can use:
Include your Matlab function in a MATLAB User Defined Function block, and call it at each simulation iteration. If you have compilation issue you shall follow the coder.extrinsic way (here an example for fmincon)
Use the Simulink Support Package for Arduino Uno Hardware Add-on that is available in the Add on store.

Run a continuous Simulink model with real-time input?

I'm new to Matlab/Simulink but have a requirement to interface with a Simulink model from a piece of software.
At the moment I have created a simple Simulink function 'Inc' which has 1 input to an addition block with a constant 1 and an output. I have used To/From Workspace blocks and can run the simulation over a fixed time using a time/value vector input, such as simin = [0,0;5,0;5,1;10,1].
What I would like to do is run my model continuously? Reading Workspace values in real-time rather than a pre-defined time based vector?
I can't see how to set this up? Can Matlab/Simulink do this?
To summarise, I would like simin and simout to be single values i.e. simin = 1, then with the model running continuously(infinitely) at the next fixed step simout would update to simout = 2. simin changes would be made at the Workspace at varying intervals.
The short answer is you can't (easily) do that, that's not how Simulink works. The MATLAB workspace is read/accessed at the beginning of the simulation and passed to the Simulink engine, and even if the values in the workspace change before the simulation is finished, this is not taken into account by the Simulink model running.
There are ways to work around this, but if you are a novice to MATLAB & Simulink, be aware that these are fairly advanced techniques, and I would advise to familiarise yourself with Simulink first.
Have a look at these similar questions for suggestions of how to do what you want:
Stream data form MATLAB to Simulink
Problem of variable updating in workspace
How Do I Change a Block Parameter Based on the Output of Another Block?
Tuning block parameters at every time step in a simulation
Simulink Signal Viewing using Event Listeners and a MATLAB UI
Obviously, you need to change the simulation end time to Inf or some large number.

Where does Simulink start execution from?

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.

Plotting in Matlab using Real Time data from Simulink

I'm trying to solve a problem of simulating in real time in Simulink (This is solved) but plotting (real time) in Matlab ?
Details:
I want to be able to run a Simulink simulation (which is running in real time) and be able to turn on / off manual switches while the simulation is happening. This works well when I'm using the built in Scopes in Simulink but now I want to export that data to Matlab in real time as well (To make a custom looking graph).
So is there a way, to export this data (it can be sampled if that is necessary) to Matlab and make a plot that is constantly updating. Meanwhile I can still manipulate the switches in Simulink and influence the simulation manually ?
Simulink is effectively running continuously until I stop it.
Thanks for the help!
There should be some kind of notification going when simulink updates the data to be visualized. Maybe this is the linkdata feature.
Another, worse, solution is the drawnow command to redraw the graphs continously (the latter could be unnescessary costly for you program).

Why would an interpreted MATLAB function block be evaluated twice in Simulink?

I have a Simulink model that includes the following subsystem.
The bm_train_adapter block will call a MATLAB function of the same name, passing all the input arguments in a single vector.
The subsystem has been given a sample time of 900 (secs), which is why all the signals are colored in red (for discrete signals).
However, in the debugger I have observed that the bm_train_adapter function gets called twice at each simulation timestep. This yields horribly wrong results since the function includes side-effects.
Why is Simulink calling my interpreted MATLAB function more than once per timestep? How can I prevent this?
I think this is because of your solver setup. In your Configuration Parameters window, check out the Solver Options pane.
I believe the discrete and ode1 solvers will call once per timestep. ode2 will call twice per timestep, ode4 will call 4 times per timestep, etc.
This behavior is very helpful for simulating continuous dynamics, but it can be confusing when interacting with discrete elements.
The reason was that my model had algebraic loops caused by unit delay blocks in subsystems. To solve these loops, the solver had no choice but to evaluate some blocks more than once.
The solution was to move out all unit delays from their subsystems.