Curve Fitting: Professor Henry Arguello
Curve Fitting: Professor Henry Arguello
Least-squares Line
Given a set of data points (x1 , y1 ), ..., (xN , yN ), where the abscissas {xk }
are distinct, one goal of numerical methods is to determine a formula
y = f (x) that relates these variables. This section emphasizes fitting the
data to linear functions of the form,
(1) y = f (x) = Ax + B
How do we find the best linear approximation of the form (1) that goes
near (not always through) the points? To answer this question, we need
to discuss the errors (also called derivations or residuals):
There are several norms that can be used with the residuals in (3) to
measure how far the curve y = f (x) lies from the data
There are several norms that can be used with the residuals in (3) to
measure how far the curve y = f (x) lies from the data
There are several norms that can be used with the residuals in (3) to
measure how far the curve y = f (x) lies from the data
1 PN
(5) Average error: E1 (f ) = |f (xk − yk )|,
N k=1
There are several norms that can be used with the residuals in (3) to
measure how far the curve y = f (x) lies from the data
1 PN
(5) Average error: E1 (f ) = |f (xk − yk )|,
N k=1
1 PN
(6) Root-Mean-Square error: E2 (f ) = ( |f (xk − yk )|2 )1/2 .
N k=1
Example Compare the maximum error, average error and RMS error
for the linear approximation y = f (x) = 8.61.6x to the data points
(-1,10), (0,9), (1,7), (2,5), (3,4), (4,3), (5,0) and (6,-1).
The errors are found using the values for f (xk ) and ek given in table
xk yk f (xk ) = 8.6 − 1.6xk ek e2k
-1 10.0 10.2 0.2 0.04
0 9.0 8.6 0.4 0.16
1 7.0 7.0 0.0 0.00
2 5.0 5.4 0.4 0.16
3 4.0 3.8 0.2 0.04
4 3.0 2.2 0.8 0.64
6 -1.0 -1.0 0.0 0.00
2.6 1.40
(7) E∞ (f ) = max{0.2, 0.4, 0.0, 0.4, 0.2, 0.8, 0.6, 0.0} = 0.8,
1
(8) E1 (f ) = (2.6) = 0.325,
8
1
(9) E2 (f ) = ( (1.4)1/2 ) ≈ 0.41833.
N
We can see that the maximum error is largest, and if one point is badly
in error, its value determines E1 (f ). The average error E1 (f ) simply
averages the absolute value of the error at the various points. It is often
used because it is easy to compute. The error E2 (f ) is often used when
the statistical nature of the error is considered.
A best-fitting line is found by minimizing one of the quantities in equa-
tions (4) through (6). Hence there are three best-fitting lines that we
could find.
Let {(xk , yk )}Nk=1 be a set of N points, where the abscissas {xk } are
distinct. The least-squares line y = f (x) = Ax + B is the line that
minimizes the Root-Mean-Square error E2 (f ).
The quantity E2 (f ) will be a minimum if and only if the quantity
PN
N(E2 (f ))2 = k=1 (Axk + Byk )2 is a minimum.
y = Ax + B
are the solution of the following linear system, known as the normal
equations:
P P
N 2 N PN
(10) k=1 xk A + k=1 xk B = k=1 xk , yk ,
P
N PN
k=1 xk A + NB = k=1 yk .
PN PN
(11) E(A, B) = k=1 (Axk + B − yk )2 = 2
k=1 dk
Setting the partial derivatives equal to zero in (12) and (13), use the
distributive properties of summation toPobtain
0 = k=1 (Axk + Bxk yk xk ) = A Nk=1 xk2 + B Nk=1 xk − Nk=1 yk xk
PN 2
P P
(14)
0 = Nk=1 (Axk + B − yk ) = A Nk=1 xk + NB − Nk=1 yk
P P P
(15)
Equations (14) and (15) can be rearranged in the standard form for a
system and result in the normal equations (10).
Example
Find the least-squares line for the data points given in the above ex-
ample. The sums required for the normal equations (10) are easily
obtained using the values in the table.
92A + 20B = 25
20A + 8B = 37
y = −1.6071429x + 8.6428571
Example
The values in table are use to find the summations required in formula
(16), where the power used is M = 2.
The coefficient A = 7.68680/1.5664 = 4.9073, and we get d = 4.9073t2
and g = 2A = 9.7146m/sec2 .
(1) y = CeAx
(4) Y = AX + B
The original points (xk , yk ) in the xy-plane are transformed into the
points (Xk , Yk ) = (xk , ln(yk )) in the XY-plane. This process is called data
linearization. Then the least-squares line (4) is fit to the points
{(Xk , Yk )}. The normal equations for finding A and B are
P P P
N 2 A+ N N
(5) k=1 kX k=1 k =
X k=1 Xk Yk ,
P
N
A + NB = Nk=1 Yk .
P
k=1 X k
After A and B have been found, the parameter C in equation (1) is com-
puted:
(6) C = eB .
Example
Use the data linearization method and find the exponential fit y = CeAx
for the five data points (0, 1.5), (1, 2.5), (2, 3.5), (3, 5.0), and (4, 7.5).
Apply the transformation (3) to the original points and obtain
xk yk Xk Yk = In(yk ) Xk2 X k Yk
0.0 1.5 0.0 0.405465 0.0 0.000000
1.0 2.5 1.0 0.916291 1.0 0.916291
2.0 3.5 2.0 1.252763 4.0 2.505526
3.0 5.0 3.0 1.609438 9.0 4.828314
4.0 7.5 4.0 2.014903 16.0 8.059612
10.0 6.198860 30 16.309743
PM
(16) f (x) = j=1 cj fj (x)
2
N
X N
X M
X
(17) E(c1 , c2 , ..., cM ) = (f (xk )−yk )2 = cj fj (xk ) − yk .
k=1 k=1 j=1
N
X XM
(18) cj fj (xk ) − yk (fi (xk )) = 0 for i = 1, 2, ..., M.
k=1 j=1
M N
! N
X X X
(19) fi (xk )fi (xk ) cj = fi (xk )yk for i = 1, 2, ..., M.
j=1 k=1 k=1
Matrix Formulation
Al though (19) is easily recognized as a system of M linear equations
in M unknowns, one must be clever so that wasted computations are
not performed when writing the system in matrix notation. The key is to
write dawn the matrices F and F’ as follows:
f1 (x1 ) f2 (x1 ) ··· fM (x1 ) f1 (x1 ) f1 (x2 ) · · · f1 (xN )
f1 (x2 ) f2 (x2 ) ··· fM (x2 ) f2 (x1 ) f2 (x2 ) · · · f2 (xN )
F = . .. , F’ = .. .. .
.. ..
.. . . . . .
f1 (xN ) f2 (xN ) · · · fM (xN ) fM (x1 ) fM (x2 ) · · · fM (xN )
The element in the ith row of the product F’Y in (20) is the same as the
ith element in the column matrix in equation (19); that is,
PN 0
(21) k=1 fi (xk)yk = rowi − F’ . y1 y2 · · · yN
Polynomial Fitting
When the foregoing method is adapted to using the functions {fi (x) =
xj−1 } and the index of summation ranges from j = 1 to j = M + 1, the
function f (x) will be a polynomial of degree M:
Least-Squares Parabola
Suppose that {(Xk, Yk)}Nk=1 are N points, where the abscissas are
distinct. The coefficients of the least-squares parabola
The partial derivatives ∂E/∂A, ∂E/∂B, and ∂E/∂C must be zero. This
results in (28)
0 = ∂E(A, B, C)/∂A = 2 Nk=1 (Axk2 + Bxk ∗ C − yk )(xk2 ),
P
Polynomial Wiggle
For example let f (x) = 1.44/x2 + 0.24x be used to generate the six
data points (0.25,23.1), (1.0,1.68), (1.5,1.0), (2.0,0.84), (2.4,0.826), and
(5.0,1.2576). The result of curve fitting with the least-square polynomi-
als
is shown in Figure through (d). Notice that P3 (x), P4 (x), and P5 (x), ex-
hibit a large wiggle in the interval [2,5]. Even though P5 (x) goes through
the 6 points, it produces the worst fit. If we must fit a polynomial to this
data, P2 (x) should be the choice.
Figure: (a) using P2 (x) to fit data. (b) using P3 (x) to fit data. (c) using P4 (x) to
fit data. (d) using P5 (x) to fit data
Professor Henry Arguello Numerical methods March 12, 2018 33 / 33