I'm trying to create the function :
- x(t) = -1 if t<0
- x(t) = 1 if t>0
So I did this :
function y = x(t);
if t == 0
y = 0;
elseif t < 0
y = -1;
else
y = 1;
end
endfunction
t = linspace(0,100,1000);
plot(t,x(t));
but it doesn't work.
It does not work, because if t is a vector (has more than one element), e.g. t=[-1 0 1] then t==0 yields a vector result: [F T F]. Thus you always got the else solution: y=1. To make it work, you can:
1.) use a for loop and check every element of the t vector individually:
2.) use the builtin sign() function, which does exactly the same, and work not only for vectors but also for matrices: