OSU ENGR H192 - Lecture 22A - MATLAB Plotting

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32MATLAB - Lecture 22ATopics Covered:1. Plotting basic 2-D plots.The plot command.The fplot command.Plotting multiple graphs in the same plot.Formatting plots.Two Dimensional Plots / Chapter 5MAKING X-Y PLOTS MATLAB has many functions and commands that can be used to create various types of plots.105In our class we will only create two dimensional x – y plots.8 10 12 14 16 18 20 22 24020040060080010001200DISTANCE (cm)INTENSITY (lux)Light Intensity as a Function of DistanceComparison between theory and experiment.TheoryExperimentPlot titley axislabelx axislabelTextTick-mark labelEXAMPLE OF A 2-D PLOTData symbol106LegendTick-markTWO-DIMENSIONAL plot() COMMAND where x is a vector (one dimensional array), and y is a vector. Both vectors must have the same number of elements. The plot command creates a single curve with the x values on the abscissa (horizontal axis) and the y values on the ordinate (vertical axis). The curve is made from segments of lines that connect the points that are defined by the x and y coordinates of the elements in the two vectors.106- 110The basic 2-D plot command is:plot(x,y) If data is given, the information is entered as the elements of the vectors x and y. If the values of y are determined by a function from the values of x, than a vector x is created first, and then the values of y are calculated for each value of x. The spacing (difference) between the elements of x must be such that the plotted curve will show the details of the function. CREATING THE X AND Y VECTORS106- 110PLOT OF GIVEN DATA Given data:>> x=[1 2 3 5 7 7.5 8 10];>> y=[2 6.5 7 7 5.5 4 6 8];>> plot(x,y)A plot can be created by the commands shown below. This can be done in the Command Window, or by writing and then running a script file.Once the plot command is executed, the Figure Window opens with the following plot.106- 107xy1 2 3 5 7 7.5 86.5 7 7 5.5 4 6 8102106- 107PLOT OF GIVEN DATA106- 107LINE SPECIFIERS IN THE plot() COMMANDLine specifiers can be added in the plot command to:Specify the style of the line.Specify the color of the line.Specify the type of the markers (if markers are desired).plot(x,y,’line specifiers’)106- 107LINE SPECIFIERS IN THE plot() COMMANDLine Specifier Line Specifier Marker SpecifierStyle Color TypeSolid - red r plus sign +dotted : green g circle odashed -- blue b asterisk *dash-dot -. Cyan c point .magenta m square syellow y diamond dblack kplot(x,y,‘line specifiers’)107- 108LINE SPECIFIERS IN THE plot() COMMANDThe specifiers are typed inside the plot() command as strings.Within the string the specifiers can be typed in any order.The specifiers are optional. This means that none, one, two, or all the three can be included in a command.EXAMPLES:plot(x,y) A solid blue line connects the points with no markers.plot(x,y,’r’) A solid red line connects the points with no markers.plot(x,y,’--y’) A yellow dashed line connects the points.plot(x,y,’*’) The points are marked with * (no line between the points.)plot(x,y,’g:d’) A green dotted line connects the points which are marked with diamond markers.110- 111YearSales (M)1988 1989 1990 1991 1992 1993 1994127 130 136 145 158 178 211PLOT OF GIVEN DATA USING LINE SPECIFIERS IN THE plot() COMMAND >> year = [1988:1:1994];>> sales = [127, 130, 136, 145, 158, 178, 211];>> plot(year,sales,'--r*')Line Specifiers:dashed red line and asterisk markers.110- 111PLOT OF GIVEN DATA USING LINE SPECIFIERS IN THE plot() COMMAND Dashed red line and asterisk markers.% A script file that creates a plot of% the function: 3.5^(-0.5x)*cos(6x)x = [-2:0.01:4];y = 3.5.^(-0.5*x).*cos(6*x);plot(x,y)CREATING A PLOT OF A FUNCTION Consider:42for)6cos(5.35.0xxyxA script file for plotting the function is:Creating a vector with spacing of 0.01. Calculating a value of y for each x. Once the plot command is executed, the Figure Window opens with the following plot.111- 112A PLOT OF A FUNCTION 42for)6cos(5.35.0xxyx111- 112CREATING A PLOT OF A FUNCTION If the vector x is created with large spacing, the graph is not accurate.Below is the previous plot with spacing of 0.3.111- 112x = [-2:0.3:4];y = 3.5.^(-0.5*x).*cos(6*x);plot(x,y)112- 113THE fplot COMMANDfplot(‘function’,limits)The fplot command can be used to plot a functionwith the form: y = f(x)The function is typed in as a string.The limits is a vector with the domain of x , and optionally with limits of the y axis:[xmin,xmax] or [xmin,xmax,ymin,ymax]Line specifiers can be added.112- 113PLOT OF A FUNCTION WITH THE fplot() COMMAND>> fplot('x^2 + 4 * sin(2*x) - 1', [-3 3])33for1)2sin(42 xxxyA plot of:PLOTTING MULTIPLE GRAPHS IN THE SAME PLOT Plotting two (or more) graphs in one plot:1. Using the plot command.2. Using the hold on, hold off commands.114- 116USING THE plot() COMMAND TO PLOTMULTIPLE GRAPHS IN THE SAME PLOT Plots three graphs in the same plot: y versus x, v versus u, and h versus t.By default, MATLAB makes the curves in different colors.Additional curves can be added.The curves can have a specific style by adding specifiers after each pair, for example:114- 115plot(x,y,u,v,t,h)plot(x,y,’-b’,u,v,’—r’,t,h,’g:’)114- 115USING THE plot() COMMAND TO PLOTMULTIPLE GRAPHS IN THE SAME PLOT 42  xPlot of the function, and its first and second derivatives, for , all in the same plot.102633 xxy42  xx = [-2:0.01:4];y = 3*x.^3-26*x+6;yd = 9*x.^2-26;ydd = 18*x;plot(x,y,'-b',x,yd,'--r',x,ydd,':k')vector x with the domain of the function.Vector y with the function value at each x.42  xVector yd with values of the first derivative.Vector ydd with values of the second derivative.Create three graphs, y vs. x (solid blue line), yd vs. x (dashed red line), and ydd vs. x (dotted black line) in the same figure.114- 115-2 -1 0 1 2 3 4-40-20020406080100120USING THE plot() COMMAND TO PLOTMULTIPLE GRAPHS IN THE SAME PLOThold on Holds the current plot


View Full Document

OSU ENGR H192 - Lecture 22A - MATLAB Plotting

Documents in this Course
Strings

Strings

22 pages

Load more
Download Lecture 22A - MATLAB Plotting
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 22A - MATLAB Plotting and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 22A - MATLAB Plotting 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?