1 / 48

Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege

Engr/Math/Physics 25. Chp5 MATLAB Plots & Models 1. Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu. Learning Goals. List the Elements of a COMPLETE Plot e.g.; axis labels, legend, units, etc. Construct Complete Cartesian (XY) plots using MATLAB

Télécharger la présentation

Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Engr/Math/Physics 25 Chp5 MATLABPlots & Models 1 Bruce Mayer, PE Licensed Electrical & Mechanical EngineerBMayer@ChabotCollege.edu

  2. Learning Goals • List the Elements of a COMPLETE Plot • e.g.; axis labels, legend, units, etc. • Construct Complete Cartesian (XY) plots using MATLAB • Modify or Specify MATLAB Plot Elements: Line Types, Data Markers, Tic Marks • Distinguish between INTERPolation and EXTRAPolation

  3. Learning Goals cont • Construct using MATLAB SemiLog and LogLog Cartesian Plots • Use MATLAB’s InterActive Plotting Utility to Fine-Tune Plot Appearance • Create “Linear-Transform” Math Models for measured Physical Data • Linear Function → No Xform • Power Function → LogLog Xform • Exponential Function → SemiLog Xform

  4. Learning Goals cont • Use Regression Analysis as quantified by the “Least Squares” Method • Calculate • Sum-of-Squared Errors (SSE or J) • The Squared Errors are Called “Residuals” • “Best Fit” Coefficients • Sum-of-Squares About the Mean (SSM or S) • Coefficient of Determination (r2) • Scale Data if Needed • Creates more meaningful spacing

  5. Learning Goals cont • Build Math Models for Physical Data using “nth” Degree Polynomials • Use MATLAB’s “Basic Fitting” Utility to find Math models for Plotted Data • Use MATLAB to Produce 3-Dimensional Plots, including • Surface Plots • Contour Plots

  6. Why Plot? • Engineering, Math, and Science are QUANTITATIVE Endeavors, we want NUMBERS as Well as Words • Many times we Need to • Understand The (functional) relationship between two or More Variables • Compare the Values of MANY Data Points

  7. Why Plot? cont • Plots have TREMENDOUS Utility in Two Major Areas • Communication • To Help OTHERS understand the RESULTS of Your Tests or Experiments or Theories • Analysis • To Help You ANALYZE Data or Theories to Determine the Significance or Meaning of the Data

  8. Plotting Trivia • Rene Decartes (1596-1650) Developed “Cartesian” (XY) Plots in about 1637 • Florence Nightingale Developed the “Polar Area Plot” (Pie Chart) in 1857

  9. Plot Title Tic Mark Label Connecting Line Axis UNITS Data Symbol Annotations Tic Mark Legend Axis Title

  10. Arrow CallOut Theoretical Plot → NO Data Markers Important Output Value

  11. Consider a Rocket Launch MATLAB Plot Example • A Math Model for the Height, y, vs. the Distance, x: • Where both x & y are in units of miles • Use MATLAB to Plot y vs x for a 51 mi DownRange Dist

  12. The Results The Command Session >> x = [0:0.1:51]; >> y = 0.43*sqrt(1.73*x); >> plot(x,y) >> xlabel('Distance (mi)') >> ylabel('Height (mi)') >> title('Rocket Height as a Function of Downrange Distance')

  13. The plot appears in the Figure window Output from One of Use the menu system. Select Print on the File menu in the Figure window. Answer OK when you are prompted to continue the printing process. Type print at the command line. This command sends the current plot directly to the printer. Save the plot to a file to be printed later or imported into another application such as PowerPoint. You need to know something about graphics file formats to use this file properly. See the subsection Exporting Figures. Plot OutPut

  14. The essential features of a Maximally Understandable Plot 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 (such as when plotting both speed and distance versus time), then indicate the units in the axis label if there is room, or in the legend or labels for each curve Each axis should have regularly spaced tick marks at convenient intervals - not too sparse, but not too dense - with 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 Elements of a Useful Plot

  15. If you plot more than one curve or data set, label each Curve/DataSet on its plot or use a legend to distinguish them. If you are preparing multiple plots of a similar type or if the axes’ labels cannot convey enough information, use a title. When in Doubt, TITLE If you plot 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 (within a single Data-Set), then plot them using the dot symbol. Elements of a Useful Plot cont

  16. 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 what occurs between the data points. Take appropriate care to prevent such MisInterpretation. 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. The Curve should be SMOOTH Elements of a Useful Plot cont

  17. grid Command • The grid command displays gridlines at the tick marks corresponding to the tick labels. • Type grid on to add gridlines; • Type grid off to stop plotting gridlines. • When used by itself, grid toggles this feature on or off, but you might want to use grid on and grid off to be sure.

  18. axis Command • The axis command overrides the MATLAB Default selections for the axis limits. • The basic syntax: axis([xmin xmax ymin ymax]). • This command sets the scaling for the x- and y-axes to the minimum and maximum values indicated. Note that, unlike an array, this command does not use commas to separate the values.

  19. LineWidth Command • MATLAB’s Default width and color for a plotted line are • Thin • Blue • This “thin blue line” is often hard to SEE and to PHOTOCOPY • Use 'LineWidth',n, to increase WIDTH • Use color-spec to make BLACK

  20. Affect of grid, axis, LineWidth • Compare to the Previous Version Command Session plot(x,y, 'k', 'LineWidth', 3), xlabel('Distance (mi)'),... ylabel('Height (mi)'),grid on, axis([0 51 0 4.1]),... title('Rocket Height as a Function of Downrange Distance')

  21. A single Row or Column Vector, v, can be plotted as plot(v) The X-Axis value = Vector Index; 1, 2, 3,...n The Y-Axis value = Vector-Value Example: Plot TOP DataSet from sld-9 using Row Vector The 39 Data Points for Vector, p Plotting Vectors >> p = [143, 151, 164, 149, 154, 169, 164, 172, 181, 183, 167, 177, 163, 199, 164, 168, 162, 155, 191, 153, 151, 150, 143, 177, 142, 145, 138, 136, 147, 143, 161, 137, 138, 138, 136, 140, 147, 148, 151] • The Plot Statement >> plot(p), xlabel('Hole No'), ylabel('DelP (10x Torr)'),... title('Distribution Tube Uniformity Test'), grid

  22. Vector Plot

  23. MATLAB can Not Plot x vs z if z is Complex For Complex z, the statement plot(z) is effectively the Same as plot(real(z), imag(z)) Example Plot for Complex Number Plots • Command Session >> w = 0.3-.7j >> n = [0:0.1:10]; >> z = cos(w.^n); >> plot(z), xlabel('Re'), ylabel('Im') >> u = cos(w^4.7) u = 1.0011 - 0.0386i >> u = cos(w^.6) u = 0.9182 + 0.3476i

  24. Complex Plot

  25. To make a quick Function-Plot, use MATLAB’s fplot Need Only The FUNCTION Independent Variable RANGE The fplot Syntax: fplot → “Smart” Plotting • String Text String that describes the function • xmin & xmax are the plotting Range: • Example the transient Response for an RLC Circuit (c.f. ENGR43): fplot(‘string’, [xmin, xmax]) • Apply fplot over the range of 0-9 sec • Where

  26. The fplot Command Session fplot('(exp(-0.3*t))*(7*cos(13*t) - 11*sin(13*t))', [0 9]) EQUIVALENT Session >> u = '(exp(-0.3*t))*(7*cos(13*t) - 11*sin(13*t))'; >> fplot(u, [0 9])

  27. Anonymous Function & fplot Command Session uofx = @(x) cos(x)/log(x+3) fplot(uofx, [0 37]),grid

  28. Beware fplot Syntax uoft = @(t) (exp(-0.3*t)).*(7*cos(13*t) - 11*sin(13*t)) fplot('uoft', [0 9]),grid fplot(uoft, [0 9]),grid

  29. Recall the Polynomial fcn; e.g. Plot Polynomials w/ polyval • x is the Value of the independent variable • Evaluate the example polynomial at x = 73 • Find y using MATLAB’s polyval function >> P = [17 -31 5 19]; >> Y_73 = polyval(P,73) Y_73 = 6448474 y = polyval(p,x) • Where • p is a Vector containing of the (constant) coefficients of the polynomial

  30. Let’s Plot this Polynomial over −1.8  x  4.2 Plotting w/ polyval • Note the Zero Coeff. For the 3rd Degree Term • The Command Window Session >> p5 = [1,-2.31,0,-4.73,6.11,1.94]; >> x = [-1.8:.01:4.2]; >> plot(x, polyval(p5,x)), xlabel('x'), ylabel('y = f(x)')

  31. The polyvalPlot (also fminbnd) TheFigureWindow >> pofx = @(x) x.^5 - 2.31*x.^4 - 4.73*x.^2 + 6.11*x + 1.94 pofx = @(x)x.^5-2.31*x.^4-4.73*x.^2+6.11*x+1.94 >> [xmin, minval] = fminbnd(pofx, 0,3) xmin = 2.1371e+000 minval = -1.0212e+001

  32. Find Local Minimum • Find The MIN between 1-3 using polyval and min commands >> x1 = [1:.001:3]; >> y1 = polyval(p5,x1); >> [yMin, kMin] = min(y1) yMin = -10.2117 kMin = 1138 >> xMin = x1(kMin) xMin = 2.1370

  33. To save a figure that can be opened in subsequent MATLAB sessions, save it in a figure file with the .fig file name extension To do this, select Save from the Figure window File menu or click the Save button (the disk icon) on the toolbar. If this is the first time you are saving the file, the Save As dialog box appears. Make sure that the type is MATLAB Figure (*.fig). Specify the name you want assigned to the figure file. Click OK. Saving Figures

  34. Why .fig File? • The MATLAB FIG-file is a binary format to which you can save figures so that they can be opened in subsequent MATLAB sessions. • What is Saved  whole figure, including • Graph(s), • Graph data • Annotations • Edit later withOUTReDoing DATA

  35. Saving to .fig – Step-1 In Figure 1 Window Click File→Save As...

  36. Saving to .fig – Step-2 To Open Using MATLAB Select the .fig file-format Type in a descriptive FileName and hit Save

  37. To save the figure in a format that can be used by another application, such as the standard graphics file formats; e.g., TIFF or JPG, perform these steps Select Export Setup from the File menu. This dialog lets you specify options for the output file, such as the figure size, fonts, line size and style, and output format Select Export from the Export Setup dialog. A standard Save As dialog appears. Exporting Figures

  38. Select the format from the list of formats in the Save As type menu. This selects the format of the exported file and adds the standard file name extension given to files of that type The Instructor likes the .jpg (Joint Picture experts Group) format; a good compromise of: compatibility, file-size, and resolution Enter the name you want to give the file, less the extension. Then click Save. Exporting Figures cont

  39. Exporting to file  Step-1 In Figure 1 Window Click File→Export Setup...

  40. Saving to file  Step-2 In the Export Setup Dialog Box Click Export In the Save As Dialog Box Type in a descriptive FileName and hit Save

  41. The .jpg export

  42. MATLAB provides a Very Nice Utility in which a Plot is sent to the MSWindows ClipBoard for Subsequent Pasting into Other Applications To Save to the ClipBoard Select Copy Options from the Edit menu. The Copying Options page of the Preferences dialog box appears. Complete the fields on the Copying Options page and click OK. Select Copy Figure from the Edit menu. Windows ClipBoard Copy

  43. Copy to Clipboard  Step-1 In Figure 1 Window Click Edit→Copy Options...

  44. Copy to Clipboard  Steps 2&3 In the Preferences Dialog Box check the Copy-Options Boxes as you see Fit Close the Dialog Box

  45. Copy to Clipboard – Steps 4&5 In the Figure Window use File → Copy Figure to send the plot to the clipboard Paste the plot image into PowerPoint, Word, etc.

  46. The Copy Figure export

  47. All Done for Today The Best Plot EVERDone The French engineer, Charles Minard (1781-1870), illustrated the disastrous result of Napoleon's failed Russian campaign of 1812. The graph shows the size of the army by the width of the band across the map of the campaign on its outward and return legs, with temperature on the retreat shown on the line graph at the bottom. Many consider Minard's original the BEST statistical graphic ever drawn.

  48. Engr/Math/Physics 25 Appendix Time For Live Demo Bruce Mayer, PE Licensed Electrical & Mechanical EngineerBMayer@ChabotCollege.edu

More Related