MATLAB: Plot a graph with linspace

linspaceMATLAB

Hi guys,
I want to plot this graph with Linspace,
This is what i got;
t = linspace(0,3,4)
u = [1 1 0 0]
plot(t,u)

Best Answer

You can't get that exact plot with linspace. I've managed to get it by using
t = [0 1 1 2 3];
u = [1 1 0 0 0];
plot(t, u, 'k-')
which gets the plot you want. To make the plot exactly you need to have two u values for t=1, hence the way I defined it. Linspace assigns one value to each position, so you won't get the vertical line you want.
You could make an approximation by using lots of values - something like
t = linspace(0, 3, 1000);
u = (t < = 1);
plot(t, u, 'k-')
I've used 1000 data points, the more you use the closer it will appear to what you want.