Simulink model in a while loop - matlab

i want to get values for a simulink model. The aim is to implement the simulink model in a while loop in a matlab script
The simulation has to run 10s. The output of the simulink model is a signal value, which shouldnt exceed the value 1.
The while loop should run, till the right values a,b,c and d are find.
If the simulation is wrong, the value should increment. (a is outer loop and d the inner) All possible combinations should be run.
How it can be implemented, is it possible and how to get with possible errors in the simulink model?
How to get the simulink function data live (If it saved to the workspace, it appears, after the simulation (10s) is done)? I would like to get the results during the run in the workspace.
Have someone experience with it?
Thank you in advance!

Related

Sample sequence of points from continuous signal simulink

I have a Matlab function (created by me) that must be evaluated only at a given rate. I would like to sample the value of a signal, give to this function (discrete values) and then, the calculated output must be hold until the next value is available. Is there a way in simulink to do this? All answers I have found use quantizer + ZOH but in this case I still get "a continuum" (or almost it) of points to be evaluated by thsi function which is really slow. Changing the rate of simulink's solver is also not an option as the result of this function will be given for a continuous time system.
Any help will be highly appreciatted!
Thank you
Assuming by Matlab function you mean a MATLAB Function block, then it sounds as if all you need to do is make the block discrete. Do that by right-clicking on the block, going down to Block Properties and then in the resulting dialog enter your required sample time.
The block will then sample its input and generate an output (which is held between sample times) at each sample time.

Count number of nonlinear iterations in FMU

Hello
I'm running an FMU model using FMILibrary directly.
The model has a certain size of non-linear system.
I would like to get the information of how many non-linear iteration were needed at every time step. Something similar what Dymola returns when selecting SimulationSetup-->Debug-->Nonlinear Iterations would be nice.
Any idea how to get to that information?
Thanks

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.

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

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.

Automatically changing block variables and collecting data from a model

I have a relatively basic Simulink block Model in which there is a Gaussian Noise Generator & an Error rate calculator. I want to get data on how changing the "variance" property of the Gaussian noise generator affects the result from the error rate calculator.
The most obvious way to do this is to manually run the simulation, record the results, change the variance of the Gaussian Noise Generator, rerun etc.
However, is there a way of getting the Simulink model to run the model, increase the Gaussian Noise variance by an amount, and rerun automatically for x number of iterations, and then to store the results in a list or array?
I am damn sure there is, but being new to Matlab & Simulink I have no idea of how to go about doing it? Somehow I imagine it would involve setting the "variance" as an input, and collecting the figure from the error rate calculator as an output - but again, I don't really know.
You want to use a for loop, and within the loop use the functions set_param and sim.
Look at the doc for more on how to use those functions.
There are also a couple of examples of using set_param here and using sim here.