As the title indicates, I need help on how to plot a triangular function in Matlab. e.g.
$ f(x)=\begin{cases} 1-|x|, & |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.
Generally, to plot a function you 1) sample the domain, defining a vector
x; 2) compute the images of those samples, obtaining a vectory; and 3) useplot(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:
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