Chp6 MATLAB Fcn Discovery Engr/Math/Physics 25 Bruce Mayer, PE

advertisement
Engr/Math/Physics 25
Chp6 MATLAB
Fcn Discovery
Bruce Mayer, PE
Registered Electrical & Mechanical Engineer
BMayer@ChabotCollege.edu
Engineering/Math/Physics 25: Computational Methods
1
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Learning Goals
 Create “Linear-Transform” Math Models
for measured Physical Data
• Linear Function → No Xform
• Power Function → Log-Log Xform
• Exponential Function → SemiLog Xform
 Build Math Models for Physical Data
using “nth” Degree Polynomials
 Use MATLAB’s “Basic Fitting” Utility to
find Math models for Plotted Data
Engineering/Math/Physics 25: Computational Methods
2
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Learning Goals
 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 (r2)
• Scale Data if Needed
– Creates more meaningful spacing
Engineering/Math/Physics 25: Computational Methods
3
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Learning Goals cont
 Build Math Models for Physical Data
using “nth” 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
Engineering/Math/Physics 25: Computational Methods
4
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Function Discovery

Physical Processes for some Response
(OutPut), y, as Resulting from some
Excitation (InPut), x, can many times be
approximated by 3 Functions
1. The LINEAR function: y = mx + b
– Produces a straight line with SLOPE a of m and an
INTERCEPT of b when plotted on rectilinear axes
2. The POWER function: y = bxm
– gives a straight line when plotted on log-log axes
3. The exponential function y = b·10mx or y = b·emx
– Yields a straight line when plotted on a semilog plot
with logarithmic y-axis
Engineering/Math/Physics 25: Computational Methods
5
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Linear Transformations
 For a Linear
Function We can
Easily Find the
Slope, m, and
y-Intercept, b
y
y  mx  b
y
m
x
b
x
Engineering/Math/Physics 25: Computational Methods
6
PwrFcn  y  bx
m
 Take ln of both sides
ln y  ln bx m  ln b  ln x m
ln y  mln x   ln b
y
x
 Transform the
Power Function to
Line-Like Form
Yp
Xp
Bp
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Power Function Xform
 Thus the Power
Function Takes the
Form:
y  bx m  ln b  ln x m
ln y  mln x   ln b
 Yp  mX p  B p
 If The Data Follows
the Power Function
Then a Plot of
Engineering/Math/Physics 25: Computational Methods
7
ln y vs. ln x
 Yields a Staight Line
 So if we suspect a
PwrFcn, Plot the
Data in Log-Log
Form
 Example:
y  13 x
7  x  91
1.73
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Power Function y = 13x1.73
 Rectlinear Plot
Power Function
Power Function
4
3.5
 Log-Log Plot
x 10
10.5
10
3
9.5
2.5
9
8.5
y
ln(y)
2
1.5
8
7.5
7
1
6.5
0.5
6
0
0
10
20
30
40
50
60
70
80
90
x
Engineering/Math/Physics 25: Computational Methods
8
100
5.5
1.5
2
2.5
3.5
3
ln(x)
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
4
4.5
5
y = 13x1.73 by MATLAB loglog
5
y = 13x 1.73
10
>> x =
linspace(7, 91,
500);
>> y =
13*x.^1.73;
4
10
y
>> loglog(x,y),
xlabel('x'),
ylabel('y'),
title('y =
13x^1^.^7^3'),
grid
3
10
2
10
0
10
1
10
x
Engineering/Math/Physics 25: Computational Methods
9
2
10
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Exponential Function Xform
 Recall the General
form of the
Exponential Fcn
y  be
mx
 Again taking the ln
ln y  ln be
 ln b  mx ln e
ln y  mx  ln b
mx
Engineering/Math/Physics 25: Computational Methods
10
 In This Case Let
ln y  Ye
ln b  Be
 Then the Xformed
Exponential Fcn
Ye  mx  Be
 A SemiLog (log-y)
plot Should Show as
a Straight Line
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Exponential Fcn Plot
 Plot v  115e
0.61t
 ln v  0.61t  ln 115
 SemiLog Plot
 Rectlinear Plot
Exponential Function
Exponential Function
120
5
ln(115) = 4.79
4
100
3
80
ln(v)
v
2
60
1
40
0
20
0
-1
0
1
2
3
4
5
6
7
8
t
Engineering/Math/Physics 25: Computational Methods
11
9
10
-2
0
1
2
3
4
5
6
7
t
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
8
9
10
V = 115e-0.61t by MATLAB semilogy
v = 115e-0.61t
3
10
>> t =
linspace(0, 10,
500);
2
10
v
>> v =
115*exp(0.61*t);
1
10
semilogy(t,v),
xlabel('t'),
ylabel('v'),
title('v =
115e^^0^.^6^1^t'),
grid
0
10
-1
10
0
1
2
3
4
5
t
Engineering/Math/Physics 25: Computational Methods
12
6
7
8
9
10
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Steps for Function Discovery
1. Examine the data near the origin.
•
•
The linear function can pass through
the origin only if b = 0
The exponential function (y = bemx) can never
pass through the origin
– as et > 0 (positive) for ALL t; e.g., e−2.7 = 0.0672
– unless of course b = 0, which is a trivial case: y = 0·emx
•
The power function (y = bxm) can pass through
the origin (e.g.; y = 7x3) but only if m > 0 (positive)
as
– As y = bx-m = b/xm → Hyperbolic for negative m
Engineering/Math/Physics 25: Computational Methods
13
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
“Discoverable” Functions
400
25
150
ye  e
x
20
350
y pp  x 2
300
100
250
ypp
ypm
15
ye
y pm  x 2
10
200
150
50
100
5
50
0
-5
0
5
0
-5
x
0
5
0
-5
0
x
x
 In most applications x is NONnegative
Engineering/Math/Physics 25: Computational Methods
14
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
5
Steps for Function Discovery
y  mx  b
2. Plot the data using
rectilinear scales.
•
•
If it forms a straight line, then it can be
represented by the linear function
and you are finished.
Otherwise, if you have data at
x = 0, then
y  bx
m
y  be
mx
a. If y(0) = 0, then try the power function.
b. If y(0)  0, then try the exponential function
c. If data is not given for x = 0,
then proceed to step 3.
Engineering/Math/Physics 25: Computational Methods
15
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Steps for Function Discovery
y  mx  b
3. If you suspect a power function,
then plot the data using log-log
scales.
•
Only a power function will form a
straight line on a log-log plot.
y  bx
m
y  be
mx
4. If you suspect an exponential
function, then plot the data using
the SemiLogy scale.
•
Only an exponential function will form
a straight line on a SemiLog plot.
Engineering/Math/Physics 25: Computational Methods
16
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
SemiLog and LogLog Scales
10
10
10
ye  e
3
10
10
10
10
3
1
10
2
0
10
1
ypm
ye
10
y pm  x 2
1
0
10
10
2
2
2
10
10
y pp  x
x
-1
10
0
-1
10
-2
-3
-5
0
x
5
10
-2
10
-3
10
-2
10
0
10
2
10
-1
-2
10
-2
10
x
0
• Note Change in power Function x-axis scales
– In this case x MUST be POSITIVE
Engineering/Math/Physics 25: Computational Methods
17
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
10
2
Steps for Function Discovery
y  mx  b
5. In function discovery applications,
use the log-log and semilog plots
only to identify the function type,
but not to find the coefficients b
and m.
•
•
y  bx
m
y  be
mx
The reason is that it is difficult to
interpolate on log scales
To Determine Quantities for m & b
Perform the Appropriate
Linearization Transform to plot one of
– ln(y) vs. ln(x) → Power Fcn
– ln(y) vs x → Exponential Fcn
Engineering/Math/Physics 25: Computational Methods
18
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
The polyfit Function

The Command → p = polyfit(x,y,n)
•
This Function Fits a polynomial of degree n to
data described by the vectors x and y, where x is
the independent variable.
• polyfit Returns a row vector p of length n + 1
that contains the polynomial coefficients in order
of descending powers

Note That a FIRST Degree Polynomial
Describes the Eqn of a LINE
•
If w =p1z +p2 then polyfit on data Vectors W & Z
returns: p = ployfit(Z,W,1) = [p1, p2] → [m, b]
Engineering/Math/Physics 25: Computational Methods
19
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Using polyfit For Discovery

polyfit of degree-1 (n = 1) returns the
parameters of a Line
•
•

p1 → m (slope)
p2 → b (intercept)
Thus polyfit can provide m & b for any of the
previously noted
functions AFTER
the appropriate
Linearization
Transform
Engineering/Math/Physics 25: Computational Methods
20
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
m & b by polyfit(x,y,1)
 We need to find an
Eqn for the Vapor
pressure of Ethanol,
C2H5OH, as a fcn of
Temperature
 Find Pv vs T Data
by Consulting
• P. E. Liley and W. R.
Gambill, Chemical
Engineers’ HandBk,
New York, McGrawHill Inc., 1973,
p. 3-34 & 3-54
Engineering/Math/Physics 25: Computational Methods
21
Pv for
Ethanol (Torr)
Temperature
(Kelvins)
760
351.4
400
336.5
200
321.4
100
307.9
60
299
40
292
20
281
10
270.7
5
261
1
241.7
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
m & b by polyfit(x,y,1)cont
Ethanol Vapor Pressure
 As a Starting Point
Make a Rectilinear
Plot
700
600
500
Pv (Torr)
 Since this a Vapor
Pressure we
Suspect an Antoine
or Clapeyron
Relation  Pv ~ em/T
800
400
300
200
100
0
240
260
280
300
T (K)
320
340
>> Pv = [1, 5, 10, 20, 40, 60, 100, 200, 400, 760];
>> T = [241.7, 261, 270.7, 281, 292, 299, 307.9, 321.4, 336.5,
351.4];
>> plot(T,Pv,'x', T,Pv,':'), xlabel('T (K)'), ylabel('Pv
(Torr)'),...
title('Ethanol Vapor Pressure'), grid
Engineering/Math/Physics 25: Computational Methods
22
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
360
Compare LogLog vs SemiLogY
 Log Log Plot
 logY vs linX
Ethanol Vapor Pressure
3
Ethanol Vapor Pressure
3
10
10
2
2
10
Pv (Torr)
Pv (Torr)
10
1
1
10
0
10
2.8
10
0
10
0.5
0.6
10
10
1000/T (1/K)
3
3.2
3.4
3.6
1000/T (1/K)
 Looks like an Exponential in 1/T
• e.g., the Clapeyron eqn
Engineering/Math/Physics 25: Computational Methods
23
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
3.8
4
4.2
m & b by polyfit(x,y,1)cont
 The Plots Looks
Pretty Well
Exponential
 For a 1st-Cut
Assume the
Clapeyron form
1
ln Pv  m   B or
T 
Pv  e m 1 T  B  e B e m 1 T 
 Now Xform
ln Pv  m1 T   B
Ye
x
 Thus Plot
ln(Pv) vs 1/T
Pv  be m 1 T 
Engineering/Math/Physics 25: Computational Methods
24
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Be
m & b by polyfit(x,y,1)cont
Ethanol Vapor Pressure - Clapeyron Plot
 The Command
Session
7
6
>> Ye = log(Pv);
>> x = 1./T
>> plot(x,Ye,'d',
x,Ye,':'), xlabel('1/T
(1/K)'),
ylabel('ln(Pv)
(ln(Torr))'),...
title('Ethanol Vapor
Pressure - Clapeyron
Plot'), grid
Engineering/Math/Physics 25: Computational Methods
25
ln(Pv) (ln(Torr))
5
4
3
2
1
0
2.8
3
3.2
3.4
3.6
1/T (1/K)
3.8
 Nicely Linear →
Clapeyron is OK
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
4
4.2
-3
x 10
m & b by polyfit(x,y,1)cont
 Apply PolyFit to
Find m and B
 To Increase the Sig
Figs displayed for B
these plots are
typically plotted with
x1 = 1000/T
>> p = polyfit(x,Ye,1)
p =
1.0e+003 *
-5.1304
0.0213
>> x1 = 1000./T
>> p1 = polyfit(x1,Ye,1)
 or
p1 =
Ye  Be  mx
-5.1304
21.2512

ln Pv  21.2512  5.13041000 T 
ln Pv  21.3  5130.41 T 
Engineering/Math/Physics 25: Computational Methods
26
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
m & b by polyfit(x,y,1)cont
Ethanol Vapor Pressure - Clapeyron Plot
7
6
ln(Pv) =21.2512-5130.4(1/T)
ln(Pv) (ln(Torr))
5
4
3
2
1
0
2.8
Pv  e
3
21.25125130.4 1 T 
3.2
3.4
3.6
1000/T (1000/K)
Engineering/Math/Physics 25: Computational Methods
27
3.8
4
4.2
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
All Done for Today
Power
and
Exps
Engineering/Math/Physics 25: Computational Methods
28
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Engr/Math/Physics 25
Appendix
f x   2 x  7 x  9 x  6
3
2
Bruce Mayer, PE
Licensed Electrical & Mechanical Engineer
BMayer@ChabotCollege.edu
Engineering/Math/Physics 25: Computational Methods
29
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Power Fcn Plot
y  13 x
7  x  91
1.73
>> x =[7:91];
>> y = 13*x.^1.73;
>> Xp = log(x); >> Yp = log(y);
>> plot(x,y), xlabel('x'), ylabel('y'),...
grid, title('Power Function')
>> plot(Xp,Yp), xlabel('ln(x)'),
ylabel('ln(y)'),...
grid, title('Power Function')
Engineering/Math/Physics 25: Computational Methods
30
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Exponential Fcn Plot
v  115e
0.61t
 ln v  0.61t  ln 115
>> t = [0:0.05:10];
>> v = 120*exp(-0.61*t);
>> plot(t,v), xlabel('t'), ylabel('v'),...
grid, title('Exponential Function')
>> Ve = log(v);
>> plot(t,Ve), xlabel('t'),
ylabel('ln(v)'),...
grid, title('Exponential Function')
Engineering/Math/Physics 25: Computational Methods
31
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Altitude of Right Triangle
 The Area of RIGHT Triangle
A  1 2 x  y
 The Area of an ARBITRARY
Triangle
A  1 2 L  h
L
y
h
 By Pythagoras for
Rt-Triangle
L
x   y 
2
Engineering/Math/Physics 25: Computational Methods
32
2
x
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Altitude of Right Triangle cont
 Equating the A=½·Base·Hgt noting
that
Base1  x & Base 2  L
1 2 x  y  1 2 x  y 
2
 Solving for h
h
x   y 
Engineering/Math/Physics 25: Computational Methods
33
h
L
x  y
2
2
y
h
2
x
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Normalized Plot
>> T = [69.4, 69.7, 71.6, 75.2, 76.3, 78.6, 80.6,
80.6, 82, 82.6, 83.3, 83.5, 84.3, 88.6, 93.3];
>> CPH = [15.4, 14.7, 16, 15.5, 14.1, 15, 17.1,
16, 17.1, 17.2, 16.2, 17, 18.4, 20, 19.8];
>> Tmax = max(T);
>> Tmin = min(T);
>> CPHmax = max(CPH);
>> CPHmin = min(CPH);
>> Rtemp = Tmax - Tmin;
>> Rcroak = CPHmax - CPHmin;
>> DelT = T - Tmin;
>> DelCPH = CPH - CPHmin;
>> Theta = DelT/Rtemp;DelCPH = CPH - CPHmin;
>> Omega = DelCPH/Rcroak;
>> plot(T, CPH,), grid
>> plot(Theta,Omega), grid
Engineering/Math/Physics 25: Computational Methods
34
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Start Basic Fitting Interface 1
 FIRST →
Plot the
DATA
Engineering/Math/Physics 25: Computational Methods
35
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Start Basic Fitting Interface 2
Goodness of
Fit; smaller is
Better
Engineering/Math/Physics 25: Computational Methods
36
Expand Dialog Box
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Start Basic Fitting Interface 3
Frog Croaking in the Evening - 2045hrs
 Result
1
0.9
 Chk by
polyfit
y = 0.8737*x + 0.04286
0.8
0.7
Omega
0.6
>> p =
polyfit(Theta,Ome
ga,1)
p =
0.8737
0.0429

0.5
0.4
0.3
0.2
Croak Data
linear Fit
0.1
0
0
Engineering/Math/Physics 25: Computational Methods
37
0.1
0.2
0.3
0.4
0.5
Theta
0.6
0.7
0.8
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
0.9
1
Caveat
Engineering/Math/Physics 25: Computational Methods
38
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Greek Letters in Plots
Frog Croaking Frequency
1
0.9
 = 0.8737 + 0.04286
0.8
0.7

0.6
0.5
0.4
0.3
0.2
Croak Data
Linear Fit
0.1
0
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7

Engineering/Math/Physics 25: Computational Methods
39
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
0.8
0.9
1
Plot “Discoverables”
% "Discoverable" Functions Displayed
% Bruce Mayer, PE • ENGR25 • 15Jul09
%
x = linspace(-5, 5);
ye = exp(x);
ypp = x.^2;
ypm = x.^(-2);
% plot all 3 on a single graphe
plot(x,ye, x,ypp, x,ypm),grid,legend('ye', 'ypp', 'ypm')
disp('Showing MultiGraph Plot - Hit ANY KEY to continue')
pause
%
% PLot Side-by-Side
subplot(1,3,1)
plot(x,ye), grid
subplot(1,3,2)
plot(x,ypp), grid
subplot(1,3,3)
plot(x,ypm), grid
Engineering/Math/Physics 25: Computational Methods
40
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
% "Discoverable" Functions Displayed
% Bruce Mayer, PE • ENGR25 • 15Jul09
%
x = linspace(-5, 5);
ye = exp(x);
ypp = x.^2;
ypm = x.^(-2);
% plot all 3 on a single graphe
plot(x,ye, x,ypp, x,ypm),grid,legend('ye', 'ypp', 'ypm')
disp('Showing MultiGraph Plot - Hit ANY KEY to continue')
pause
%
% PLot Side-by-Side
subplot(1,3,1)
plot(x,ye), grid
subplot(1,3,2)
plot(x,ypp), grid
subplot(1,3,3)
plot(x,ypm), grid
Engineering/Math/Physics 25: Computational Methods
41
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Plot_Model-4.ppt
Download