Unit delay's magnitude - matlab

What is the magnitude of delay in sec offered by unit delay (simulink block) for following condition?
Configuration parameters are variable step with ode45 solver.
Max step size is 1/60 and min step size is auto.
In delay block sample time is kept as 0.5s.
Input processing is set as 'Elements as channels (sample based)'.
Thanks

I hope you are using discrete blocks as you have mentioned "Unit Delay". In that case, the delay caused is your sample time. ie here it is 0.5s.
Usually Matlab will go for a discreet solver unless you have explicitly mentioned 'ode45' solver. Also, stepsize of solver wont affect the delay

Simply your sampling time. But If you have some informations that we do not know I will give you a way to measure it.
In you model add 2 Scopes. Assign one of these Scopes' input as output of the Delay Block. Other Scope's input will be input of the Delay Block.
Run the simulation and open the graph results of Scopes. There you go. You can see how much delay your Delay Block caused.

Related

Simulink: Get rid of time delay

I am trying to run a closed loop system on simulink as shown below.
When I run it it get this result.
As you can see, there is a slight time delay in getting the step function up to 1. The closed loop output also doesn't start until around 1.5 sec. I understand that this is what would occur in the real world, but I was wondering if there was a way to get rid of this time delay and make the output show 'ideal' results.
Thanks
Edit
Just thought i'd add a bit more info. The step input is a standard step input and the only things I have changed in terms of settings is the simulation time is 8 seconds, and the solver is a fixed-step ode1 (euler).
You are using a fixed step solver, so the step size defaults to (stop_time-start_time)/50, which in your case equals 0.16. Hence you do not have a time step at exactly 1s. At the 6th time step = 0.96, the step is 0. At the 7th time step = 1.12, the step is 1. That is exactly what is being shown, and correct for the simulation parameters you are using.
With a fixed step solver, if you want the step to occur at exactly 1s then you need to specify a step size so that the model takes a time step at 1s.
You do that by going to the Solver panel of the Simulation Parameters pull down menu and change the Step Size to something appropriate. (Note that the plot will still show the step starting at 1, but finishing one time step later.)
Alternatively you can use a variable step solver.
(This would display the step as being exactly vertical at 1s.)
Regarding the time delay, you have a 3 more poles than zeros so will have a 3 step time delay when using fixed-step Euler.
The only way to change that is to use a different solver.

Is it possible to stop a Simulink simulation when a certain condition is met?

Assume that you have a Simulink simulation where a certain signal is first positive and after some time t in a given interval, it becomes negative. Your goal is to find the zero-crossing.
A first approach would be to plot the signal over the given interval, save it and calculate the zero-crossing.
When repeating this simulation numerous times for varying parameters, it would be beneficial to be able to stop the simulation after the signal has become negative. Then there is already enough information for the calculation of the zero-crossing in Matlab. How could you do that?
Yes, use the Stop Simulation block with the appropriate logical input to the block:
You can use an if / else block to control the flow in the Simulink model. In the if / else block, you can choose the condition if u > 0, continue as normal if it's true, and use the else-option to bypass the rest of the flow you would otherwise run. For instance jump directly to the scope.
Another ways:
You can use Hit Crossing Block in Simulink to find time at the moment of hitting zero.
Another way - use any Trigger or Resettable system. It detects the zero crossing too. For example: this question at SO.
Of course you can also use User Defined function to detect zero crossing by your hand and do whatever you want with the signal at the same time.
About making a lot of simulations and then stops:
you can use Check Upper Static Bound for automatically stops simulation at the moment when zero will be crossed in nth time. For example:
I set upper bound = 10 for this block and this stops at 10th crossing.
There are a lot of ways to save function values in this points or just array of times but this is an another question :)

Variable transport delay using time after threshold has been reached in Simulink

In Simulink how can I delay the transport block to the exact time of when a certain threshold has been reached?
Basically I have an S-function which switches to an alternate input source, a sine wave, after a certain value has been reached. As soon as the switch occurs I want to start the sine wave. For that I need to delay the wave generation until the switch occurs.
How can I send that time to the Variable transport delay block?
Maybe you can try this way:
you need Variable Integer Delay for example. It changes delay to what you need. Calculate needed value of delay you can in User Defined Function, route to it your threshold value and current time and all other values you need.
Hope i understood your question correctly!

How to change the sampling time for all of the model blocks in Simulink?

I have a model and I need to change the sample time of each block that I currently have in my Simulink model.
The problem is that I have so many blocks that make changing this parameter for each individual one cumbersome. Is there a means to change it for a group of blocks?
One more thing, what is the default sample time indicated by "-1"?
This can be done quite easily. In general it is a good practise to be aware of the simulation time, simulation steps and solver you are using in simulink simulations, as sometimes the simulation can go wrong just because of the solver, or because of the simulation step size.
To change all this parameters (and the step size, what I assume is your "sampling time")
you need to go to the Solve Pane that looks like this:
You can see in there how "Max step size" and "min step size" are there, set to auto. This two exist because some odes (as ode45 in this case) use variable step size, but if you want fixed step size you can change the solver to ode1 or ode3 for example.
About that -1 thing... You should not change each blocks sample rate unless you really meant to. When do yo want to do this? In general when you want the sample rate of THAT specific block to be smaller than the rest. So if you have a simulation that is running the whole system at 1e-2 sample rate, and you have an specific block thatneeds to run just every second, then you change the sample rate. Else the default is -1, which means the same sample rate that you have set up in the Solve Pane.
So:
ALWAYS be aware of whats going on in the Solve Pane
Dont change those "-1" unless you really meant to

Matlab Simulink: How to specify a definite solver step size for every iteration?

I want to set a variable step size for every solver step by using the command in the S-function like:
dT= ... % calculate the dT from the inputs of Block and the parameters of S-function
set_param(gcs,'...',num2str(dT));
However, the Matlab does not provide us with a assignable parameter like 'Step' for specifing the solver step size by using the command "set_param()" above. The callable and assignalbe parameters for the solver step size are only 'MaxStep' and 'MinStep'. Therefore, the following two commands are acceptable and executable in Simulink:
set_param(gcs,'MaxStep',num2str(dT1));
set_param(gcs,'MinStep',num2str(dT2));
Thus, I was trying to assign a same value to dT1 and dT2 in order to get the a certain step size, but there was immediately a error report indicating that the max. step and min. step cannot be the same.
So my question is how to specify a step size to the solver in the script of S-function?
The solver settings (used by variable step and fixed-step blocks) are set on initialization and can't be changed using the simulation.
And I assume the fixed-step solver suggestion in the comments won't work for you as you seem to indicate that you want to change the step size during the simulation.
Generically there is no real mechanism for you to have that sort of control over defining (on a step by step basis) the step size that Simulink takes during the simulation.
Nominally that's what the Simulink solver does for you automatically based on the settings during initialization.
You can do it on a block by block basis if all the blocks are S-functions and have a variable step size.
And you could do it by running the simulation over a single time step, saving the SimState, determining the next sample time, running for one time step, saving the SimState, etc., but that would be very inefficient.