Scope displays unexpected puls generator output - matlab

I am having troubles understanding the output of my scope in this simple simulink model:
I am using a fixed step solver (tried with ode3 and ode8).
Pulse type of the puls generator is set to Sample based and I varied the Period and Pulse Width.
First I set the simulation time to 10 and set the puls generator to Period = 10 and Puls width = 5. The output of the scope is as expected:
But when I tried with simulation time 10,000 and the puls generator with Period = 1,000 and Puls width = 500 it seems my scope is wrong:
Why is the first falling edge at 5,500? I used the Autoscale button every time.
Using sim time 100,000 and Period = 10,000 and Puls width = 5,000 I don't even get a single falling edge:
Even with longer simulation time there seems to be a single rising edge at the end of the scope window.
What am I doing wrong? Is the scope not suitable for such long simulation times using fixed step solver? Or is it not "safe" to use the Autoscale button?

All of the plots you show are correct. Simulink is fine with long simulation times. It is "safe" to use the Autoscale button.
By default a scope is set to only display the last 5000 simulation time steps. Since your model is taking a step size of 1s (this is based on using the default step size of the Pulse Generator, which is 1s), in your second plot you are only seeing points from t=5000 to t=10000 (so the first down step in that time period is at 5500), and in your third plot you are only seeing points from t=95000 to t=100000 (which is a period in which the value of the pulse is low/zero).
To see all simulation times, open the Scope block's parameters (by clicking the button with a picture of a cog on it), go to the History tab, and deselect the Limit data points to last: check box.
Then rerun your simulation and press the autoscale button. You'll then see what (I think) you are expecting.

Related

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 - Output 1 every 30 seconds, 0 otherwise

I need a subsystem that needs to output 1 at interval or 30 seconds or slightly over 30 seconds.
Written in matlab code it should work like that
function y = fcn(time,uplinkTimeInterval)
%#codegen
persistent lastTriggerTime
if isempty(lastTriggerTime)
lastTriggerTime = 0;
end
if time>=lastTriggerTime || time == 0
y = 1;
lastTriggerTime = time + uplinkTimeInterval;
else
y = 0;
end
end
where ulplinkTimeInterval is 30 seconds. Of course I tried to use the matlab function block with this code but for some reason it does not work (in debug mode I can see that y takes value 1 as it should but it simply does not ouput the value outside the block), therefore I wanted to use blocks but I do not know how to do it.
Thanks very much for your help
You can make this logic relatively easily with code or blocks. As you requested a solution using blocks, here it is!
Use the clock block to keep track of time, and some constant block to determine the interval (in seconds) at which to give 1 instead of 0.
Use the memory block to delay the clock signal by 1 timestep, so we can compare consecutive steps' values.
Divide the times by the interval, and round down, to give how many intervals have passed.
Finally, compare consecutive "number of intervals passed" using a relational operator. If more intervals have passed on the upper line, then you have just stepped over the interval threshold.
Note: this will return a 0 for every timestep where you have not crossed a new interval, and a 1 at each individual timestep where you have. The accuracy of the output will depend on the step size of your model.
Edit: It may be clearer / easier to just add the memory block after the floor block, so you are only doing the division / rounding once. It would still allow you to do a comparison to the previous time step. That would look like:
Easiest way to do this is with a just a single Pulse Generator block, set to have a "high" of 1 every 30 seconds. That is shown as part of the image below. The signal will be high for whatever the percentage of the period is specified in the block dialog.
If for some reason you really need to use a subsystem then use a Triggered and Enabled Subsystem (See top right of image). Feed the same pulse signal into both the trigger and the enable port, and set the outport inside the subsystem to have Output when disabled to reset, and to have an Initial Output of 0 (See the lower right of the image).
The model below shows how to do this. In this instance the pulse has been set to have a period of 30s with the rising edge happening every 1% of that period (See the top left of the image).
The output signal will be high for one time step every time the input rises (assuming the trigger is set to rising edge.)

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.

Matlab 'Step' response size doesn't change risetime?

In using Matlab's 'Step' command in finding the step response of a system's transfer function, it's possible to change the step size from the default of 1 to something else (eg 1e-2), like so:
stepOpt = stepDataOptions('StepAmplitude', 1e-2);
step(TF_closed_loop, stepOpt);
In this case the TF is a physical system, eg a motor. However, although the resulting step size is indeed different, the time scale doesn't change at all. Eg if it took 100 seconds to reach 1, it still takes 100 seconds to reach 1e-2...and this is not a reasonable result for a physical system that would take less time to go a shorter distance.
Is there another required setting in Matlab to make this accurate?
It's already accurate. By changing the step amplitude you are only multiplying the input by a constant newA/oldA. The response is the same as in the first case, but multiplied by that same constant. But of course, it is going to take the same amount of time to reach a given percentage of the stationary value.

Matlab Simulink Square Wave

I am new to Simulink and I am trying to model an oscillator to control an automation controller.
The question is:
I created a pulse generator that results in a square wave. To design the oscilator I need that 2 others chanels (one is the same signal, while other is the reverse) remain in zero when the input (the square wave) is oscillating. The problem is that I can't make the other 2 signals remain in zero. I tried using the blocks for discrete elements in the library, such as: "Delay", "Unit Delay", and even "Zero Order Hold". Every block just shifted the entire curve, while what I need is to delay the signal when it assumes the "1" value.
Follows some prints:
I have no reputation for all the images so: the subsystem consists of 3 pulse generators, and theres a scope linked to the subsystem
Please Help!!!!
It sounds like you're asking for a signal that rises at some pre-specified delay after the pulse generator rises, but falls at the same time as the pulse. This is shown in the picture below,
If that's correct, then it can be created using an enabled subsystem, where the subsystem contains only a unit delay, as shown in this picture,
Within the subsystem you must also
Set the Enable block to reset its states.
Set the Outport block to reset its value when disabled AND set an initial value of 0.
Specify an appropriate sample rate in the Unit Delay block (this acts as the amount by which the rising signal is delayed)