Matlab Simulink Square Wave - matlab

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)

Related

How to store a specific time that a signal value is changed in another parameter in Matlab simulink?

I have two Matlab function blocks that one is producing zero signal for the other. I want to store the exact time that signal changes to one in another parameter in order to use it elsewhere.
How can I do it?
This type of functinality is achieved using a Triggered Subsystem, as per the image below,
The output of the trigger block will take on value of the input (which in this case is the simulation time) every time the trigger signal rises. Look at the parameters of the Trigger block inside the subsystem for other options such as falling edge, or both edge, triggering.

Simulink model error

I've had to create the Simulink Diagram after this picture:
My answer is this one:
I've gave some values to a, b and c (like 3, 4 and 5), but when I try to run it, it gives me the following warning:
Warning: The model 'ex2_2' does not have continuous states, hence Simulink is using the
solver 'VariableStepDiscrete' instead of solver 'ode45'. You can disable this diagnostic by
explicitly specifying a discrete solver in the solver tab of the Configuration Parameters
dialog, or by setting the 'Automatic solver parameter selection' diagnostic to 'none' in the
Diagnostics tab of the Configuration Parameters dialog
Warning: 'ex2_2/Unit Delay' is discrete, yet is inheriting a continuous sample time; consider
replacing unit delay with a memory block. When a unit delay block inherits continuous sample
time, its behavior is the same as the memory block. Unit delay block's time delay will not be
fixed and could change with each time step. This might be unexpected behavior. Normally, a
unit delay block uses discrete sample time. You can disable this diagnostic by setting the
'Discrete used as continuous' diagnostic to 'none' in the Sample Time group on the
Diagnostics pane of the Configuration Parameters dialog box. "
and the output (scope) it's just a step signal...
I don't know where I'm wrong here.
You've built the model correctly, but did not configure it correctly.
When running the model as-is, what will happen is the following:
The Step block is a continuous-time source by default (it's Sample Time setting is 0).
Simulink sees that the Step is connected to the Unit delay block, which is guaranteed to have constant output during minor steps (unlike Memory blocks)
According to the documentation on sample times:
[...] Simulink sets [Fixed-in-Minor step] as either an inherited sample time or as an alteration to a user specification of 0 (continuous). This setting is equivalent to, and therefore converted to, the fastest discrete rate when you use a fixed-step solver.
Continuous sample time degrades to Fixed-in-Minor-Step rather than Discrete-time in the context of Variable-step solvers. Variable step size is used to speed up simulations (larger steps are taken when accuracy allows it), but requires trickery to compute, for example, the exact time at which the step triggers (the "Enable zero-crossing detection" tick box in the Step block's options). So, Simulink's best way forward in this particular situation is to convert the sample time of the Step block into Fixed-in-Minor-Step, which is propagated forward and inherited by all other blocks in the model.
This is usually an awkward type of sample time, which is effectively not continuous-time, but still seen by all blocks as such. Therefore, the inherently discrete-time Unit delay will complain about being driven by a continuous-time signal (your 2nd warning), and Simulink itself will complain about the complete lack of "real" continuous-time blocks anywhere, while being instructed to use a continuous-time solver, ode45 (your 1st warning).
As usual in software development: it's always best to be explicit, about everything. In this case, the simplest way out of this is to explicitly specify a discrete-time, fixed-step solver in the model configuration dialog. That way, the continuous-time from the step will be automatically converted into discrete-time for the Unit delay.
The block diagram you want to implement is a discrete-time system. Since there are no Ordinary Differential Equations (ODE) but only their discrete counterpart (Finite-differences equations), you do not need an ODE solver as the warning 1 points out.
In discrete-time systems you have to specify your sample time in order to approach to a continuous-time representation where every single instant t is built up by the discrete integer n and its progressions (n+1, n+2, and so on) as t = nT where T is your sample time; Simulink basically expects that you respect this kind of logic, then asks for a sample time. By default Simulink sets it variable which may lead the result "out of track". This is what the warning 2 points out.
Solution: In the 'Model Configuration Parameters' menu, at the Solver tab, set 'Fixed-Step' as Type. The pane will change with a field related to the sample-time (Fixed-step size); set then a Sample time in seconds different than "auto" (e.g. 0.01). This will solve the warning 2. Still in the Solver menu, set also 'Discrete (no continuous states)' instead of any other useless solver. This will solve the warning 1.

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 :)

Illegal rate transition while trying to normalize signal in simulink

I have a signal in simulink which I want to normalize so that the highest value of the signal is always 1. So I use a MaxRunningResettable-Block to remember the highest value that passed so far. I then divide the signal by that value.
A little test with a signal generation block, the running resettable block, the divide block and a scope runs just fine. But when I add this normalizing function to my simulink model I get an error:
"Model initialization failed - Illegal rate transition found involving Unit Delay"
I don't even need to connect the little test case to my other model. Simply by putting it into my model I get this error. Strangely the RunningResettable-block turns yellow when I copy it - indicating that it has a different sampling rate I suppose. I don't get why this happens. I already tried to add a zero-order-hold-block behind the RunningResettable but that didn't help.
As suggest I tried to add another constant block to the R-input of the RunningResettable-Block. I tried several sampling frequencies for that Block (-1, 0, 1/fAb) but that didn't help.
Ok, finally I think that I found my failure. It seems, that the SignalGenerator outputs a continous signal (black). Because in my model there are mostly discrete signals this somehow causes errors. So when I simply add an zero-order-hold block after the signal generator everything seems to work just fine. The sampling frequency of the zero-order-hold has to be adjusted to the rest of the system.

Simulink and Monoflops

I'm using Matlab 7 and have a problem in creating a monoflop which shall raise for a specific time to "1" and after that time fall to "0". How can I do this with Matlab/Simulink 7?
I don't have any other version, so I can't use the "Monostable" Block from newer versions.
Any ideas?
greets, poeschlorn
There are a couple of ways to do this, depending on whether or not you want the pulse (i.e. "monoflop") to occur at a predetermined time or in response to another signal (like a rising edge)...
Creating a pulse at a predetermined time:
If you want to create a single pulse that steps from 0 to 1 at time tOnset, then steps back to 0 after a time tDur has elapsed, you can do this using a Step block, a Transport Delay block, and a Sum block. Here is what the layout would look like:
You would set the Step time of the Step block to tOnset, the Time delay of the Transport Delay block to tDur, and then subtract the delayed signal from the original signal.
Creating a pulse in response to a rising edge:
This is will be a bit more complicated. It will require two Detect Increase blocks, a Relay block, a Transport Delay block, a Gain block, and a Sum block. Here's what the layout would look like:
Assuming your input signal is either a 1 or a 0, the first Detect Increase block will output a 1 when the input steps from 0 to 1. By setting the Switch on point to 0.5 and the Switch off point to -0.5 for the Relay block, this will create hysteresis in the Relay such that the output will persist in the "on" state (i.e. an output of 1) after the brief pulse that occurs when the rising edge is detected.
To get the Relay block to switch back into the "off" state (i.e. an output of 0) after a specified time tDur, you would set the Time delay of the Transport Delay block to tDur. The Detect Increase block in the feedback loop will output a 1 when the delayed signal steps from 0 to 1, and this output will then be scaled by setting the Gain of the Gain block to 2.
When subtracted from the input signal, this gain will ensure that the output from the Sum block can be pulled below -0.5 no matter what the positive input is (0 or 1), thus ensuring that the Switch off point of the Relay block is reached and its output is turned off when the delayed signal has a rising edge (i.e. after tDur has elapsed). One result of this is that any additional rising edges occurring in the model input after the first rising edge and during the time tDur will be ignored. Once the output from the model returns to 0, the next rising edge on the model input will trigger another pulse.