1
$\begingroup$

I want to define a function $y(t)$ that has 2 cases:

$$y(t)= \begin{cases} -1 & \text{for} & -T \lt t \leq 0 \\ 1 & \text{for} & 0 \lt t\leq T \end{cases} $$ where $T$ is an integer.

I already defined $t$ using linspace between $-T$ to $T$ in a resolution of 1000 points.

I was advised to use the command ones, but I dont know how to apply it by the function terms.

Thank you.

$\endgroup$
  • 2
    $\begingroup$ y=[-ones(500,1);ones(500,1)]; $\endgroup$ – Matt L. Apr 6 '15 at 18:01
  • 1
    $\begingroup$ Thank you, it worked. I just dont understand how does it work, because when I plotted (t,y) it gave me exactly what I wanted, but ones(500,1) gives you a vector of 500 1's, and it has nothing to do with t. Im just trying to understand this better. $\endgroup$ – minimal risk Apr 7 '15 at 5:27
  • 2
    $\begingroup$ You're right, y has nothing to do with t, just the lengths of the two vectors must be the same. y simply gives you the values, and t defines where on the t-axis these values are located. That's the way it works. $\endgroup$ – Matt L. Apr 7 '15 at 6:58
  • 1
    $\begingroup$ Why not to use the sign function and set the value for t=0 to be 1? $\endgroup$ – jojek Jun 6 '15 at 13:34
1
$\begingroup$

Well, this is Octave but should work in Matlab:

function y=Y(t)
  y=ones(size(t));
  y=y+(t<=0)*(-2);

.. and it is vectorised. Calling will look like:

>t=linspace(-T,T,1000);
>y=Y(t);

To make this more general we could also make the time at which the function switches a parameter also:

function y=Y(t,S)
  y=ones(size(t));
  y=y+(t<=S)*(-2);
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.