egr 102 introduction to engineering modeling

29
EGR 102 Introduction to Engineering Modeling Lab 05B Plotting 1 EGR 102 - Fall 2018

Upload: others

Post on 04-Dec-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

EGR 102Introduction to Engineering

Modeling

Lab 05B

Plotting

1EGR 102 - Fall 2018

Overview

• Plotting in MATLAB

• 2D plotting ( ezplot(), fplot(), plot())

• Formatting of 2D plots

• 3D plotting (surf(), mesh(), plot3())

• Formatting of 3D plots

• Creating multiple plots

2EGR 102 - Fall 2018

Plotting in MATLAB

• Like with Excel, MATLAB has the capabilities to produce several types of figures with proper coding

• Can produce 2D & 3D plots.

• There are several commands that can be used to create 2D plots in MATLAB

• ezplot()

• fplot()

• plot()

• Can also create multiple figures, overlays, and subplots

3EGR 102 - Fall 2018

2D Plotting in MATLABezplot()

• This is the most basic plotting command in MATLAB.

• All that is needed is an expression as an equation

• ezplot automatically produces a properly formatted figure showing the general shape/trend of that expression

• Does not use actual data, however, so it is very limited in regards of actual information that it can provide

4EGR 102 - Fall 2018

2D Plotting in MATLABezplot()

• The only entry needed is an equation

• Should be entered with all operators

• Use apostrophes to denote it as a character string (shows up in purple text)

• MATLAB automatically labels and titles the plot based on the equation

Title is equation being shown

Lab

eled

on

ly if

dep

end

ent

vari

able

d

efin

ed

Label is independent variable

5EGR 102 - Fall 2018

2D Plotting in MATLABfplot()

• Very similar in purpose and appearance to ezplot()

• The differences are:

• You can set the limits/boundaries for which part of the expression is shown

• Accepts expressions, functions, and function handles

fplot(Function,[Limits])

Ex:Function Handle → fh=@sin(x);

fplot(fh,[-3,3])

6EGR 102 - Fall 2018

2D Plotting in MATLABplot()

• The user defines the data to be used

plot(A,B,‘format’)

• The format can be changed with linespec commands. linespecis short for line specifications and allows for the appearance of the figure to change• By default, produces a solid blue line

• Can change line color, line type, and add data markers

• Can also use plot tools to make temporary changes to a figure’s appearance

• For more detailed information, look up linespec in MATLAB’s helpfeature

7EGR 102 - Fall 2018

2D Plotting in MATLABplot()

Plot() is more versatile and customizable than ezplot()

•Plot() let’s us choose our variables. Here they are called x (independent, horizontal axis) and y (dependent, vertical axis)

•You can change which variables are displayed very simply within the plot() command.

8EGR 102 - Fall 2018

•Plot() also gives you some control over the appearance of the plot.

•Either linespec (the third entry in plot, see the arrow) or plot tools (circled in red on the figure) can be used

•Linespec will always be shown as it is part of the code. Look up linespec in MATLAB Help to see all of your options. This example made a green line with point markers.

•Plot tools are temporary. They change the figure’s appearance but are lost once closed unless they are added to the code

9

2D Plotting in MATLAB Linespec Options

EGR 102 - Fall 2018

2D Plotting in MATLABFormatting

• The linespec feature allows for the appearance to change but does not include formatting

• Recall that proper formatting requires a title and labels. These can be added with:

• title(‘Plot_Name’)

• xlabel(‘Independent_Name’)

• ylabel(‘Dependent_Name’)

• Make sure to use character strings contained inside a set of apostrophes

10EGR 102 - Fall 2018

2D Plotting in MATLABFormatting

• There are other features that can also be added but may not always be required

• A legend can be added using

legend(‘series1’,’series2’,…)

• Each series is a different data set. Enter these in the same order as the data sets were entered

• A grid can be added by using grid on

11EGR 102 - Fall 2018

2D Plotting in MATLABFormatting

•The plot isn’t very useful without proper annotation

•The xlabel(), ylabel(), and title() commands were used to give proper annotation and context to the figure

•These commands should come immediately after the plot() commandNow the plot actually tells us something!

12EGR 102 - Fall 2018

3D Plotting in MATLAB

• There are several commands that can be used to create 3D plots in MATLAB

• surf()

• mesh()

• contour() or contourf()

• plot3()

13EGR 102 - Fall 2018

3D Plotting in MATLABData

• 3D plotting considers a third dimension or variable when producing a figure• This can either be entered as a single array or as an array with 2

associated vectors.

• As an array, the number of rows and columns are used as the first two variables, and then the third variable is the actual values in the array

• As a 2 vector 1 array system uses values form the vectors as the planar values and the array as the height.

3D_command(A) or 3D_command(a,b,c)

• This new third dimension can be labeled using

zlabel(‘Name’)

14EGR 102 - Fall 2018

3D Plotting in MATLAB surf() & mesh()

• These commands develop a 3D image that has a layer that conforms to the data provided• One is a solid surface and the other is a grid or mesh hence their

names

• Each point is a combination of the provided coordinates• For an array, the intersection is the row and column number and the

height is the value at that combination

• For the two vector one array system, the first two vectors determine the coordinate and the array gives the height

15EGR 102 - Fall 2018

3D Plotting in MATLAB surf() & mesh()

•Surf() and mesh() are very similar in appearance. Difference is whether it is a solid surface or a mesh (hence their names).

•Notice the difference in axis values•Surf is plotting an array. It’s x and y are the number of rows and columns•Mesh is using two vectors and an array. It shows the actual values corresponding to the z axis values

16EGR 102 - Fall 2018

3D Plotting in MATLABFormatting

•Like in 2D plotting, these plots need to be properly annotated in order to actual convey information•Title(), xlabel(), and ylabel() all work the same as before•A third label, zlabel(), is now included to account for the third dimension

17EGR 102 - Fall 2018

3D Plotting in MATLAB plot3()

• plot3() produces a line in 3D space

• It is recommended that three vectors are used as the inputs for this command

• Depending on circumstances, this could be advantageous compared to the other plots

• One such case would be if the relationship between variables can be observed instead of looking at the result

• This also means that three independent variables can be considered instead of only two.

18EGR 102 - Fall 2018

3D Plotting in MATLAB plot3()

•Plot3() produces a 3D line plot•It is often used to display the relationship between two or three functions that share an independent variableoThis plot shows 2 functions and the variableoThe next slide is 3 functions without showing the variable•The variable is often placed on the z-axis to allow for easier interpretation

19EGR 102 - Fall 2018

3D Plotting in MATLAB plot3()

•Because a third axis is being plotted, the zlabel() command needs to be used•It is also often helpful to include a grid with these plots. This is done by using the command grid on•With three functions plotted, the value of the variable is not shown. Instead, it is what all of the functions are equal to that makes the coordinates

20EGR 102 - Fall 2018

Contour Plotting in Matlabcontour()/contourf() Commands• Contour plots are a 2D image that provide 3D information

• Each plot contains contour lines that represent different heights, or the third dimension

• The difference between the commands is how this information is presented• contour() only provides the contour lines

• contourf() provides shading between the contour lines that can help with interpretation

• Because the heights are color-coded, a colorbar is needed to interpret the figures

• Type colorbar following each contour() or contourf() command to add

one to each figure

• zlabel() does not need to be used with these figures

21EGR 102 - Fall 2018

Contour Plotting in Matlabcontour()/contourf()

•As was the case with surf() and mesh(), contour() and contourf() are very similar•The difference is that contourf() if “filled”•Instead of adding a third axis, contour plots portray the third dimension as contour lines like a topographic map

•The same data is shown here as was plotted by surf() and mesh()•The number of contour lines can be controlled in the command. The fourth entry is the number of contour lines. Notice that the contourf() plot has twice the contour lines that the contour() plot does

22EGR 102 - Fall 2018

Contour Plotting in MatlabFormatting

•Contour plots only need to use xlabel(),ylabel(), and title() since there is not a third axis•We need a colorbar for contour plots•This can be done by typing colorbar in with the other formatting commands•The contour() example has no colorbar and cannot be interpreted whereas the contourf() example clearly shows what the contours represent

23EGR 102 - Fall 2018

Creating Multiple Plots

• There are situations where multiple figures are needed at a time• By default, overwrites the previous plotting command unless told not

to. There are several ways to avoid this

• New figures can be added by typing figure between plot commands

• Overlay plots can be developed in two ways”• Enter multiple data sets in one plot command

plot(A1,B1,’format1’,A2,B2,’format2’,…)• Use the hold on command. This will include every plot command after

on one figure. Use hold off to stop.

24EGR 102 - Fall 2018

Creating Multiple PlotsNew Figures

•By default, subsequent plot commands overwrite the previous one

•Use the figure command (circled in red) to create multiple figures

25EGR 102 - Fall 2018

Creating Multiple Plots Overlay Plots

•There are two ways to create overlay plots: hold on (see red arrow) and multiple inputs in one plot command (see below)

•Both produce the same figure•A legend should be included with overlay plots. Use legend() and enter the names in the order they appear in the code. MatLab autodetects their linespec features

26EGR 102 - Fall 2018

Creating Multiple PlotsSubplots

• Another method is creating a subplot where multiple panes are created in a figure

• This is done using the command

subplot(#ofrows,#ofcols,#ofpane)• #ofrows and #ofcols are how many rows and columns of panes there

are in total

• #of pane identifies which pane it is in. They are numbered going left to right starting in the top left corner.

• Use this command before every plot you want to include. The only

thing that should change is #ofpane.

27EGR 102 - Fall 2018

Creating Multiple PlotsSubplots

•Each subplot needs its own formatting commands.•Before each plot command, you must use subplot() to include is in the subplot. Enter the # of rows, # of columns, and pane #. Only the pane # should change if it is the same subplot.

28EGR 102 - Fall 2018

Summary

• Plotting in MATLAB

• 2D plotting ( ezplot(), fplot(), plot())

• Formatting of 2D plots

• 3D plotting (surf(), mesh(), plot3())

• Formatting of 3D plots

• Creating multiple plots

29EGR 102 - Fall 2018