My simple simulink model runs too fast - matlab

I have designed a simple simulink and stateflow diagrams on Matlab. It works perfectly! But I have two issues:
1) The model runs too fast. The transition time between two states are amazingly fast that I can not see. I tried to change some parameters, but I was not succeed. What should I change (maybe on Model Configuration Parameters?) to make it slower so we can see the transitions?
2) Currently I just have a constant input. How can I set a series of constants, probably periodic? I assume there should be a special component. I need, like an input of say 0 at time 0, then 1 after 1 minute, 2 at time 3, etc ?

1) I had the same issues with some interactive simulations where the CPU would make them run too fast for the user to interact. Using a Simulink Real Time Execution Block brought the solution, despite it gives heavy CPU load due to the solution it uses. With this, if you define a 10sec simulation, that's what it will last. If you don't want to pass through this burden, just lengthen your simulation, or inspect thoroughly the scopes throughout the whole simulation (disabling point limit in the Scope, clicking the Parameters button and then unchecking Data History/Limit data points to last...)
2) Simulink/Sources/Repeating Sequence. You define vectors of times and the value to output in that time. Keep in mind that the block interpolates the values between two points, therefore, if you want to change from 2 to 3 in T=1s, you have to define two points, one U=2 in T=1s and another U=3 in T=1s. For example, 0 in (0..1), 5 in (1..2), 10 in (2..3) and 15 in (3..4):
Time values: [0 1 1 2 2 3 3 4]
Output values: [0 0 5 5 10 10 15 15]

If you just want to check the transitions, you don't need to make it slower. You can try to use the Debug mode available in Stateflow and you can see how your model works step by step.

The option to slow down Stateflow animation (so that, for example, you can better see the transitions between states during simulation) is under the Display menu in the Simulink menu bar.
In the latest release (R2014b), the option is under Display->Stateflow Animation (as well as under Simulation->Stateflow Animation). (See here for more details.)
In older releases, it's under Display->Data Display in Simulation->Chart Animation Delay (sec) (although, I'm not 100% sure when this option was moved in the menus).
Note that you have to be inside of the chart for these options to be enabled in the menu.

Related

I have a process to move a picking agent and a product through the same delay block

Currently, I have two delay blocks above each other, where one agent goes through one of the blocks and the other through the other one.
But when I want exponentially distributed values with a mean of 120 seconds, They both need to have the same value always. So they are done at the same time.
You simply need to have the two delay blocks use their own (but with the same seed) random object.
Start off by creating two identical random objects
And have each of the delay blocks use of them of them
Then the numbers they will sample will be the same for every sampling iteration.
See this post for a similar problem
Why do two flowcharts set up exactly the same end with different results every time the simulation is run even when I use a fixed seed?
Not questioning your probably bad design by not using resources, These are the steps to follow to ensure things happen at the exact same time:
1.Create a variable called seed of type long
2.create a cyclic event that runs every 1 minute and has the following code:
seed=(new Random()).nextLong();
3.In both blocks you will use the following code to calculate the exponential distribution:
exponential(120,0,new Random(seed))

Structuring an experiment with psychtoolbox

I have a design for an experiment that I would like to code in psychtoolbox using MATLAB. I understand the basics of how to use this toolbox however it would be extremely helpful if someone has designed a similar experiment before and could provide me with some code that could help me to carry out the following:
The experimental procedure will be composed of 80 trials divided into 5 blocks with 16 trials in each block. The experiment consists of the participant selecting a number from the screen. There is a desirable number (target number) and an associated less desirable number (lure number). I won't go into further detail about the reasoning behind this experiment as it is not relevant to my question.
I have attached an image that shows 1 block of trials (16 trials). The other 4 blocks are the same as this block.
Target and lure numbers will be presented on the screen to choose from (an example can be seen in image below).
In some of the trials as can be seen from the trials table only one target number and one lure number are presented for the participant to choose from (instead of two targets and two lures).
The lure(s) that appears with each target(s) should not always be the same. I want the lure that is shown with the target to be randomly selected in each trial (as can be seen in the trials image there is more than one possible lure). In the trials image I have attached the trial numbers are presented just for clarity, in each block the presentation of the targets needs to be randomized.
You can use the BalanceTrials function that comes with psychtoolbox. You use all the possible lures and targets as inputs and it returns a random order of all possible combinations. You can also specify a minimum length of the list it returns, but if there are more combinations it will make the list longer to make it balanced. Here is an example:
numberOfTrials = 80;
targetNumbers = {'3','4','5','6','7','4 5','4 6','4 7'};
lureNumbers = {'3','4','5','6','7','4 7'};
[targets, lures] = BalanceTrials(numberOfTrials, 1, targetNumbers, lureNumbers);
You can split this up into 5 blocks or you do it each time for each block.

MATLAB/Simulink figures

I want to create a similar figure with Simulink like:
Figure-1
This is my MATLAB code:
n = importdata('n.txt',';')
cars = n(:,2)
trucks = n(:,3)
bus = n(:,4)
t = linspace(1,365,365)
t = transpose(t)
Here are my Simulink blocks:
Figure-2
And "Scope" block does this kind of figure:
Figure-3
Why is Simulink figure (Figure-3) not similar to Figure-1. I want to create a similar figure with Simulink. Where is the problem?
Note that the t(ime) vector that defines your input data has no effect on the length of time that the simulation runs - it purely defines the shape of your input data.
By default, the Stop Time for a Simulink model is 10 seconds, which is why your second figure only runs out to 10 seconds. Your model is only reading/simulating the first 10 seconds of data. Change the Stop Time (across the top of the model's window) to be either 365, or even better max(t). (In the latter case, if you subsequently change t in the MATLAB Workspace then the simulation stop time will change accordingly too, without you having to manually change anything in the model.)
By default the Scope will show all of the simulated data. But if it doesn't (or you zoom at any time) then you can use the zoom tools (across the top of the Scope) and various of the Scope Properties to change the amount of data you see.

Simulink Block to increase signal with a step at a certain time

I state that I have searched for a long time but I can't find an answer to my problem.
I don't find a Simulink block that has the function of giving in input one step that is worth an x (defined by me) for 5s and 1.1x from 6 to 50s at two models (one linear and one non linear systems)
Usually i use the Step Block to start an initial and final value of the step but the change is almost instantaneous. What block can I use to do this?
Option 1
Use multiple step blocks and add them up. Simple as that.
Option 2
Use the signal builder block.

Any Tic Toc function in Simulink for embedded blocks

I have a system with some embedded Matlab blocks where I'd like to perform some actions after a certain amount of time, in this case turn on lights and switches in an interface to which I send signals from Simulink.
The problem is that I thought I'd use "tic"-"toc" and "while" in a Matlab function block to perform these actions, say one parameter becoming 1 after 5 seconds, the following parameter becoming 1 after 12 seconds and so on, but I noticed that tic-toc apparently doesn't work in Simulink for embedded functions.
Is there any similar functions that could be used in Simulink for embedded functions or is there any other way to do this?
Edit: I've tried to get the clock's time as well, but it's a growing value. Is there any way to "lock" the time as a parameter when the block's function is executed?
You shouldn't be using absolute time in an embedded system, which is at least one of the reasons why tic-toc and clock from MATLAB don't work with Simulink Coder.
You should create your own counter, which you start and stop when you need to.
This is pretty easy to do using a Unit Delay and Summation block.
If you need to be able to enable and/or reset the counter then use the appropriate block from the Additional Discrete library.