0% found this document useful (0 votes)
25 views27 pages

ENGG100 Fall2023 Lab02 Slides

This document provides an introduction to plotting techniques in numerical analysis. It discusses how to create basic two-dimensional x-y plots and subplots, and how to add titles, labels, grids and adjust axis scaling. It also covers plotting multiple lines by holding plots, specifying line styles and colors, and adding legends and text annotations to plots. The document uses examples with trigonometric functions to demonstrate various plotting features in MATLAB.

Uploaded by

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

ENGG100 Fall2023 Lab02 Slides

This document provides an introduction to plotting techniques in numerical analysis. It discusses how to create basic two-dimensional x-y plots and subplots, and how to add titles, labels, grids and adjust axis scaling. It also covers plotting multiple lines by holding plots, specifying line styles and colors, and adding legends and text annotations to plots. The document uses examples with trigonometric functions to demonstrate various plotting features in MATLAB.

Uploaded by

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

Introduction to

Dr. Qasim Imtiaz


Assistant Professor
Department of Chemistry and Chemical Engineering
Lahore University of Management Sciences

ENGG100 – Fall 2023 1


Learning Objectives

Numerical
Plotting
Techniques

• Two-Dimensional Plots • Curve Fitting

• Subplots • Interactive Fitting Tools

ENGG100 – Fall 2023 2


Plotting
ENGG100 – Fall 2023 3
What’s Going On
in This Graph?

Large tables of data are difficult to


interpret. Scientists and engineers use
graphing techniques to make the
information easier to understand.

With a graph, it is easy to identify trends,


pick out highs and lows, and isolate data
points that may be measurement or
calculation errors.

ENGG100 – Fall 2023 4


Using the Clausius–Clapeyron Equation
The Clausius–Clapeyron equation can be used to find the saturation vapor pressure of water in the
atmosphere for different temperatures. The saturation water vapor pressure is useful to meteorologists
because it can be used to calculate relative humidity, an important component of weather prediction,
when the actual partial pressure of water in the air is known.
The Clausius-Clapeyron equation is
𝑃0 ∆𝐻𝑣 1 1
𝑙𝑛 = × −
6.11 𝑅𝑎𝑖𝑟 273 𝑇
where, P0 = saturation water-vapor pressure, in mbar, at temperature T,
∆Hv = latent heat of vaporization for water, 2.453 × 106 J/kg,
Rair = gas constant for moist air, 461 J/kg, and
T = temperature in kelvins (K).
Find the saturation vapor pressure at temperatures from −60°F or higher than 120°F, using the
Clausius–Clapeyron equation, and present the results graphically.

ENGG100 – Fall 2023 5


Simple x-y Plots
• plot(x,y) creates a 2-D line plot of the data in y versus the corresponding
values in x.
• To plot a set of coordinates connected by line segments, specify x and
y as vectors of the same length.
• To plot multiple sets of coordinates on the same set of axes, specify at
least one of x or y as a matrix.

• The area plot is quite similar to the simple x-y plot, the only exception
being that the area under the curve is filled in.
• The code area(x, y) results in the area plot.

ENGG100 – Fall 2023 6


Titles, Labels, and Grids
• Good engineering practice requires including axis labels and a title in our
plot.
plot(x, y), title(‘Clausius–Clapeyron Equation’), xlabel(‘Temperature, F’)
ylabel(‘Saturation Vapor Pressure, mbar'), grid on

• The text color changes to red when you enter a single quote ('). This alerts
you that you are starting a string (a list of characters enclosed by single
quotes).

• Select the docking arrow underneath the exit icon in the upper right-hand
corner of the figure window.
ENGG100 – Fall 2023 7
Creating Multiple Plots
• Execute the following commands: • Use sections to execute your
x = 0:pi/100:2*pi; program interactively, one section
y1 = cos(x*4); at a time - ensuring each section
generates no more than one figure.
plot(x,y1)
y2 = sin(x); • Run your entire program using the
pause command to temporarily
figure y2)
plot(x, halt the execution so that you can
plot(x, y2) examine the figure.
• Create multiple figure windows
using the figure function.

ENGG100 – Fall 2023 8


Plots with More than One Line
• We can layer plots on top of one • Another way to create a graph with
another using the hold on multiple lines is to request both
command: lines in a single plot command:
x = 0:pi/100:2*pi; x = 0:pi/100:2*pi;
y1 = cos(x*4); y1 = cos(x*4);
plot(x,y1) y2 = sin(x);
y2 = sin(x); plot(x, y1, x, y2)
hold on
plot(x, y2)
hold off

ENGG100 – Fall 2023 9


Plots with More than One Line
• If plot is called with two • Execute the following command:
arguments, one a vector and the plot(Y)
other a matrix, MATLAB®
successively plots a line for each
row in the matrix: • If the plot function is called with a
Y = [y1; y2]; single matrix argument, MATLAB®
plot(x, Y) draws a separate line for each
column of the matrix. The x-axis is
labeled with the row index vector,
1:k, where k is the number of rows
in the matrix. This produces an
evenly-spaced plot, sometimes
called a line plot.

ENGG100 – Fall 2023 10


Line, Color, and Mark Style
• You can change the appearance of your plots by selecting user-defined
line styles and line colors, and by choosing to show the data points on the
graph with user-specified mark styles.
x = 0:pi/100:2*pi;
y1 = cos(x*4);
y2 = sin(x);
plot(x, y1, ‘:ok’, x, y2, ‘--xr’)

• The indicators are listed inside a string, denoted with single quotes. The
order in which they are entered is arbitrary and does not affect the output.

ENGG100 – Fall 2023 11


Line, Color, and Mark Style
Line, Mark, and Color Options
Line Type Indicator Point Type Indicator Color Indicator • The plot command offers
additional options to control
solid - point . blue b how the plot appears. You
dotted : circle o green g need to specify the
property name and value to
dash-dot -. x-mark x red r control these properties.
dashed -- plus + cyan c
(none) no line star * magenta m • Explore the possible
square s yellow y combinations by searching
the help documentation:
diamond d black k doc Chart Line Properties
pentagram p white w
hexagram h
triangle up ^
triangle left <
ENGG100 – Fall 2023 12
Axis Scaling and Annotating Plots
Axis Scaling and Annotating Plots
When the axis function is used without inputs, it freezes the axis at
axis the current configuration. Executing the function a second time
returns axis control to MATLAB®.
The input to the axis command must be a four-element vector that
axis (v) specifies the minimum and maximum values for both the x- and y-
axes—for example, [xmin, xmax, ymin, ymax]
axis equal Forces the scaling on the x- and y-axis to be the same.
Allows you to add a legend to your graph. The legend shows a
legend(‘string1’, ‘string2’, etc)
sample of the line and lists the string you have specified.
text (x_coordinate, Allows you to add a text box to the graph. The box is placed at the
y_coordinate, ‘string’) specified x- and y-coordinates and contains the string value specified.
Similar to text. The box is placed at a location determined
gtext(‘string’)
interactively by the user by clicking in the figure window.
ENGG100 – Fall 2023 13
Subplots
• The subplot command allows x = 0:pi/100:2*pi;
users to create multiple graphs in y1 = cos(x*4);
the same figure window. subplot(2, 1, 1)
plot(x, y1)
• The function subplot(m, n, p) splits y2 = sin(x);
the figure into an m × n matrix. the subplot(2, 1, 2)
variable p identifies the portion of plot(x, y2)
the window where the next plot will
be drawn.

ENGG100 – Fall 2023 14


Let’s Practice

ENGG100 – Fall 2023 15


Numerical Techniques
ENGG100 – Fall 2023 16
Curve Fitting
• When we measure something, we don’t gather data at every possible data
point. Using a numerical technique, we can estimate the value of y at
values of x where we didn’t take a measurement.

• Interpolation: A technique for estimating an intermediate value based on


nearby values.

• Curve Fitting: A technique for modeling data with an equation. If we know


something about the underlying relationship between x and y, we may be
able to determine an equation based on those principles.
The Ideal Gas Law
PV = nRT • When real gases deviate from this
simple relationship:
• We can try to understand the
• Underlying assumptions: physics of the situation and adjust
• All the molecules in a gas collide the equation accordingly.
elastically.
• The molecules don’t take up any n2 a
P+ 2 V − nb = nRT
room in their container. V

• We take the data and model them


• Neither assumption is entirely empirically.
accurate, so the ideal gas law is a
good approximation of reality.
Eyeballing a Set of Data
• Plot the following data on graph Linear Model of Some Data
paper and draw a straight line 15
through the data points to get a
rough model of the data’s behavior. 13

x=012345 11

y = 15 10 9 6 2 0 9

y-axis
7

• Find the equation of the line. 5

• This process is called “eyeballing” 1

– no calculations were done, but it -1


looks like a good fit. -1 0 1 2
x-axis
3 4 5 6
Linear Regression
• A technique for modeling a set of x = 0:5;
data as a straight line. y = [15, 10, 9, 6, 2, 0];
y_calc = m*x + c;
• Uses least squares fit to compare sum_1 = sum((y-y_calc).^2)
how well different equations model
the data. polyfit (x, y, 1)
y_best = m_best*x + c_best;
• In this technique, the differences sum_2 = sum((y-y_best).^2)
between the actual and calculated plot(x, y, 'o', x, y_best)
values (called the residuals) are
squared and added together.
Linear Regression through Zero
• Modeling of data should be based not only on the data collected but also
on a physical understanding of the process.
x = 0:10:50; Wind Resulting
Velocity, x Force, y
y = [0, 24, 38, 64, 82, 90]; 0 0
plot(x, y, ‘o’) 10 24
hold on 20 38
30 64
polyfit (x, y, 1)
40 82
y_best = m_best*x + c_best; 50 90
plot(x, y_best)
Linear Regression through Zero
• We can force the model through zero with left
division.
m_new = x’\y’ Wind Resulting
Velocity, x Force, y
y_new = m_new*x; 0 0
plot(x, y_new) 10 24
hold off 20 38
30 64
40 82
• This technique requires the x and y vectors to 50 90
be expressed as columns.
Polynomial Regression
• Taylor’s theorem tells us that any smooth function can be approximated as
a polynomial, so a common approach is to fit data with a higher-order
polynomial of the form:
y = a1xn + a2xn-1 + a3xn-2 + … + anx + an+1

• Polynomial regression is used to get the best fit by minimizing the sum of
the squares of the deviations of the calculated values from the data.

• The polyfit function allows us to do this easily in MATLAB®.


Polynomial Regression
x = 0:5; f3 = polyfit (x, y, 3)
y = [15, 10, 9, 6, 2, 0]; y3 = a3*x.^3 + a2*x.^2 + a1*x + a0;
f1 = polyfit (x, y, 1) sum3 = sum((y-y3).^2)
y1 = a1*x + a0;
sum1 = sum((y-y1).^2) plot(x, y, 'o', x, y1, x, y2, x, y3)

f2 = polyfit (x, y, 2) Using the subplot function, plot


y2 = a2*x.^2 + a1*x + a0; smooth curves for first-order,
second-order, and third-order
sum2 = sum((y-y2).^2) models.
The Polyval Function
• The polyfit function returns the coefficients of a polynomial that best fits
the data, based on a regression criterion.

• The polyval function evaluates the corresponding polynomial at each


point in x without entering those coefficients into a MATLAB® expression
for the polynomial.

• The polyval function requires two inputs: the first is a coefficient array and
the second is an array of x-values for which we would like to calculate new
y-values.
The Polyval Function
clear subplot(1,3,1)
x = 0:5; y = [15, 10, 9, 6, 2, 0]; plot(x,y,'o', x_smooth,y3)
x_smooth = 0:0.1:5; axis([0,6,−5,15])
f1 = polyfit (x, y, 1) subplot(1,3,2)
y1 = polyval(f1,x_smooth); plot(x,y,'o', x_smooth,y4)
axis([0,6,−5,15])
y2 = polyval(polyfit (x, y, 2), x_smooth) subplot(1,3,3)
y3 = polyval(polyfit (x, y, 3), x_smooth) plot(x,y,'o', x_smooth,y5)
y4 = polyval(polyfit (x, y, 4), x_smooth) axis([0,6,−5,15])
y5 = polyval(polyfit (x, y, 5), x_smooth)
Let’s Practice

ENGG100 – Fall 2023 27

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy