Professional Documents
Culture Documents
Chapter 4
Discrete Least-
Squares
At the end of this chapter, students should be able to:
Describe curve fitting and distinguish the two approaches to curve fitting
Describe and derive linear least-squares
Describe polynomial least-squares
Apply least-squares method in curve fitting problems
4.1 Introduction
There are many instances in engineering practice where a relationship between
two variables can be characterized experimentally. Often no underlying theory is
available to represent this relationship mathematically and analysis needs to be
done to see the behavior or the trend of the dependent variable given the
independent variable. The process of using the pattern of the data to make
prediction is called trend analysis. The procedure of finding a function to
represent this relationship is known as curve fitting. In this chapter, we shall
begin with a brief discussion on two general approaches to curve fitting
techniques namely least-squares and interpolation methods. The rest of the
chapter will focus on discrete least-squares method. We shall learn to fit a
polynomial to a given set of data using least-squares procedure. Interpolation
method will be discussed in depth in Chapter 4 (Interpolating Polynomials).
51
THE METHOD OF LEAST SQUARES Part 3
f(x)
f(x)
x x
(a) Least-squares (b) Interpolation
Figure 4.1
Often in the real world one expects to find linear relationships between variables.
For example, the force of a spring linearly depends on the displacement of the
spring: y = kx (here y is the force, x is the displacement of the spring from rest,
and k is the spring constant). To test the proposed relationship, researchers go
to the lab and measure what the force is for various displacements.
52
THE METHOD OF LEAST SQUARES Part 3
f(x)=a0+a1x
(x2,y2)
E3
E1 (x3,y3)
(x1,y1)
x
Figure 4.2
Suppose the equation of the line that we are looking for is f(x) a0 a1 x .
Linear least-squares is a procedure in which the coefficients a0 and a1
(representing the y-intercept and slope) are determined such that the line best
fits the data points. For a given set of data points (x, y), x is assumed to be exact
and only the y values randomly have errors. The error (or called “residual”) for
each data set is the discrepancy between the observed value y and the
approximate value of y which is f(x) a0 a1 x , predicted by the line (or called
the “calculated value”):
Ei f(xi ) y i or Ei (a0 a1 xi ) - yi
The strategy for fitting the “best” line through the data would be to minimize the
sum of the residual error squares for all available data, i.e.
n n
E i2 (a0 a1xi ) y i 2 to be a minimum.
i 1 i 1
n
Let E (a0 , a1 ) (a
i 1
0 a1 x i ) y i .
2
The objective is to minimize the function
53
THE METHOD OF LEAST SQUARES Part 3
2a0 n 2a1 x - 2 y i i 0
a0 n a x y
1 i i (1)
2a0 x i 2a1 x i
2
- 2 x y i i 0
a0 x i a1 x i
2
x y i i (2)
n n n
(2) a0 i 1
x i a1
i 1
xi
2
x y
i 1
i i
The normal equations (1) and (2) above can also be written in matrix form and
the values of the coefficients a0 and a1 are obtained by solving the matrix:
n
n
n xi yi
i 1 a0 = i 1
n
2 a1 n
n
xi
i 1
i 1
xi
xi y i
i 1
54
THE METHOD OF LEAST SQUARES Part 3
i 1
xi ,
i 1
2
xi ,
i 1
xi y i , y
i 1
i
2
Example 1
Find the straight line that minimizes the least-squares error for the points (1,
4), (2, 5) and (3, 8).
Solution
xi
i 1
i 1
xi
xi y i
i 1
Find the values for all the elements of the matrix:
Total no. of points n = 3
i xi yi xi2 xiyi
1 1 4 1 4
2 2 5 4 10
3 3 8 9 24
n n n n
i 1
xi 6
i 1
y i 17 i 1
x i 14
2
x y
i 1
i i 38
55
THE METHOD OF LEAST SQUARES Part 3
17 6 3 17
A0 10 A1 12
38 14 6 38
A0 A1
a0 1.6667 a1 2
A A
Example 2
Consider the data presented below.
xi 1.5 1.8 2.4 3.0 3.5 3.9 4.4 4.8 5.0
yi 4.8 5.7 7.0 8.3 10.9 12.4 13.1 13.6 15.3
Determine the least-squares linear function that best-fit the data and find the
value of y when x = 2.0.
Solution
xi
i 1
i 1
xi
a1
xi y i
i 1
Find the values for all the elements of the matrix:
Total no. of points: n = 9
56
THE METHOD OF LEAST SQUARES Part 3
i xi yi xi2 xiyi
1 1.5 4.8 2.25 7.20
2 1.8 5.7 3.24 10.26
3 2.4 7.0 5.76 16.80
4 3.0 8.3 9.00 24.90
5 3.5 10.9 12.25 38.15
6 3.9 12.4 15.21 48.36
7 4.4 13.1 19.36 57.64
8 4.8 13.6 23.04 65.28
9 5.0 15.3 25.00 76.50
n n n n
i 1
x i 30.3
i 1
y i 91.1
i 1
x i 115.11
2
x y
i 1
i i 345.09
57
THE METHOD OF LEAST SQUARES Part 3
Warm up exercise
An ecologist wished to know whether certain species of aquatic insects
have their ecological range limited by temperature. He collected the
following data, relating the average daily temperature at different
portions of a creek with the elevation of that portion of the creek (above
a sea level).
(i) Find the straight line that provides the best least-squares fit to this
data.
(ii) Use the linear function to estimate the average daily temperature
for this creek at altitude 3.2 kilometers.
58
THE METHOD OF LEAST SQUARES Part 3
a0 a1 xi a2 xi ... am xi
2 m
error at the ith point is Ei = - yi and we
E
i 1
i
2
. Here, the least squares procedure is handled in a similar manner as in
59
THE METHOD OF LEAST SQUARES Part 3
n n n n n
∑ a0 a1 ∑ x i a2 ∑ x i ..... am
2
∑ xi
m
∑y i (1)
i 1 i 1 i 1 i 1 i 1
n n n n n
a0 ∑ x i a1 ∑ xi
2
a2 ∑ x i ..... am
3
∑ xi
m 1
∑y x i i (2)
i 1 i 1 i 1 i 1 i 1
n n n n n
a0 ∑x i
2
a1 ∑x i
3
a2 ∑x i
4
..... am ∑x i
m2
∑y x i i
2
(3)
i 1 i 1 i 1 i 1 i 1
n n n n n
a0 ∑x i
3
a1 ∑x i
4
a2 ∑x i
5
..... am ∑x i
m3
∑y x i i
3
(4)
i 1 i 1 i 1 i 1 i 1
. .
. .
. .
n n n n n
a0 ∑ xi
m
a1 ∑ xi
m 1
a2 ∑ xi
m2
..... am ∑ xi
2m
∑y x i i
m
(m+1)
i 1 i 1 i 1 i 1 i 1
Example 4
Fit the data in the given table with the discrete least squares polynomial of
degree two.
i 0 1 2 3 4
xi 0 0.25 0.5 0.75 1.00
yi 1.0 1.2840 1.6487 2.1170 2.7183
Solution
i x y x2 x3 x4 xi yi xi 2 yi
1 0 1.0 0 0 0 0 0
2 0.25 1.2840 0.0625 0.0156 0.0039 0.3210 0.0803
3 0.5 1.6487 0.2500 0.1250 0.0625 0.8244 0.4122
4 0.75 2.1170 0.5625 0.4219 0.3164 1.5878 1.1908
5 1.00 2.7183 1.0000 1.0000 1.0000 2.7183 2.7183
2.5000 8.7680 1.8750 1.5625 1.3828 5.4514 4.4015
61
THE METHOD OF LEAST SQUARES Part 3
Example 5
Fit a quadratic curve to the following data.
x 2 4 6 8 10
y 3.07 12.85 31.47 57.38 91.29
Solution
Degree : m = 2 , f(x) a0 a1 x a2 x 2
n
x x 2
3
a0
a
y
x x x
2
1 xy
x2
x x
3 4
a2
x y
2
62
THE METHOD OF LEAST SQUARES Part 3
i x y x2 x3 x4 xi yi xi 2 yi
1 2 3.07 4 8 16 6.14 12.28
2 4 12.85 16 64 256 51.4 205.6
3 6 31.47 36 216 1296 188.82 1132.92
4 8 57.38 64 512 4096 459.04 3672.32
5 10 91.29 100 1000 10000 912.9 9129
30 196.06 220 1800 15664 1618.3 14152.1
Warm up exercise
x 5 10 15 20 25 30 35 40 45 50
y 16 25 32 33 38 36 39 40 42 42
Make a scatter plot of these points. Which of the function above is more
suitable?
63
THE METHOD OF LEAST SQUARES Part 3
Exercise 4
3. The table below shows the time (in seconds) required for water to drain through a
hole in the bottom of a bottle as a function of depth (in meters) to which the bottle
has been filled.
x 5 6 10 14 16 20 22 28 28 36 38
y 30 22 28 14 22 16 8 8 14 0 4
6. The following tabulated values are taken from an experiment. Complete the table
and calculate the sum of error squares (use linear regression).
64