32 CHAPTER 1 AN INTRODUCTION TO M ATLAB
®
FIGURE 1.4
Function plotted over the range from−4to 4. It has a
maximum value of4×10
6
.
FIGURE 1.5
The same function as plotted in Fig.1.4but with a
limit on the range of they-axis.
the rows. This is illustrated by running scripte4s104.mwhich generates six different plots, one in each
of the six cells. These plots are given in Fig.1.6.
% e4s104.m
x = 0.1:.1:5;
subplot(2,3,1), plot(x,x)
title(’plot of x’), xlabel(’x’), ylabel(’y’)
subplot(2,3,2), plot(x,x.^2)
title(’plot of x^2’), xlabel(’x’), ylabel(’y’)
subplot(2,3,3), plot(x,x.^3)
title(’plot of x^3’), xlabel(’x’), ylabel(’y’)
subplot(2,3,4), plot(x,cos(x))
title(’plot of cos(x)’), xlabel(’x’), ylabel(’y’)
subplot(2,3,5), plot(x,cos(2 *x))
title(’plot of cos(2x)’), xlabel(’x’), ylabel(’y’)
subplot(2,3,6), plot(x,cos(3 *x))
title(’plot of cos(3x)’), xlabel(’x’), ylabel(’y’)
The current plot can be held on screen by using the functionholdand subsequent plots are drawn
over it. The function
hold onswitches the hold facility on whilehold offswitches it off. The figure
window can be cleared using the function
clf.
M
ATLABprovides many other plot functions and styles. To illustrate two of these, thepolarand
compassplots, we display the roots ofx
5
−1=0 which have been determined using the MATLAB
functionroots. This function is described in detail in Section3.11. Having determined the five roots
of this equation we plot them using both
polarandcompass. The functionpolarrequires the absolute
values and phase angles of the roots, whereas as the function
compassplots the real parts of the roots
against their imaginary parts.