1
votes

I am trying to mimic the behavoir of the Matlab's Simulink relay block with just Matlab code. My code is as follows (not familiar with persistent variable? click):

function out = fcn(u,delta)
persistent y;

if isempty(y)
   y = 0;
end

if u >= delta
    y = 1;
elseif u <= -delta
    y = 0;
end

out = y;

When I look to the output I and compare with the real relay block I see: enter image description here

Where does the difference come from? Both blocks inhert the same sample time? Or does the relay block have something extra to show the discontinuity?

Simulink blockdiagram download

1
how much is the sample time for the relay you are programming? I assume you see kind of response to the commands.NKN
The MATLAB function block inherts the sample time of the solver.WG-

1 Answers

0
votes

I'm not quite sure about this explanation, maybe somebody can support it.

The MATLAB function Block does not support Zero-Crossing Detection, the Relay Block does. That means the latter knows in advance, when your sine will reach the threshold delta and sets the output accordingly to the correct time. The MATLAB function Block needs 2 or more steps to detect the slope (respectively the crossing of the threshold). So from one step to another it realizes that the condition for the new output was set and updates the output and you get a ramp, not a step.

C/C++ S-Functions do have Zero-Crossing Detection - though it seems quite complicated.