Applied Numerical Methods Using MATLAB. Won Y. Yang
Чтение книги онлайн.
Читать онлайн книгу Applied Numerical Methods Using MATLAB - Won Y. Yang страница 15
Figure 1.2 Examples of graphs obtained using the ‘ plot()
’ command. (a) Data not arranged – pointwise, (b) data not arranged – interpolated linearly, and (c) data arranged along the horizontal axis.
>[temp1,I]=sort(temp(:,1)); temp2=temp(I,2); >plot(temp1,temp2)
This will yield the graph like Figure 1.2c, which looks more informative than Figure 1.2b.
We can also use the ‘ plot()
’ command to draw a circle.
>r=1; th=[0:0.01:2]*pi; % [0:0.01:2] makes [0 0.01 0.02 .. 2] >plot(r*cos(th),r*sin(th)) >plot(r*exp(j*th)) % Alternatively,
Note that the ‘ plot()
’ command with a sequence of complex numbers as its first input argument plots the real/imaginary parts along the horizontal/vertical axis.
The ‘ polar()
’ command plots the phase (in radians) and magnitude given as its first and second input arguments, respectively (see Figure 1.3a).
Figure 1.3 Graphs drawn by using various graphic commands.
>polar(th,exp(-th)) % polar plot of a spiral
Several other plotting commands, such as ‘ semilogx()
’ (with a base 10 logarithmic scale for the x‐axis), ‘ semilogy()
’ (with a base 10 logarithmic scale for the y‐axis), ‘ loglog()
’ (with a base 10 logarithmic scale for both the x‐axis and the y‐axis), ‘ stairs()
’ (stairstep graph), ‘ stem()
’ (discrete graph), ‘ bar()
’ /
‘ barh()
’ (vertical/horizontal bar graph), and ‘ hist()
’ (histogram), may be used to draw various graphs (shown in Figure 1.3). Readers may use the ‘ help
’ command to get the detailed usage of each one and try running the following MATLAB script “nm01f03.m” to draw various types of graphs.
%nm01f03.m: plot several types of graph th=[0:0.02:1]*pi; subplot(521), polar(th,exp(-th)) % polar graph subplot(522), semilogx(exp(th)) % with a base 10 logarithmic scale for <i>x</i>-axis subplot(523), semilogy(exp(th)) % with a base 10 logarithmic scale for <i>y</i>-axis subplot(524), loglog(exp(th)) % with a logarithmic scale for <i>x</i>-/<i>y</i>-axis subplot(525), stairs([1 3 2 0]) % stairstep graph subplot(526), stem([1 3 2 0]) % discrete graph subplot(527), bar([2 3; 4 5]) % vertical bar graph subplot(528), barh([2 3; 4 5]) % horizontal bar graph y=[0.3 0.9 1.6 2.7 3 2.4]; subplot(529), hist(y,3) % histogram subplot(5,2,10), hist(y,0.5+[0 1 2])
Moreover, the commands ‘ sprintf()
’, ‘ text()
’, and ‘ gtext()
’ are used for combining supplementary statements with the value(s) of one or more variables to construct a string and printing it at a certain location on the existing graph. For instance, let us run the following statements:
>f=1./[1:10]; plot(f) >n=3; [s,errmsg]=sprintf('f(%1d)=%5.2f',n,f(n)) >text(3,f(3),s) %writes the text string at the point (3,f(3)) >gtext('f(x)=1/x') %writes the input string at point clicked by mouse
The command ‘ ginput()
’ allows you to obtain the coordinates of a point by clicking the mouse button on the existent graph. Let us try the following commands:
>[x,y,butkey]=ginput %get the x,y coordinates & # of the mouse button or ascii code of the key pressed till pressing the ENTER key >[x,y,butkey]=ginput(n) %repeat the same job for up to n points clicked
1.1.5 Three Dimensional (3D) Graphic Output
MATLAB has several three‐dimensional (3D) graphic plotting commands such as ‘ plot3()
’, ‘ mesh()
’, and ‘ contour()
’. ‘ plot3()
’ plots a two‐dimensional (2D) valued‐function of a scalar‐valued variable; ‘ mesh()
’/‘ contour()
’ plots a scalar valued‐function of a 2D variable in a mesh/contour‐like style, respectively.
Readers are recommended to use the ‘ help
’ command for detailed usage of each command. Try running the above MATLAB script “nm01f04.m” to see what figures will appear (Figure 1.4).
%nm01f04.m: to plot 3D graphs t=0:pi/50:6*pi; expt= exp(-0.1*t); xt= expt.*cos(t); yt= expt.*sin(t); % dividing the screen into 2x2 sections clf subplot(521), plot3(xt,yt,t), grid on %helix subplot(522), plot3(xt,yt,t), grid on, view([0 0 1]) subplot(523), plot3(t,xt,yt), grid on, view([1 -3 1]) subplot(524), plot3(t,yt,xt), grid on, view([0 -3 0]) x=-2:.1:2; y=-2:.1:2; [X,Y] = meshgrid(x,y); Z =X.̂2 + Y.̂2; subplot(525), mesh(X,Y,Z), grid on %[azimuth,elevation]=[-37.5,30] subplot(526), mesh(X,Y,Z), view([0,20]), grid on pause, view([30,30]) subplot(527), contour(X,Y,Z) subplot(528), contour(X,Y,Z,[.5,2,4.5])
Figure 1.4 3D graphs drawn by using plot3()
, mesh()
, and contour()
.
1.1.6 Mathematical Functions
Mathematical functions and special reserved constants/variables defined in MATLAB are listed in Table 1.3.
Table 1.3 Functions and variables inside MATLAB.
Function | Remark | Function | Remark |
cos(x)
|
exp(x)
|
Exponential function | |
sin(x)
|
log(x)|
|
Natural logarithm | |
tan(x)
|
log10(x)
|
|