title: How to mimic MATLAB/Simulink relay behavior?
body: <p>I am trying to mimic the behavior of MATLAB's Simulink <a href="http://www.mathworks.nl/help/simulink/slref/relay.html" rel="nofollow noreferrer">relay</a> block with just MATLAB code.</p>
  <p>My code is as follows (not familiar with persistent variable? <a href="http://www.mathworks.nl/help/coder/examples/working-with-persistent-variables.html" rel="nofollow noreferrer">click</a>) :</p>
  <pre><code>function out = fcn(u,delta)
  persistent y;
  
  if isempty(y)
     y = 0;
  end
  
  if u &gt;= delta
      y = 1;
  elseif u &lt;= -delta
      y = 0;
  end
  
  out = y;
  </code></pre>
  <p>When I look to the output and compare with the real relay block I see :</p>
  <p><img src="https://i.stack.imgur.com/Zw8PP.png" alt="enter image description here" /></p>
  <p>Where does the difference come from?</p>
  <p>Both blocks insert the same sample time, does the relay block have something extra to show the discontinuity?</p>
  <p><a href="https://dl.dropboxusercontent.com/u/20782274/relay.mdl" rel="nofollow noreferrer">Simulink block diagram download</a></p>