0% found this document useful (0 votes)
9 views

Lecture 5

The document outlines the computational methods and modeling techniques for engineering applications, specifically focusing on plotting in MATLAB. It provides instructions on generating plots, labeling axes, saving figures, and improving plot aesthetics, along with practical questions for application. Key features include the use of commands like 'fplot', 'subplot', and 'legend' to enhance data visualization.

Uploaded by

vileji2030
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture 5

The document outlines the computational methods and modeling techniques for engineering applications, specifically focusing on plotting in MATLAB. It provides instructions on generating plots, labeling axes, saving figures, and improving plot aesthetics, along with practical questions for application. Key features include the use of commands like 'fplot', 'subplot', and 'legend' to enhance data visualization.

Uploaded by

vileji2030
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Computational Methods and Modeling for

Engineering Applications
(GENG-8030)

Department of Electrical and Computer Engineering,


University of Windsor, ON, Canada,

By:
Dr. Hassanzadeh

Lecture number:
(5)

Winter 2023
Nomenclature for a typical xy plot:

Computational methods and modeling for engineering applications (GENG-8030) 2


Generating a plot:
Example: The following MATLAB session plots , where represents the
height of a rocket after launch, in miles, and is the horizontal (downrange)
distance in miles.

Rockect height vs distance


4

3.5

3
Height (miles)

2.5

1.5

0.5

0
0 10 20 30 40 50 60
Distance (miles)

Computational methods and modeling for engineering applications (GENG-8030) 3


Obtaining a hard copy of the plot:
The plot will appear in the Figure window. You can obtain a hard copy of the
plot in several ways:
1. 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.
2. Type print at the command line. This command sends the current plot
directly to the printer.

Computational methods and modeling for engineering applications (GENG-8030) 4


Closing the figure window:
When you are finished with the plot, close the figure window by selecting
Close from the File menu in the Figure window.

Note that using the Alt-Tab key combination in Windows-based systems will
return you to the Command window without closing the figure window.

If you do not close the window, it will not reappear when a new plot command
is executed. However, the figure will still be updated.

Computational methods and modeling for engineering applications (GENG-8030) 5


Requirements of a correct plot:
The following list describes the essential features of any plot:
1. 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), 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 intervals
—not too sparse, but not too dense—with a spacing that is easy to
interpret and interpolate. For example, use 0.1, 0.2, and so on, rather than
0.13, 0.26, and so on.
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.
Computational methods and modeling for engineering applications (GENG-8030) 6
Requirements of a correct plot:
6. 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.
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.

Computational methods and modeling for engineering applications (GENG-8030) 7


The grid and axis commands:
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.

You can use the axis command to override the MATLAB selections for the
axis limits. The basic syntax is 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.

Computational methods and modeling for engineering applications (GENG-8030) 8


The fplot function:
The fplot function chooses a small enough spacing to display the function’s full behavior.
You can run it in two ways: using syms or @(x) as follows

-0.6

-0.8

-1

-1.2

-1.4

-1.6

-1.8

-2

-2.2

-2.4

1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2

Computational methods and modeling for engineering applications (GENG-8030) 9


Advantage of fplot
The same function plotted with the plot command. A spacing of 0.01 misses
some oscillations.

-0.5

-1

-1.5

-2

-2.5

-3
1 1.2 1.4 1.6 1.8 2

Computational methods and modeling for engineering applications (GENG-8030) 10


Practice Question 1:
Q1- Plot the equation
.
Title = Rocket height versus distance
X axis label= Distance (miles)
Y axis label=Height (miles)

Rocket height versus distance


3.5

2.5
Height (miles)

1.5

0.5

0
0 5 10 15 20 25 30 35
Distance (miles)
Computational methods and modeling for engineering applications (GENG-8030) 11
Practice Question 2:
Q2- Use the fplot command to plot and investigate the function

. How many values of x are needed to obtain the same plot using the plot
command?

1.5

0.5

-0.5

-1

-1.5

0 1 2 3 4 5 6

Computational methods and modeling for engineering applications (GENG-8030) 12


Practice Question 3:
Q3- Plot the imaginary part versus the real part of the function for . Choose
enough points to obtain a smooth curve. Label each axis and put a title on the
plot. Use the axis command to change the tick-label spacing.

n
Plot of (0.2+0.8i) for n=[0,20]
1

0.5
Imaginary

-0.5

-1
-1 -0.5 0 0.5 1
Real
Computational methods and modeling for engineering applications (GENG-8030) 13
Saving figures:
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.

Computational methods and modeling for engineering applications (GENG-8030) 14


Hints for improving plots:
The following actions, while not required, can nevertheless improve the
appearance of your plots:

1. Start scales from zero whenever possible. This technique prevents a false
impression of the magnitudes of any variations shown on the plot.

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. Space
tick marks as close as is useful, but no closer. If the data is given monthly
over a range of 24 months, 48 tick marks might be too dense, and also
unnecessary.

3. Minimize the number of zeros in the data being plotted. For example, use
a scale in millions of dollars when appropriate, instead of a scale in
dollars with six zeros after every number.

Computational methods and modeling for engineering applications (GENG-8030) 15


Hints for improving plots:
4. Determine the minimum and maximum data values for each axis before
plotting the 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.

For 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 allows you to use 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 photocopies.

6. Do not put many curves on one plot, particularly if they will be close to
each other or cross one another at several points.

Computational methods and modeling for engineering applications (GENG-8030) 16


Hints for improving plots:
7. Use the same scale limits and tick spacing on each plot if you need to
compare information on more than one plot.

Computational methods and modeling for engineering applications (GENG-8030) 17


Plotting functions:
Other useful plotting functions are title and gtext. These functions place
text on the plot. Both accept text within parentheses and single quotes, as
with the xlabel function.

The title function places the text at the top of the plot.

The gtext function places the text at the point on the plot where the cursor is
located when you click the left mouse button.
1

Sin(x)
0.5

Y
-0.5

-1
0 1 2 3 4 5 6 7
X

Computational methods and modeling for engineering applications (GENG-8030) 18


Subplots:
You can use the subplot command to obtain 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 pth pane.

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).

Computational methods and modeling for engineering applications (GENG-8030) 19


Application of the subplot command:

Computational methods and modeling for engineering applications (GENG-8030) 20


Practice Question 4:
The following script file created a figure, which shows the plots of the
functions for , and and for .

Computational methods and modeling for engineering applications (GENG-8030) 21


Data markers and line types:
To plot y versus x with a solid line and u versus v with a dashed line, type
plot(x,y,u,v,‘--’), where the symbols ‘--’ represent a dashed line.

To plot y versus x with asterisks (*) connected with a dotted line, you must
plot the data twice by typing plot(x,y,‘*’,x,y,‘:’).

Computational methods and modeling for engineering applications (GENG-8030) 22


Data markers and line types:
To plot y versus x with green asterisks (*) connected with a red dashed line,
you must plot the data twice by typing plot(x,y,‘g*’,x,y,‘r--’).

Computational methods and modeling for engineering applications (GENG-8030) 23


Specifiers for data markers, line types and colors:

Data markers† Line types Colors


Dot (.) . Solid line — Black k
Asterisk (*) * Dashed line –– Blue b
Cross (´) Dash-dotted line –. Cyan c
Circle (◦) ◦ Dotted line …. Green g
Plus sign () Magenta m
Square ( ▫ ) s Red r
Diamond ( ◊ ) d White w
Five-pointed star (w) p Yellow y

Computational methods and modeling for engineering applications (GENG-8030) 24


Labeling curves and data:
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 string you selected.

4
sinh(x)
3.5 tanh(x)

3
Hyperbolic functions

2.5

1.5

0.5

0
0 0.5 1 1.5 2
X

Computational methods and modeling for engineering applications (GENG-8030) 25


Application of hold command:

Computational methods and modeling for engineering applications (GENG-8030) 26


Application of hold command:

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7

Computational methods and modeling for engineering applications (GENG-8030) 27


Practice Question 5:
Q4- Pick a suitable spacing for and then use the subplot command to plot the
following functions

0.5

z
-0.5

-1
0 1 2 3 4 5 6 7 8
t
12

11

10
u

8
-8 -6 -4 -2 0 2 4 6 8
v

Computational methods and modeling for engineering applications (GENG-8030) 28


Practice Question 6:
Q5- Plot the following two data sets on the same plot. For each set, x = 0, 1, 2, 3,
4, 5. Use a different data marker for each set. Connect the markers for the first set
with solid lines. Connect the markers for the second set with dashed lines. Use a
legend, and label the plot axes appropriately. The first set is y = 11, 13, 8, 7, 5, 9.
The second set is y = 2, 4, 5, 3, 2, 4.
14
y1
y2
12

10

y1, y2
8

2
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
x

Computational methods and modeling for engineering applications (GENG-8030) 29


Practice Question 7:
Q6- Plot and on the same plot for . Use a solid line type for each, the gtext
command to label the sinh x curve, and the curve. Label the plot axes
appropriately.

3.5

2.5

2
y

1.5

1 0.5exp(x)

0.5 sinh(x)

0
0 0.5 1 1.5 2
x

Computational methods and modeling for engineering applications (GENG-8030) 30


Polar plots:
plot using polar coordinates , in radians, versus the radius .
Obtain a polar plot of .
Polar plot of y=2x
90 15
120 60
10
150 30
5

180 0

210 330

240 300
270

Please read the polar plots concept!.

Computational methods and modeling for engineering applications (GENG-8030) 31


Practice Question 7:
Q7- Plot the following functions using axes that will produce a straight-line

,
plot. The power function is , and the exponential function is .

101
y=2*x.-0.5 y=10. 1-x

101

100

y
10-1

100 10-2
10-2 100 0 1 2 3
x x

Computational methods and modeling for engineering applications (GENG-8030) 32


Practice Question 8:
Q8- Plot the function with a tick spacing of on the axis and on the y axis.

3
y=8*x.
8

0
y

-2

-4

-6

-8
-1 -0.75 -0.5 -0.25 0 0.25 0.5 0.75 1
x

Computational methods and modeling for engineering applications (GENG-8030) 33


Specialized plot commands:
Command Description
bar(x,y) Creates a 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’) Produces a 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) Produces a stairs plot of y versus x.


stem(x,y) Produces a stem plot of y versus x.

Computational methods and modeling for engineering applications (GENG-8030) 34


Specialized plot commands:
1 1

0.8

0.6
0.5
0.4

0.2

0 0

-0.2

-0.4
-0.5
-0.6

-0.8

-1 -1
-1 -0.5 0 0.5 1 -1 -0.5 0 0.5 1
1

0.5

-0.5

-1
-1 -0.5 0 0.5 1
Computational methods and modeling for engineering applications (GENG-8030) 35
Plotting implicit functions:
MATLAB provides the function fimplicit(f) to plot the implicit function
defined by the equation f(x,y) 0 over the default interval [5 5] for x and y.
For example, to plot the hyperbola defined by x^2 y^2 1 0 over the default
interval of [5 5], you type

If the limits for x are [−2 2] and the limits for y are [−4 4], you would type

4
4
3
3
2
2
1
1

0 0

-1 -1
-2
-2
-3
-3
-4

-4
-5 0 5 -2 -1 0 1 2

Computational methods and modeling for engineering applications (GENG-8030) 36


Three dimensional line plots:
The following program uses the plot3 function to generate the spiral curve.

Computational methods and modeling for engineering applications (GENG-8030) 37


Surface plots:
The following session shows how to generate the surface plot of the function
for 2 x 2 and 2 y 2, with a spacing of 0.1.

A mesh is a representation of a larger geometric domain by smaller discrete


cells.

Computational methods and modeling for engineering applications (GENG-8030) 38


Surface plots of implicit functions:
MATLAB provides the function fimplicit3(f) to plot the three-dimensional
implicit function defined by the equation

over the default interval . You can specify the interval with the syntax .
For example, to plot the upper half of the hyperboloid
you specify the interval as for z as , and for x and y, use the default interval ,
as follows.

Computational methods and modeling for engineering applications (GENG-8030) 39


Other three-dimensional plotting functions:
Function Description
surf(x,y,z) Creates a shaded 3D mesh surface plot.
surfc(x,y,z) Same as surf but draws contours under the surface.
waterfall(x,y,z) Same as mesh but draws mesh lines in one direction only.

Computational methods and modeling for engineering applications (GENG-8030) 40


Practice Question 9:
Plots of the surface created with the mesh function and its variant forms: a)
surf, b) surfc, c) waterfall, d) meshc,

Computational methods and modeling for engineering applications (GENG-8030) 41


Practice Question 10:
Q9- Obtain the plot of the following implicit function, known as the
Ampersand curve. Use the axis equal command.

axis equal sets the aspect ratio so that the data units are the same in every
direction.

Ampersand curve

0.5

-0.5

-1

0 0.5 1 1.5
Computational methods and modeling for engineering applications (GENG-8030) 42
Practice Question 11:
Q10- Use plot3 and fplot3 to plot the 3-D line plot described by for .

Computational methods and modeling for engineering applications (GENG-8030) 43


Practice Question 12:
Q11-Use mesh, fmesh, contour, and fcontour to create a surface plot and a
contour plot of the function .

Computational methods and modeling for engineering applications (GENG-8030) 44


Practice Question 13:
Q12- Use the fimplicit3 function to create a surface plot of the function

Computational methods and modeling for engineering applications (GENG-8030) 45

You might also like