The simple way to generate a triangle pulse is to use the triang command in the signal processing toolbox. Also, below is a code that performs the same function as triang, with a different (hopefully clearer) algorithm.
Good Luck
J. Vorwald
function test
% Initalize time and y vector (y set to zero)
dt = 1;
t = 0:dt:20;
y = t*0;
% Get a the triangle shape using the width of the base
% triSize = 6;
triSize = 5;
triShape=triang(triSize);
% insert the base at times 1.0, 10.0, and 15.0
y = insertShape(t, y, triShape, 1.0);
y = insertShape(t, y, triShape, 10.0);
y = insertShape(t, y, triShape, 15.0);
plot(t,y);
% Set the maximum y axis to one
a = axis;
a(4) = 1;
axis(a);
function yout = insertShape(t, y, triShape, tstart);
% Insert the shape (triShape) at time tstart
triSize = size(triShape,2);
istart = find( t == tstart );
ichange = [1:triSize] + istart -1;
yout = y;
yout( ichange ) = triShape;
function triangle = triang(nPoints)
%TRIANG Triangular window.
% triangle = TRIANG(nPoints) returns the N-point triangular window.
% For a triangle, the ramp up and ramp down are the symmetric
% For a triangle
% a) The number of points going up/down is (nPoints-1)/2
% b) The peak ramp value is (nRamp - 1) / nRamp;
% For a triangle with an odd number of points
% c) The triangle reaches the maximum value at the mid point, (Npoints-1)/2 + 1
% use a flag to indicate even/odd (0 - even, 1 - odd)
flagEvenOdd = rem(nPoints,2);
nRamp = (nPoints - flagEvenOdd) / 2;
ramp = [0:nRamp-1] / nRamp;
rampUp = ramp;
rampDown = rampUp(nRamp:-1:1);
if (flagEvenOdd == 1)
rampUp = [rampUp 1];
end
triangle = [ rampUp rampDown ];