Additional Plot Types and Plot Formatting The xy plot is the most commonly used plot type in MAT- LAB Engineers frequently plot either a measured or calculated dependent variable, say y, versus an independent variable, say x MATLAB has huge array of graphics functions and there are many variations and options associated with plot() To find out more type:» help plot Special Forms of Plot Linear axes for both x and y are not always the most useful Depending upon the functional form we are plotting we may wish to use a logarithmic scale (base 10) for x or y, or both axes When a log axes is used the variable may range over many orders of magnitude, yet still show detail at the smaller values Table 2.4: Common variations of the plot command Command plot(x,y) semilogx(x,y) semilogy(x,y) loglog(x,y) Explanation Linear plot of y versus x Linear y versus log of x Log of y versus linear of x Log of y versus log of x Chapter 2: Additional Plot Types and Plot Formatting 2 19
Example: Plotting y = x 2 for 0 x 20» x = 0:.5:20;» y = x.^2;» subplot(2,2,1) % address plot in row 1/column 1» plot(x,y),grid,title('y = x^2 as linear/linear')» subplot(2,2,2) % address plot in row 1/column 21» semilogx(x,y),grid,title('y = x^2 as linear/log')» subplot(2,2,3) % address plot in row 2/column 1» semilogy(x,y),grid,title('y = x^2 as log/linear')» subplot(2,2,4) % address plot in row 2/column 2» loglog(x,y),grid,title('y = x^2 as log/log') Chapter 2: Additional Plot Types and Plot Formatting 2 20
Multiple (Overlay) Plots Using the basic command plot() we can easily plot multiple curves on the same graph The most direct way is to simply supply plot() with additional arguments, e.g., plot(x1,y1,x2,y2,...) Another way is to use the hold command plot(x1,y1) hold %turns plot holding on for the current %figure plot(x2,y2) hold %turns plot holding off for current figure Example: Plotting a family of polynomials, say for n = 1234,,, y = x n, 1 x 1» x = -1:.01:1;» plot(x,x),axis('square'),grid» hold Current plot held» plot(x,x.^2)» plot(x,x.^3)» plot(x,x.^4)» title('plots of y = x^n for n = 1, 2, 3, & 4','fontsize',16)» ylabel('y','fontsize',14), xlabel('x','fontsize',14) Chapter 2: Multiple (Overlay) Plots 2 21
Line and Mark Styles A wide variety of line and mark styles can be obtained by including a character string argument to plot() plot(x,y,s) where s is a character string ( a string ) composed of at most one element from each column of Table 2.5 The basic variations are line color, line style, and mark styles Chapter 2: Multiple (Overlay) Plots 2 22
Table 2.5: Graphics line style, line color, and mark options Line Color Marker (symbol) Style Line Style y yellow. point - solid m magenta o circle : dotted c cyan x x-mark -. dashdot r red + plus -- dashed g green b blue w white k black As an example, * star s square d diamond v triangle (down) ^ triangle (up) < triangle (left) > triangle (right) p pentagram h hexagram plot(x,y,'c+:') plots a cyan dotted line with a plus at each data point, while plot(x,y,'bd') plots blue diamond at each data point but does not draw any line Chapter 2: Multiple (Overlay) Plots 2 23
Example: Plotting a family of polynomials (continued). Here we will improve upon a previous multiple plot by coding the line types using markers (symbols) and MATLAB s legend command.» x = -1:.01:1;» plot(x,x),axis('square'),grid» hold Current plot held» plot(x,x.^2,'--')» plot(x,x.^3,':')» plot(x,x.^4,'-.')» legend('y = x','y = x^2','y = x^3','y = x^4')» title('plots of y = x^n for n = 1, 2, 3, & 4','fontsize',16)» ylabel('y','fontsize',14)» xlabel('x','fontsize',14) Chapter 2: Multiple (Overlay) Plots 2 24
Axis Scaling When creating plots in MATLAB the axes are initially automatically scaled Sometimes custom axis scaling is more desirable The axis command, which has a large variety of options (type help axis), allows the axis scaling to be changed following graph generation via axis([x_min,x_max,y_min,y_max]); Another useful command in this area is zoom command Chapter 2: Multiple (Overlay) Plots 2 25
The zoom command allows the user to zoom into an area of an existing graph by drawing a zoom rectangle with the mouse To turn zoom off for a particular figure window you type zoom again Subplots The subplot command allows a given figure window in MATLAB to be broken into a rectangular array of plots, each addressed with the standard plot commands The command subplot(m,n,p) or subplot(mnp) splits the graph (figure) window into an array of plots m-rows by n-columns, and addresses the pth subplot The subplot index p counts from left to right beginning with the upper left plot and ending with the lower right plot A 2x2 Array of Subplots subplot(221) subplot(223) subplot(222) subplot(224) The example on page 2-20 uses the subplot command Chapter 2: Multiple (Overlay) Plots 2 26
Problem Solving Applied The text has an excellent problem solving example that produces user defined plots of velocity and acceleration for an aircraft powered by a advanced turboprop engine called the unducted fan To provide an alternative example in the lecture notes we will solve text problem 9 on page 67 (you have been asked to solve problem 8) Problem Statement Generate a table with the conversions from Celsius to Rankin. Allow the user to enter the starting temperature and increment between table lines. Print 25 lines in the table. The conversions we need to solve the problem are: T F = T R 459.67 9 T F = --T 5 c + 32 Chapter 2: Problem Solving Applied 2 27
Input/Output Description Starting Temp. in Celsius Temp. Increment in Celsius A 25 line table of Celsius temps. co-listed with Rankin temps. Hand Example Consider 10 C From the two conversion equations given above Algorithm Development T R 459.67 9 --T 5 C + 32 We need to write a MATLAB.m file that will request the starting temperature and step size as inputs; we will use the input function for this To display the calculated values we will use disp = 9 T R = --T 5 C + 32 + 459.67 9 T R = -- 10 + 491.67 = 509.67 5 Chapter 2: Problem Solving Applied 2 28
%*********************cel2rank.m********************** % A MATLAB script file for generating a table of values % representing generator conversion from Celsius to Rankin. % Version 1.0 by Mark Wickert, September 1997. % start_temp = input('enter the starting temperature in Celsius: '); temp_step = input('enter the table temperature step in Celsius: '); % Perform calculations index = 1:25; T_Celsius = start_temp + (index-1)*temp_step; T_Rankin = 9/5*T_Celsius + 32 + 459.67; % Format data for plotting (quick easy way) disp([' Index#' ' T_Celsius' ' T_Rankin']) disp([index' T_Celsius' T_Rankin']) %*************End script file cel2rank.m*************** Testing A command window dialog session that sweeps through 10 C» cel2rank Enter the starting temperature in Celsius: 0 Enter the table temperature step in Celsius: 5 Index# T_Celsius T_Rankin 1.0000 0 491.6700 2.0000 5.0000 500.6700 3.0000 10.0000 509.6700 4.0000 15.0000 518.6700 5.0000 20.0000 527.6700 6.0000 25.0000 536.6700 7.0000 30.0000 545.6700 The Value Pre-calculated Checks Chapter 2: Problem Solving Applied 2 29
8.0000 35.0000 554.6700 9.0000 40.0000 563.6700 10.0000 45.0000 572.6700 11.0000 50.0000 581.6700 12.0000 55.0000 590.6700 13.0000 60.0000 599.6700 14.0000 65.0000 608.6700 15.0000 70.0000 617.6700 16.0000 75.0000 626.6700 17.0000 80.0000 635.6700 18.0000 85.0000 644.6700 19.0000 90.0000 653.6700 20.0000 95.0000 662.6700 21.0000 100.0000 671.6700 22.0000 105.0000 680.6700 23.0000 110.0000 689.6700 24.0000 115.0000 698.6700 25.0000 120.0000 707.6700 Chapter 2: Problem Solving Applied 2 30