plot(x,y,'r+:')will plot y against x, using a red marker "+" and a red dotted line (the dotted line is indicated by the colon; how would you have found that out if I had not put in this parenthetical remark?). If you simply use
plot(x,y,'r+')then it will plot the data points but no line connecting them. Matlab further allows a user access to "handle graphics", the object-oriented substrata for plotting data. Using handle graphics you can change the marker type to be an alphabetic letter, for example, or a Greek symbol. As another example, when you plot multiple curves in a single plot (remember, this is called a set of "axes" by Matlab), the color of the line will by default cycle through a fixed set of colors for each new curve. Handle graphics allows you to reset that, using the ColorOrder property. Using handle graphics goes beyond what is needed in an introductory course. If you do use Matlab professionally in your student career, you want to be aware of the underlying capabilities.
I have not found any occasion to use handle graphics over the past 15 years. Before that, a little.
Plotting variants and helpers that are extremely useful in scientific computing are
Not enough info given above to really figure out how to use the commands? Well, crank up Matlab and turn to your old friend the help command!
x = NaN;
if (x == NaN)
disp('Yup, x is no good')
end
will never display the message.
>> x = linspace(0,4*pi,1000); >> y = tan(x); >> plot(x,y)which yields the plot
This is not helpful. The reason is the tan function has poles (goes to +/- infinity) at pi/2, 3*pi/2, .... So remove entries that are bigger than 10 in absolute value:
>> y = y - (abs(y)>10).*y; >> plot(x,y)giving the plot
However, the plot is still unsatisfactory. It shows the graph crossing the x-axis at pi/2, etc., because Matlab connects consecutive points. Also, it puts values of zero around the asymptotes, which clearly are not valid. The trick is to eliminate those, by replacing those data values with NaN (NotaNumber):
>> y = y + (abs(y)>10)*NaN; >> plot(x,y)which gives the plot:
>> I = find(abs(y)>10); >> y(I) = NaN;The vector I has the indices where y is greater than 10 in absolute value, and the second line sets only those entries to be NaN. One more try at plotting gives:
>> A = eye(4) % Set up the identity matrix of order 4
A =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
>> plot(A)
Then you get the plot
Matlab has plotted four "curves". Each is a column of A plotted against its index number. So the first curve is the broken line through the points (1,1), (2,0), (3,0), and (4,0). You only see the part between x-coordinates 1 and 2, however, because the rest of the line lies along the x-axis. The later curves (from the remaining columns of A) overlay it, and conceal it. Since column 4 gets drawn last, the x-axis appears turquoise in color (actually, "cyan"), the color for curve four.
However, when you do "plot(fft(A))", you nowhere get an x-axis coordinate of 4!
The reason is because fft(A) is a 4x4 matrix with complex values.
>> fft(A) ans = 1.0000 1.0000 1.0000 1.0000 1.0000 0- 1.0000i -1.0000 0+ 1.0000i 1.0000 -1.0000 1.0000 -1.0000 1.0000 0+ 1.0000i -1.0000 0- 1.0000iIn this case, Matlab plots four curves, but now with the x-coordinate for each curve the real part of the column, and the y-coordinate the imaginary part. The first "curve" consists then of the single point (1,0), plotted four times. This is a corollary to the Matlab "gotcha" described earlier: Matlab invisibly slips into complex numbers whenever appropriate, and for plotting them it does what it thinks is right.