Triangular and other step functions in matlab
Generally, to plot a function you 1) sample the domain, defining a vector x
; 2) compute the images of those samples, obtaining a vector y
; and 3) use plot(x,y)
or similar.
When your desired function graph is composed of straight lines, to get a perfect graph you should choose the sample points as the "corners" of the graph. For example, for the triangular function you should use points with 1, 0, 1 plus some points further to the left and to the right:
x = [3 1 0 1 3];
y = [ 0 0 1 0 0];
plot(x,y)
axis([4 4 .2 1.2])
If the point has a jump discontinuity you should duplicate the corresponding x in order to get the jump properly. For example, for the step function you could do
x = [3 0 0 3];
y = [ 0 0 1 1];
plot(x,y)
axis([4 4 .2 1.2])
Related videos on Youtube
YYG
Updated on December 02, 2020Comments

YYG almost 3 years
As the title indicates, I need help on how to plot a triangular function in Matlab. e.g.
$ f(x)=\begin{cases} 1x, & x< 0 \\ \\ 0, & \text{otherwise} \end{cases}$
As well as the functions
$ Af(x)=\begin{cases} A & x\geq 0 \\ \\ 0 & x<0 \end{cases}$ and $ f(x)=\begin{cases} 1 & x\geq 0 \\ \\ 0 & x<0 \end{cases}$
I tried and plotted the unit step function
$ f(x)=\begin{cases} 0 & x< 0 \\ \\ 1 & x\geq0 \end{cases}$
as
x=50:50; y=[zeros(1,50) ones(1,51)]; plot(x,y); axis([55 55 0.2 1.3])
Thanks for your interest.