MATLAB: Plotting tan(x) – basic student’s question

MATLABplot

I am new to matlab and have been trying to plot basic things.
I have sucessfully managed to plot sin(x) by doing the following:
x = -2*pi:pi/100:2*pi; y = sin(x); plot(x,y)
However, when I replace the sin(x) by tan(x), the plot does not come out right. I can't figure out why this is. On the other hand I have managed to plot tan(x) using ezplot…

Best Answer

Will, tan(x) has singularities (function goes to infinity) at pi/2 +/- n*pi. So when you plot over the range from -2*pi to +2*pi the data points just before and right after such a singularity will also be connected (by the default line), unless you tell MATLAB to ploint data points only (markers, essentially). For example,
plot(x,y,'*')
Plus, you would also want to put a limit on your y-axis, something like
ylim([-10 10])