"Warning adding a matrix with the empty matrix will give an empty matrix result."
).function y = myTest(t)
// First Part
y(find(t < 0)) = 0;
// Middle Part
y(find(0 <= t & t <= 1)) = 1;
// Middle Part
ti3 = find(1 < t & t <= 3);
y(ti3) = -1*t(ti3) + 2;
// Middle Part
y(find(3 < t & t <= 4)) = -1;
// Middle Part
ti5 = find(4 < t & t <= 6);
y(ti5) = +1*t(ti5) -5;
// Middle Part
y(find(6 < t & t <= 8)) = 1;
// Last Part
y(find(8 < t)) = 0;
endfunction
myTest(1)
t = 0:0.1:10;
plot(t',myTest(t))
(Plot for myTest(t)
using t = 0:0.1:10
.)
(Confusing warning.)
You have to remove the find()
call and use logical indexing (the logical expressions yield boolean vectors with the same size as t and can be used as indexes) :
function y = myTest(t)
// First Part
y(t < 0) = 0;
// Middle Part
y(0 <= t & t <= 1) = 1;
// Middle Part
ti3 = 1 < t & t <= 3;
y(ti3) = -1*t(ti3) + 2;
// Middle Part
y(3 < t & t <= 4) = -1;
// Middle Part
ti5 = 4 < t & t <= 6;
y(ti5) = +1*t(ti5) -5;
// Middle Part
y(6 < t & t <= 8) = 1;
// Last Part
y(8 < t) = 0;
endfunction
myTest(1)
t = 0:0.1:10;
plot(t',myTest(t))
However, after reading your post on StackExchange, if your velocity is always piecewise affine, then you could use interpln
, which can be further derivated (see SO other post) or integrated with ode
:
function out=rhs(t,y)
out = interpln(ty,t);
end
ty=[0 1 3 4 6 8
1 1 -1 1 1 0]
t=linspace(0,8,1000);
h=sqrt(%eps);
d=(interpln(ty,t+h)-interpln(ty,t))/h;
p=ode(0,0,t,rhs)
plot(x,interpln(xy,x),x,d,x,p)
See ode
and interpln
scilab help pages for the meaning of their arguments (if not explicit enough above).