ENGR-25_Plot_Model-2.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer Engr/Math/Physics 25 Chp5 MATLAB Plots & Models 2
ENGR-25_Plot_Model-2.ppt 2 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Learning Goals List the Elements of a COMPLETE Plots 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
ENGR-25_Plot_Model-2.ppt 3 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 → Log-Log Xform Exponential Function → SemiLog Xform
ENGR-25_Plot_Model-2.ppt 4 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 (r 2 ) Scale Data if Needed –Creates more meaningful spacing
ENGR-25_Plot_Model-2.ppt 5 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Learning Goals cont Build Math Models for Physical Data using “n th ” 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
ENGR-25_Plot_Model-2.ppt 6 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 Values
ENGR-25_Plot_Model-2.ppt 7 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Plot Title Axis Title Tic Mark Tic Mark Label Legend Data Symbol Annotations Axis UNITS Connecting Line
ENGR-25_Plot_Model-2.ppt 8 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods SubPlots The subplot command creates several smaller “subplots” in the same figure. The syntax is subplot(m,n,p) 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 p th pane.
ENGR-25_Plot_Model-2.ppt 9 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods SubPlot Example Consider an End-Loaded Cantilever Beam In ENGR36 & ENGR45 we will learn how the Applied Force-Load, F, affects the Beam
ENGR-25_Plot_Model-2.ppt 10 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods SubPlot Example By the ENGR36 & ENGR45 Methods Analyze how the load affects the Beam in Terms of SHEARING Force, V BENDING-Moment, M Vertical DEFLECTION, y The V, M, and y functions vs the Normalized Distance Dimension x/L
ENGR-25_Plot_Model-2.ppt 11 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods SubPlot Example We Want to Plot V, M, and y ON TOP of each other vs the common independent Variable, x/L This is a perfect Task for subplot First Construct the functions Note the use of the ones Command to construct the Constant Shear (V) vector >> XoverL = [0:0.01:1]; V = ones(1, length(XoverL)) >> M = XoverL - 1; >> y = 0.5*((XoverL).^2).*(XoverL -3);
ENGR-25_Plot_Model-2.ppt 12 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods SubPlot Example Next Make 3 subplots in an array with THREE Rows and ONE Column >> subplot(3,1,1) >> plot(XoverL,V), xlabel('x/L'), ylabel('V/Vmax'),... title('Cantilever Beam - Shear'), grid >> subplot(3,1,2) >> plot(XoverL,M), xlabel('x/L'), ylabel('M/Mmax'),... title('Cantilever Beam - Bending'), grid >> subplot(3,1,3) >> plot(XoverL,y), xlabel('x/L'), ylabel('y/ymax'),... title('Cantilever Beam - Deflection'), grid
ENGR-25_Plot_Model-2.ppt 13 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods SubPlot Result Demo SubPlot_3x2_Thk_Lines_1103.m
ENGR-25_Plot_Model-2.ppt 14 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Data-Markers & Line-Types The Data Marker and Line Type specifications in a Plot consist of optional Fields in the Basic plot statement To plot y versus x with a solid line, and then v versus u with a dashed line, type plot(x,y,u,v,’--’) The symbols ’--’ represent a dashed line To plot y versus x with asterisks (*) connected with a dotted line, we must plot the data twice by typing plot(x,y,’*’,x,y,’:’).
ENGR-25_Plot_Model-2.ppt 15 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods DataMark & LineType Example To plot y versus x with green asterisks (*) connected with a red dashed line, again plot the data twice by typing plot(x,y,’g*’,x,y,’r--’) Example: Mechanical Creep Consider a PolyStryene Cantilever Beam (c.f. sld-8) Heavily Loaded for a “long” time
ENGR-25_Plot_Model-2.ppt 16 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods DataMark & LineType Example We Would Like to plot the Vertical Deflection, Y, versus time, t, for a Constant Load using Blue Colored Data Makers in the from of a + mark Magenta Colored Dash-Dot (-.) Line to connect the Data Points
ENGR-25_Plot_Model-2.ppt 17 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods DataMark & LineType Example The Command Session for the Creep Plot The Data Set as Row Vectors >> delY_mm = [0, 2, 4, 4.5, 5.5, 6, 6.5, 8, 9, 11]; >> t_min = [0, 2, 4, 6, 9, 12, 15, 18, 21, 24]; The Plot Statement >> plot(t_min,delY_mm, ’b+’, t_min,delY_mm, 'm-.’),... xlabel('Load Application Time, t (min)'),... ylabel('Vertical Deflection, y (mm)'),... title('Polystrene Cantilever Beam Creep'), grid Notice the Data is plotted Twice Notice also the Blue- Plus, and Magenta Dash-Dot Specs
ENGR-25_Plot_Model-2.ppt 18 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods DataMaker & LineType Result
ENGR-25_Plot_Model-2.ppt 19 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods More: Markers, Lines, Colors MATLAB Provides a Wide Range of Options for Plot-Lines & Plot-Markers Data markers † Dot (.) Asterisk (*) Cross ( ) Circle ( ) Plus sign ( ) Square ( ) Diamond ( ) Five-pointed star ( ).*sdp.*sdp Line types Solid line Dashed line Dash-dotted line Dotted line –– – –. …. Colors Black Blue Cyan Green Magenta Red White Yellow kbcgmrwykbcgmrwy † Other data markers are available. Search for “markers” or “LineSpec” in MATLAB help. Don’t forget the 'LineWidth', n Command to make thicker lines
ENGR-25_Plot_Model-2.ppt 20 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Labeling Curves The legend command automatically obtains from the plot the line type used for each data set and displays a sample of this line type in the legend box next to the selected text-string For Example again Consider RLC Circuit “Ringing” We Now wish to plot e −0.3t to Show the ENVELOPE of Exponential Decay
ENGR-25_Plot_Model-2.ppt 21 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Labeling Curves Example Use Defaults for the main Function, V(t) Use Red, Dashed (--) Lines for the d(t) Decay The Calc and Plot statements >> t = [0:0.02:9]; >> V_t = (exp(-0.3*t)).*(9*cos(13*t)); >> d1 = 9*exp(-0.3*t); >> d2 = -9*exp(-0.3*t); >> plot(t,V_t, t, d1, 'r--', t, d2, 'r--'), xlabel('t'),... ylabel('V'), title('RLC Repsonse'),grid,... legend('Sinusoidal Oscillations', ’Exponential Decay')
ENGR-25_Plot_Model-2.ppt 22 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods legend Use Result
ENGR-25_Plot_Model-2.ppt 23 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Labeling Curves with gtext The gtext Command allows the USER to Place the curve Labels at Any Location on the Plot with the Mouse. Let’s use gtext(‘string’) to Label the Decaying Sinusoid Plot with Labels Located in Close Proximity with the two Curves
ENGR-25_Plot_Model-2.ppt 24 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods gtext Use Result
ENGR-25_Plot_Model-2.ppt 25 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods The hold command TYU T5.2-5 The hold Command “Freezes” plots for subsequent modification. Illustrate by T5.2-5 Example Say we Want to Plot over 0 x 1 Over this Range These functions are too difficult to Distinguish for gtext Labeling Solution → Plot & Label y 1 First Plot & Label y 2 2 nd
ENGR-25_Plot_Model-2.ppt 26 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Making BETTER Plots The following actions, while not required, can nevertheless improve the appearance & clarity of your plots: 1.Start scales from Zero when possible –This prevents a false impression of the magnitudes of variations shown on the plot Invalid if we want to emphasize DIFFERENCES 2.Use sensible tick-mark spacing –If the quantities are months, choose a spacing of 12 because 1/10 of a year is not a convenient division.
ENGR-25_Plot_Model-2.ppt 27 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Making BETTER Plots cont –Tic marks should be Neither “Too” Course/Fine 3.Minimize the number of Zeros and Scientific-Notation in the data being plotted –For example, use a scale in µW (microWatts) to show Power Data Ranging Over – Watts (11 – 137 µW) 4.Determine the min & max data values for each axis before plotting the data. Then set the axis limits to cover the entire data range (or Span) plus an additional amount to allow for clearly understood tick-marking
ENGR-25_Plot_Model-2.ppt 28 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Making BETTER Plots cont –e.g; 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 permits a tick spacing of 1 or 2. 5.Use a different line type for each curve when several are plotted on a single plot and they cross each other; –For 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/or photocopies.
ENGR-25_Plot_Model-2.ppt 29 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Making BETTER Plots cont 6.Do not put too many curves on one plot, particularly if they will –Be close to each other –Cross one another at several points. 7.Use the same scale limits and tick spacing on each plot if you, or the plot Users, need to compare information on more than one plot. –This is VERY Important The Instructor Made this Mistake on the 1 st Draft of His 1 st Publication – Luckily a colleague spotted the error during her careful review of the draft
ENGR-25_Plot_Model-2.ppt 30 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Proper Comparison Scaling Same Scales
ENGR-25_Plot_Model-2.ppt 31 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Select Plot: WorkSpace Browser >> a = linspace(0,10,500); >> y = 4*cos(a)-22*sin(1.5*a+4);
ENGR-25_Plot_Model-2.ppt 32 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Select Plot: WorkSpace Browser Results can be a Plot of your choice >> B =[7, 19, 23, 3,17, 11] B =
ENGR-25_Plot_Model-2.ppt 33 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Other Useful Plots CommandPlot Description bar(x,y) Bar chart of y versus x. plotyy(x1,y1,x2,y2) Produces a plot with two y-axes, y1 on the left and y2 on the right polar(theta,r,’type’) Polar plot from the polar coordinates theta and r, using the line type, data marker, and colors specified in the string type. stairs(x,y) Stairs plot of y versus x stem(x,y) Stem plot of y versus x.
ENGR-25_Plot_Model-2.ppt 34 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Column & Bar Chart MATLAB Col Chart >> Y = round(rand(5,3)*10); >> bar(Y,'stack') MATLAB Bar Chart
ENGR-25_Plot_Model-2.ppt 35 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Greek Letters on Plots ( ΠΣλξ ) Use the “\” then a “Character Sequence” in a Labeling Field
ENGR-25_Plot_Model-2.ppt 36 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Greek Letters on Plots ( ωφΘΔ ) Use the “\” then a “Character Sequence” in a Labeling Field
ENGR-25_Plot_Model-2.ppt 37 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods In the Greek….
ENGR-25_Plot_Model-2.ppt 38 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Greek Code % Bruce Mayer, PE % ENGR-25 02Jul13 % XY_fcn_Graph_6x6_BlueGreen_BkGnd_Template_1306.m % % The FUNCTION x = linspace(-6,6,500); y = -x.^2/3 +5.5; % % The ZERO Lines zxh = [-6 6]; zyh = [0 0]; zxv = [0 0]; zyv = [-6 6]; % % the 6x6 Plot axes; set(gca,'FontSize',12); whitebg([ ]); % Chg Plot BackGround to Blue-Green plot(x,y, zxv,zyv, 'k', zxh,zyh, 'k', 'LineWidth', 3),axis([ ]),... grid, xlabel('\fontsize{18}\Theta'), ylabel('\fontsize{18}\Psi = \phi(\Theta)'),... title(['\fontsize{16}ENGR25 Engr Honor Soc \rightarrow \tau-\beta-\pi',]),... annotation('textbox',[ ], 'FitBoxToText', 'on', 'EdgeColor', 'none', 'String', '- \infty','FontSize',48)
ENGR-25_Plot_Model-2.ppt 39 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Plot Additions hold non / off Use the hold command to add lines, markers, and others with DIFFERENT formatting to an Existing Plot Some Help Searches that assist with formatting Text Properties LineSpec
ENGR-25_Plot_Model-2.ppt 40 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example hold non / off
ENGR-25_Plot_Model-2.ppt 41 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods hold non / off Code % Bruce Mayer, PE % ENGR-25 01Jul13 % XY_fcn_Graph_BlueGreenBkGnd_Solid_Marker_Template1306.m % % The Limits xmin = -3; xmax1 = 1; xmin2 = xmax1; xmax = 3; ymin = -4; ymax = 10; % The FUNCTION x1 = linspace(xmin,xmax1,500); y1 = 1-x1.^2; x2 = linspace(xmin2,xmax,500); y2 = 3*x2+1; % % The ZERO Lines zxh = [xmin xmax]; zyh = [0 0]; zxv = [0 0]; zyv = [ymin ymax]; % % the BASE Plot axes; set(gca,'FontSize',12); whitebg([ ]); % Chg Plot BackGround to Blue-Green plot(x1,y1,'b', x2,y2,'b', x1(end),y1(end), 'ob', 'MarkerSize', 16, 'MarkerFaceColor', [ ],... 'LineWidth', 5),axis([xmin xmax ymin ymax]),... grid, xlabel('\fontsize{16}\chi'), ylabel('\fontsize{16}y \equiv \phi(\chi)'),... title(['\fontsize{16}ENGR25 Bruce Mayer, PE HOLD',]),... annotation('textbox',[ ], 'FitBoxToText', 'on', 'EdgeColor', 'none', 'String', 'XYfcnGraphBlueGreenBkGndSolidMarkerTemplate1306.m','FontSize',7) % % Turn-ON hold to ADD to BASE Plot Lines & Markers of different formats hold on plot(x2(1),y2(1), 'ob', 'MarkerSize', 11, 'MarkerFaceColor', 'g') plot(zxv,zyv, 'k', zxh,zyh, 'k', 'LineWidth', 3) plot([xmin xmax], [4 4], '-.m', [1 1], [ymin ymax], '-.m', 'LineWidth', 2) set(gca,'XTick',[xmin:1:xmax]); set(gca,'YTick',[ymin:1:ymax]) hold off
ENGR-25_Plot_Model-2.ppt 42 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Dual Y-Axis Plot
ENGR-25_Plot_Model-2.ppt 43 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods poltyy in MATLAB
ENGR-25_Plot_Model-2.ppt 44 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods stairs Plot – sin(x) >> x=0:0.25:10; >> stairs(x,sin(x));
ENGR-25_Plot_Model-2.ppt 45 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods stem Plot - >> x = 0:0.1:4; >> y = sin(x.^2).*exp(-x); >> stem(x,y)
ENGR-25_Plot_Model-2.ppt 46 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods TYU T5.3-3 Polar Plot Archimedes Spiral The interactive Session The BMayer Plot >> theta = linspace(0, 4*pi, 500); >> a = 2; >> r = a*theta; >> subplot(1,1,1) >> polar(theta, r), title('Spiral of Archimedes')
ENGR-25_Plot_Model-2.ppt 47 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods All Done for Today SeaShell Built on Archimedes Spiral
ENGR-25_Plot_Model-2.ppt 48 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer Engr/Math/Physics 25 Appendix
ENGR-25_Plot_Model-2.ppt 49 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods gtext Demo The Command Session >> t = [0:0.02:9]; >> V_t = (exp(-0.3*t)).*(9*cos(13*t)); >> d1 = 9*exp(-0.3*t); >> d2 = -9*exp(-0.3*t); >> plot(t,V_t, t, d1, 'r--', t, d2, 'r--'), xlabel('t'),... ylabel('V'), title('RLC Repsonse'),grid,... gtext('Sinusoid'), gtext('Exponential Decay'),... gtext('Exponential Decay') TWO “Exponential Decay” Labels; one each for the TOP and BOTTOM Curves –Demo_Chp5_gtext_6010.m
ENGR-25_Plot_Model-2.ppt 50 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods hold Demo >> x = [0:0.01:1]; >> y1 = sin(x); y2 = x-x.^3/3; >> plot(x,y1), gtext('sin(x)'), hold, plot(x,y2), xlabel('x'),... ylabel('y'), gtext('x-x^3/3') Current plot held Formatting Info