Applied Numerical Methods Using MATLAB. Won Y. Yang

Applied Numerical Methods Using MATLAB - Won Y. Yang


Скачать книгу
k: Black

      Sometimes we need to see the inter‐relationship between two variables. Suppose we want to plot the lowest/highest temperature, respectively, along the horizontal/vertical axis in order to grasp the relationship between them. Let us try using the following command:

       >plot(temp(:,1),temp(:,2),'kx') % temp(:,2) vs. temp(:,1) in black 'x'

Image described by caption and surrounding text.

       >[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.

       >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.

Image described by caption and surrounding text.

       >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

       >[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

      %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])

Image described by caption and surrounding text.


Скачать книгу
Librs.Net
Function Remark Function Remark
cos(x)