# plotting and model building in matlab

Embed Size (px)

TRANSCRIPT

Week 14 Plotting and Model Building in Matlab XY Plotting Functions Subplots Overlay Plots Special Plot Types Function Discovery Homework

Anatomy of a Plot (XY Plotting Functions)most common plot is the xy plot. We are plotting the function y=f(x). x values are plotted on the horizontal axis (abscissa) and y values are plotted on the vertical axis (ordinate). The typical xy plot has many facets: Scale: refers to the range and spacing of the numbers on the axis Tick mark: placed on axis to help you visualize the numbers being plotted. Axis label: this gives the name and units of the quantity plotted on an axis. Data symbol: representation of a data point Legend: relates the data set symbol to the quantity being plotted The

Requirements for a Correct Plot1. Each axis must be labeled with the name of the quantity being plotted and its units. If two or more quantities having different units are plotted (e.g. when plotting both speed and distance versus time), indicate the units in the axis label if there is room, or in the legend or labels for each curve. 2. Each axis should have regularly spaced tick marks at convenient intervalsnot to sparse, but not too densewith a spacing that is easy to interpret and interpolate (e.g. use 0.1, 0.2, and so on, rather than 0.13, 0.26, etc.). 3. If you are plotting more than one curve or data set, label each on its plot or use a legend to distinguish them. 4. If you are preparing multiple plots of a similar type or if the axes labels cannot convey enough information, use a title. 5. If you are plotting measured data, plot each data point with a symbol such as a circle, square, or cross (use the same symbol for every point in the same data set). If there are many data points, plot them using the dot symbol.

Requirements for a Correct Plot6. Sometimes data symbols are connected by lines to help the viewer visualize the data, especially if there are few data points. However, connecting the data points, especially with a solid line, might be interpreted to imply knowledge of that occurs between the data points. Thus you should be careful to prevent such misinterpretation. 7. If you are plotting points generated by evaluating a function (as opposed to measured data), do not use a symbol to plot the points. Instead, be sure to generate many points, and connect the points with solid lines.

Plot, Label, and Title Commands y=0.41.8x for 0 x 52 >> x=[0:.1:52]; >> y=.4*sqrt(1.8*x); >> plot(x,y),xlabel('Distance (miles)'),ylabel('Height (miles)'),... title('Rocket Height as a Function of Downrange Distance'),... grid on, axis([0 52 0 5])

The Resulting Graph5 4.5 4 3.5 Height (miles) 3 2.5 2 1.5 1 0.5 0 0 5 10 15 20 25 30 Distance (miles) 35 40 45 50 Rocket Height as a Function of Downrange Distance

grid, axis, fplot Commands grid: displays gridlines at the tick marks corresponding to the tick labels; grid on (adds gridlines); grid off (stops plotting grid lines) axis: overrides Matlab selections for the axis limits axis[([xmin xmax ymin ymax]) This sets the scaling for the x- and y- axes to the minimum and maximum values indicated. Variants include: axis square; axis equal; axis auto fplot: automatically analyzes the function to be plotted and decides how many plotting points to use so that the plot will show all the features of the function. fplot(string, [xmin xmax]) String is a text string that describes the function to be plotted and [xmin xmax] specifies the minimum/maximum values of the independent variable. The range of the dependent variable y can also be specified. fplot(string, [xmin xmax ymin ymax])

Compare Graphs >> f = 'cos(tan(x)) - tan(sin(x))'; >> fplot(f,[1 2])

>> x=[1:.01:2]; >> y=cos(tan(x))-tan(sin(x)); >> plot(x,y)

Using fplot Command-0.5

-1

-1.5

-2

-2.5

-3

1

1.1

1.2

1.3

1.4

1.5

1.6

1.7

1.8

1.9

2

fplot automatically chose enough points to display all the variations in the function. You can achieve the same plot using plot, but you need to know how many values to use in specifying the x vector.

Using plot Command-0.5

-1

-1.5

-2

-2.5

-3

1

1.1

1.2

1.3

1.4

1.5

1.6

1.7

1.8

1.9

2

Subplots and Overlay Plots Matlab can create figures that contain an array of plots, called subplots. These are useful when you want to compare the same data plotted with different axis types. subplot(m,n,p) is the syntax used to obtain several smaller subplots in the same figure. This command divides the figure window into an array of rectangular panes with m rows and n columns. The variable p tells Matlab to place the output of the plot command following the subplot command into the pth pane. An overlay plot comes into play when we need to plot one curve or data set on a single plot. Lets look more closely now at subplots.

For example, subplot (3,2,5) creates an array of six panes, three panes deep and two panes across and directs the next plot to appear in the fifth pane (in the bottom left corner). y=e-1.2.xsin(10x+5) for 0 y = exp(-1.2*x).*sin(10*x+5); >> subplot(1,2,1) >> plot(x,y),xlabel('x'),ylabel('y'),axis([0 5 -1 1]) >> x = [-6:.01:6]; >> y = abs(x.^3-100); >> subplot(1,2,2) >> plot(x,y),xlabel('x'),ylabel('y'),axis([-6 6 0 350]) >>

The next slide shows the output

The following shows the plots of the following functions: y=e-1.2xsin(10x+5) for 0x=[8,9,10];

plot(x,A)

plot(A,B)

>>B=[1,2,3;4,5,6;8,9,10];

Hints for Improving Plots Start scales from zero whenever possible. This technique prevents a false impression of the magnitudes of any variations shown on the plot. Use sensible tick-mark spacing. Space tick marks as close as is useful, but no closer. Minimize the number of zeros in the data being plotted. Example: Use a scale in millions of dollars when appropriate, instead of a scale in dollars with 6 zeros after every number. Determine the minimum and maximum data values for each axis before plotting data. Then set the axis limits to cover the entire data range plus an additional amount to allow convenient tick-mark spacing to be selected. Example:

If the data on the x-axis ranges from 1.2 to 9.6, a good choice for axis limits is 0 to 10. This choice will allow you to use tick spacing of 1 or 2. Use a different line type for each curve when several are plotted on a single plot and they cross each other. Example: Use a solid line, a dashed line, and combinations of lines and symbols. Beware of using colors to distinguish plots if you are going to make black-and-white printouts and copies. Do not put many curves on one plot, particularly if they will close to each other or cross one another. Use the same scale limits and tick spacing on each plot if you need to compare information on more than one plot.

Important Things to Remember When Using Log Scales 1. You cannot plot negative numbers on a log scale since the log of a negative number is not defined. 2. You cannot plot 0 on a log scale because

log 100 = ln 0 =

You must choose an appropriately small number as the lower limit on the plot. 3. The tick-mark labels on a log scale are the actual values being plotted; they are not the logarithms of the numbers. 4. Equal distances on a log scale correspond to the multiplication by the same constant (as opposed to addition of the same constant on a rectilinear scale). For example, all numbers that differ by a factor of 10 are separated by the same distance on the log scale. That is, the distance between 0.3 and 3 is the same as the distance between 30 and 300. This separation is referred

to as a decade or cycle. 5. Gridlines and tick marks within a decade are unevenly spaced. If 8 gridlines or tick marks occur within the decade, they correspond to values equal to 2,3,4,.,8,9 times the value represented by the first gridline or tick mark of the decade.

Different Scales and Corresponding Matlab Commands Rectilinear: standard xy scale Log scale/log plot: Used to represent a data set that covers a wide range of values and used to identify certain trends in data. A log-log plot has log scales on both axes. Use the loglog(x,y)command to make both scales Semilog plot: has a log scale on only one axis. Use the command and use the x scale logarithmic and the y scale y rectilinear to have the semilog(y) command to have the logarithmic and the x scale rectilinear.

Function Discovery Function discovery is the process of finding or discovering a function that can describe a particular set of data. y(x)=mx+b is the linear function and gives a straight line when plotted on rectilinear axes. Its slope is m and its intercept is b. y(x)=bxm is the power function and gives a straight line when plotted on log-log axes. y(x)=bemx is the exponential function and gives a straight line when plotted on a semilog plot whose y-axis is logarithmic. Fortunately, many applications generate data that these functions can describe.

So, What are the Steps for Function Discovery? 1. Examine the data near the origin. The exponential function can never pass through the origin (unless b=0). The linear function can pas through the origin only if b=0 and the power function can pass through the origin but only if m>0. 2. Plot the data using rectilinear scales. If it forms a straight line, then it can be represented by the liner function and you are finished. Otherwise, if you have data at x=0, then A. if y(0)=0 try the power function B. if y(0) is not = 0, try the exponential function If data is not given for x=0, proceed to step 3.

3. If you suspect a power function, plot the data using log-log scales. Only a power function will form a straight line on a log-log plot. If you suspect an exponential function, plot the data using the semilog scales. Only an exponential function will form a straight line on a semilog plot. 4. In function discovery applications, we use the log-log and semilog plots only to identify the function type, but not to find the coefficients b and m. The reason is that it is difficult to interpolate on log scales. Interpolation: After discovering a functional relation that describes the data, you can use it to predict conditions that lie within the range of original data. Extrapolation: The process of using the model to predict conditions that lie outside the original data range.

Regression We can distinguish between two types of analysis in experiments involving two variables (x and y). In correlation analysis both variables are random and we are interested in finding the relationship between them. In regression analysis (todays focus) one of the variables (say, x) is regarded as an ordinary variable (independent/controlled) because we can measure it without error or assign it whatever values we want. Variable y is the random variable. Regression analysis deals with the relationship between x and y. An example could be the dependence of the outdoor temperature (y) on the time of day (x).

Functions for Polynomial Regression p = polyfit(x,y,n)fits a polynomial of degree n to data described by the vectors x and y, where x is the independent variable. Returns a row vector p of length n+1 that contains the polynomial coefficients in order of descending powers. P = polyfit(log10(x), log10(y) [power function] P = polyfit(x,log10,y),1) [exponential function]

Week 14 Homework Read: Chapter 5, Plotting and Model Building in Matlab in Introduction to Matlab 7 for Engineers by Palm, pp. 258-311. Homework: Problems 9, 14, 17, 20, 33, 37 (Palm, Chapter 4), pp. 241-257. Homework is due at the start of recitation, April 27 and 28, 2006.