Professional Documents
Culture Documents
Ch09slides PDF
Ch09slides PDF
of Data to a Curve
Gerald Recktenwald
Portland State University
Department of Mechanical Engineering
gerry@me.pdx.edu
These slides are a supplement to the book Numerical Methods with
Matlab: Implementations and Applications, by Gerald W. Recktenwald,
c 20002007, Prentice-Hall, Upper Saddle River, NJ. These slides are
copyright c 20002007 Gerald W. Recktenwald. The PDF version
of these slides may be downloaded or stored or printed only for
noncommercial, educational use. The repackaging or sale of these
slides in any form, without written consent of the author, is prohibited.
The latest version of this PDF le, along with other supplemental material
for the book, can be found at www.prenhall.com/recktenwald or
web.cecs.pdx.edu/~gerry/nmm/.
page 1
Overview
F (x) = x +
1
1 2 3 4 5 6
x
P
For statistical and computational reasons choose minimization of = ri2
X
m
2
= [yi (xi + )]
i=1
X
m
2
ri
i=1
and
=0
=constant
where
X
m X
m
Sxx = xixi Sx = xi
i=1 i=1
Xm Xm
Sxy = xiyi Sy = yi
i=1 i=1
Note: Sxx, Sx, Sxy , and Syy can be directly computed from the given (xi, yi) data.
Thus, Equation (1) and (2) are two equations for the two unknowns, and .
1
= (SxSy mSxy ) (3)
d
1
= (SxSxy SxxSy ) (4)
d
with
2
d = Sx mSxx (5)
Now, lets rederive the equations for the t. This will give us insight into the process or
tting arbitrary linear combinations of functions.
For any two points we can write
x1 + = y1
x2 + = y2
or
x1 1 y1
=
x2 1 y2
Writing out the x + = y equation for all of the known points (xi, yi),
i = 1, . . . , m gives the overdetermined system.
2 3 2 3
x1 1 y1
6 x2 17 6 y2 7
6 . ... 5 = 6
7 7 or Ac = y
4 .. 4 ... 5
xm 1 ym
where 2 3 2 3
x1 1 y1
6 x2 17 6 y2 7
A=6
4 ... ... 7
5 c= y=6 7
4 ... 5
xm 1 ym
Note: We cannot solve Ac = y with Gaussian elimination. Unless the system is consistent (i.e., unless
y lies in the column space of A) it is impossible to nd the c = (, )T that exactly satises
all m equations. The system is consistent only if all the data points lie along a single line.
Minimizing requires
T T
= 2A y + 2A Ac = 0
c
or
T T
(A A)c = A b
The linefit function ts a line to a set of data by solving the normal equations.
>> x = [1 2 4 5];
3.5
>> y = [1 2 2 3];
>> c = linefit(x,y) 3
c =
0.8000
2
Evaluate and plot the t
1.5
>> xfit = linspace(min(x),max(x));
>> yfit = c(1)*xfit + c(2) 1
>> plot(x,y,o,xfit,yfit,-);
0.5
0
0 1 2 3 4 5 6
x
R2 is a measure of how well the t function follows the trend in the data. 0 R2 1.
Define:
y is the value of the t function at the For a line t yi = c1xi + c2
known data points.
1 X
y is the average of the y values y = yi
m
Then: X 2
2
(y i y) r22
R =X =1P
(yi y)
2 (yi y)2
r22
Consider a line t to a data set with R = 1 P
2
= 0.934
(yi y)2
5
Vertical distances between given y
4
data and the least squares line t. y
Vertical lines show contributions to 3
r2. 2
1
1 2 3 4 5 6
x
5
Vertical distances between given y
4
data and the average of the y . y
Vertical lines show contributions to 3
P
(yi y)2 2
1
1 2 3 4 5 6
x
GPa
temperature (Cf. Example 9.4) 200
T (C) 20 500 1000 1200 1400 1500
Bulk Modulus
195
G (GP a) 203 197 191 188 186 184
190
>> [t,D,labels] = loadColData(SiC.dat,6,5);
>> g = D(:,1); 185
>> [c,R2] = linefit(t,g);
c = 180
-0.0126 0 500 1000 1500
203.3319
R2 = T C
0.9985
Examples:
y = c1ec2x ln y = x +
y = c1xc2 ln y = ln x +
y = c1xec2x ln(y/x) = x +
Consider
c2 x
y = c1e (6)
Taking the logarithm of both sides yields
ln y = ln c1 + c2x
v = ln y b = ln c1 a = c2
The preceding steps are equivalent to graphically obtaining c1 and c2 by plotting the data
on semilog paper.
y = c1ec2x ln y = c2x + ln c1
1
5 10
4.5
3.5 0
10
3
2.5
y
y
2
1
10
1.5
0.5
2
0 10
0 0.5 1 1.5 2 0 0.5 1 1.5 2
x x
ln y = ln c1 + c2 ln x (7)
v = ln y u = ln x b = ln c1 a = c2
The preceding steps are equivalent to graphically obtaining c1 and c2 by plotting the data
on log-log paper.
y = c1xc2 ln y = c2 ln x + ln c1
2
14 10
12
10
8
1
10
y
y
0
0 10 2 1 0 1
0 0.5 1 1.5 2 10 10 10 10
x x
The preceding steps are equivalent to graphically obtaining c1 and c2 by plotting the data
on semilog paper.
y = c1xec2x ln(y/x) = c2x + ln c1
1
0.7 10
0.6
0.5
0
10
0.4
y
y
0.3
1
10
0.2
0.1
2
0 10
0 0.5 1 1.5 2 0 0.5 1 1.5 2
x x
function c = xexpfit(x,y)
% xexpfit Least squares fit of data to y = c(1)*x*exp(c(2)*x)
%
% Synopsis: c = xexpfit(x,y)
%
% Input: x,y = vectors of independent and dependent variable values
%
% Output: c = vector of coefficients of y = c(1)*x*exp(c(2)*x)
0.7
y
>> noise = 0.05; 0.3
>> x = linspace(x0,2,200);
>> y = 5*x.*exp(-3*x); 0.2
>> yn = y + noise*(rand(size(x))-0.5);
>> % Guarantee yn>0 for log(yn) 0.1
>> yn = abs(yn);
>> c = xexpfit(x,yn); 0
0 0.5 1 1.5 2
c = x
5.7701
-3.2330
4. The least-squares principle denes the best t as the values of c1 and c2 that minimize
2 2
(c1, c2) = y F (x)2 = y Ac2.
6. Solving the normal equations gives the slope c1 and intercept c2 of the best t line.
or
X
n
F (x) = cj fj (x)
j=1
The basis functions
f1(x), f2(x), . . . , fn(x)
are chosen by you the person making the t.
The coecients
c1, c2, . . . , cn
are determined by the least squares method.
F (x) function can be any combination of functions that are linear in the cj . Thus
2 2/3 x 4x
1, x, x , x , sin x, e , xe , cos(ln 25x)
are not valid basis functions as long as the cj are the parameters of the t.
For example, the t function for a cubic polynomial is
3 2
F (x) = c1x + c2x + c3x + c4,
X
n
ri = yi F (xi) = yi cj fj (xi)
j=1
The least-squares solution gives the cj that minimize r2.
Ac = y,
where 2 3
f1(x1) f2(x1) f3(x1)
6 f1(x2) f2(x2) f3(x2) 7
A=6
4 ... ... ... 7
5,
f1(xm) f2(xm) f3(xm)
2 3
2 3 y1
c1 6 y2 7
c = 4c25 , y=6 7
4 ... 5 .
c3
ym
If F (x) cannot interpolate the data, then the preceding matrix equation cannot be solved
exactly: b does not lie in the column space of A.
The least-squares method provides the compromise solution that minimizes
r2 = y Ac2.
The c that minimizes r2 satises the normal equations
T T
(A A)c = A y.
2 3 2 3
c1 y1
6 c2 7 6 y2 7
c=6 7
4 ... 5 , y=6 7
4 ... 5 .
cn ym
12
Consider tting a curve to the following
data. 10
x 1 2 3 4 5 6 8
y
4
2
0 1 2 3 4 5 6 7
x
where the ci are the coecients to be determined by the t and the basis functions are
2
f1(x) = x , f2(x) = x, f3(x) = 1
The A matrix is 2 3
x21 x1 1
6 x2 x2 17
A=6
4 ...
2
... ... 7
5
x2m xm 1
where, for this data set, m = 6.
>> x = (1:6);
>> y = [10 5.49 0.89 -0.14 -1.07 0.84];
F(x) = c1 x2 + c2 x + c3
12
y
4
2
0 1 2 3 4 5 6 7
x
c1
F (x) = + c2x 10
x
8
y
, x 4
x
In Matlab:
2
>> x = (1:6); 0
The overdetermined coecient matrix contains the basis functions evaluated at the
known data 2 3
f1(x1) f2(x1) . . . fn(x1)
6 f1(x2) f2(x2) . . . fn(x2) 7
A=6
4 ... ... ... 7
5
f1(xm) f2(xm) . . . fn(xm)
Thus, if A is available
F (x) = Ac
evaluates F (x) at all values of x, i.e., F (x) is a vector-valued function.
function A = xinvxfun(x)
A = [ 1./x(:) x(:) ];
Advantages:
The basis functions are dened in only one place: in the routine for evaluating the
overdetermined matrix.
Automation of tting and plotting is easier because all that is needed is one routine
for evaluating the basis functions.
End-user of the t (not the person performing the t) can still evaluate the t
function as y = c1f1(x) + c2f2(x) + + cnfn(x).
Disadvantages:
Storage of matrix A for large x vectors consumes memory. This should not be a
problem for small n.
Evaluation of the t function may not be obvious to a reader unfamiliar with linear
algebra.
The columns of A are the basis functions evaluated at each of the x data points.
As before, the normal equations are
T T
A Ac = A y
function A = poly2Basis(x)
A = [ x(:).^2 x(:) ones(size(x(:)) ];
where yi is the value of the t function evaluated at xi, and y is the average of the
(known) y values.
For a linear combination of basis functions
X
n
yi = cj fj (xi)
j=1
To account for the reduction in degrees of freedom in the data when the t is performed,
it is technically appropriate to consider the adjusted coecient of determination
P
2 m1 (yi y)2
Radjusted = 1 P ,
mn1 (yi y)2
2
fitnorm provides the option of computing Radjusted
Syntax:
c = polyfit(x,y,n)
[c,S] = polyfit(x,y,n)
In Matlab:
>> x = (1:6);
>> y = [10 5.49 0.89 -0.14 -1.07 0.84];
>> c = polyfit(x,y,3);
>> xfit = linspace(min(x),max(x));
>> yfit = polyval(c,xfit);
>> plot(x,y,o,xfit,yfit,--)
10
2
1 2 3 4 5 6
35
30
25
Conductivity (W/m/C)
20
15
10
0
0 10 20 30 40 50 60
Temperature (K)
1
k(T ) = c1 2
+ c2T
T
To t using linear least squares we need to write this as
1 c1 2
(T ) = = + c2T
k(T ) T
function y = cuconBasis1(x)
% cuconBasis1 Basis fcns for conductivity model: 1/k = c1/T + c2*T^2
y = [1./x x.^2];
An m-le that uses fitnorm to t the conductivity data with the cuconBasis1 function
is listed on the next page.
function conductFit(fname)
% conductFit LS fit of conductivity data for Copper at low temperatures
%
% Synopsis: conductFit(fname)
%
% Input: fname = (optional, string) name of data file;
% Default: fname = conduct1.dat
%
% Output: Print out of curve fit coefficients and a plot comparing data
% with the curve fit for two sets of basis functions.