Chapter 5 Fitting Other Models
Chapter 5 Fitting Other Models
Chapter 5
Fitting Other
Models and
Continuous
Data
At the end of this chapter, students should be able to:
Describe discrete least-squares for other type of functions or models
Describe continuous least-squares approximation
Apply these two least-squares method in curve fitting problems
5.1 Introduction
In the previous chapter we discussed techniques of finding least-squares
polynomials to fit discrete data. In this chapter we shall proceed with fitting other
models (other than polynomials) to that same type of data. We shall also discuss
how to perform continuous least-squares approximation (fitting functions or
continuous data). At the end of this chapter, sequences of Legendre polynomials
will be discussed as an alternative method of fitting functions.
65
THE METHOD OF LEAST SQUARES PART 3
The technique of data linearization has been used to fit curves such as
y Ce kx , y cx k , y A ln( x ) B and many more. Once the model has been
Y = a0 + a1 X
1
2. ax2 b
y 1
Y , X x 2, a0 b, a1 a
y
y ab x Y a0 a1 x
3. ln y ln a x ln b
Y ln y, a0 ln a, a1 ln b
y ax b Y a0 a1 X
4.
ln y ln a b ln x
Y ln y, X ln x , a0 ln a, a1 b
ax
y Y a0 a1 X
bx
5.
1 b 1 1
. Y
1
,X
1
, a0
1
, a1
b
y a x a y x a a
y keax Y a0 a1 x
6.
ln y ln k ax Y ln y, a0 ln k, a1 a
66
THE METHOD OF LEAST SQUARES PART 3
Example 1
Use the method of least-squares to fit an exponential model y ae bx to the
following data.
xi 0.4 0.8 1.2 1.6 2.0 2.3
yi 800 975 1500 1950 2900 3600
Solution
Identify the expected function : y ae bx
Identify the unknowns : a, b
Linearize:
y aebx
ln y ln aebx
o ln y ln a ln e bx
ln a bx ln e ln e 1)
ln a bx
67
THE METHOD OF LEAST SQUARES PART 3
xi
i 1
i 1
xi
x i Yi
i 1
Determine the respective values:
i xi xi2 yi Yi xi Yi
1 0.4 0.16 800 ln 800 2.6738
2 0.8 0.64 975 ln 975 5.5059
3 1.2 1.44 1500 ln 1500 8.7759
4 1.6 2.56 1950 ln 1950 12.1209
5 2.0 4.00 2900 ln 2900 15.9449
6 2.3 5.29 3600 ln 3600 18.8340
n n
x
i 1
i 8.3
x
n
i 14.09
n
Y i 44.6170 x Y
i 1
i i 63.8555
i 1 i 1
a0 6.3037 and
a1 0.8187
Solve for the model unknowns:
a0 ln a 6.3037 a1 b 0.8187
ae 6.3037
546.5906
Example 2
68
THE METHOD OF LEAST SQUARES PART 3
Solution
y Ax k
y Ax k
ln y ln A k ln x
Y a0 a1 X
Y ln y, X ln x , a0 ln A, a1 k
x y X = ln x X2 Y = ln y XY
1.00 4.33 0.0000 0.0000 1.4656 0.0000
1.15 4.58 0.1398 0.0195 1.5217 0.2127
1.40 4.98 0.3365 0.1132 1.6054 0.5402
1.43 5.06 0.3677 0.1279 1.6214 0.5799
1.60 5.28 0.4700 0.2209 1.6639 0.7821
2.00 5.80 0.6831 0.4805 1.7579 1.2185
Sum: 1.9971 0.9620 9.6388 3.3333
Rewrite
ln y ln A k ln x
as
Y a0 a1 X
69
THE METHOD OF LEAST SQUARES PART 3
6
6
6 Xi Yi
i 1 a0 = i 1
6
2 a1 6
6
Xi
i 1
i 1
Xi
X i Yi
i 1
6 1.9971 a0 9.6358
1.9971 0.9620 = 3.3333
a1
a0 1.4649 , a1 0.4239
Warm up exercise
70
THE METHOD OF LEAST SQUARES PART 3
∫ f(x) - a dx
b
2
0 a1 x a2 x 2 ... ai x i ... am x m
a
We seek to minimize the sum of error squares. From the calculus of functions of
several variables, a necessary condition for the values a0, a1, …, and am to
minimize E is such that
∂E ∂E ∂E ∂E
= = ... = ... =0 0im
∂ a0 ∂ a1 ∂ ai ∂ am
Hence,
b
∫ f(x) - a0 a1 x ... ai x i ... am x m dx 0
2
(1)
a
a0
b
∫ f(x) - a0 a1 x ... ai x i ... am x m dx 0
2
( 2)
a
a1
.
.
.
b
∫ f(x) - a0 a1 x ... ai x i ... am x m dx 0
2
(i )
a
ai
.
.
.
b
∫ f(x) - a0 a1 x ... ai x i ... am x m dx 0
2
(m )
a
am
∫ 2 f(x) - a
b
∫ a
b
0 a1 x ... ai x i ... am x m f ( x ) dx 0
a
b b b b b
a
a0 dx a1 xdx ... ai x i dx ... am x m dx f ( x ) dx
a a a
a
b b b b b
a0 dx a1 xdx ... ai x dx ... am x dx f ( x ) dx
i m
a a a a a
Simplifying (2) to (m) using similar procedures yield the normal equations
71
THE METHOD OF LEAST SQUARES PART 3
b b b b b
a0 xdx a1 x 2 dx ... ai x i 1dx ... am x m 1dx xf ( x )dx
a a a
a
a
.
.
b b b b b
a0 x i dx a1 x 2i dx ... ai x 2i dx ... am x m i dx x i f ( x )dx
a a a
a
a
.
.
b b b b b
a0 x m dx a1 x 2m dx ... ai x i m dx ... am x 2m dx x m f ( x )dx
a a a
a
a
72
THE METHOD OF LEAST SQUARES PART 3
Example 3
Derive the normal equation in matrix form to approximate y f(x) with a straight
line on the interval [a, b].
Solution
Let P1 ( x ) a0 a1 x
∂E ∂E
= =0
∂ a0 ∂ a1
b
E ∫ f(x) - P (x) dx
2
1
a
b
∫ f(x) - a a1 x dx
2
0
a
b
∫ f(x) - a0 a1 x 2 dx 0
a
a0
b
∫ 2 f(x) - a
a
0 a1 x ( 1)dx 0
∫ a
a
0 a1 x f ( x )dx 0
b b b
a dx a xdx f ( x )dx
a
0
a
1
a
b b b
a0 dx a1 xdx f ( x )dx
a a
a
b
∫ f(x) - a0 a1 x 2 dx 0
a
a1
b
∫ 2 f(x) - a
a
0 a1 x ( x )dx 0
∫ a
a
0 a1 x f ( x )xdx 0
73
THE METHOD OF LEAST SQUARES PART 3
b b b
a
a0 xdx a1 x 2 dx xf ( x )dx
a
a
b b b
a0 xdx a1 x dx xf ( x )dx
2
a a a
Example 4
Find the least-squares polynomial approximation of degree one to the function
y x 2 4x 4 on the interval [0,2].
Solution
2 2
2
dx
0
xdx
f ( x )dx
a0 0
a 2
0
2 2
1
xdx
0
0
x 2 dx
0
xf ( x )dx
56
2 2 a
8 3
0
2 3 a1 68
3
a0 3.336
a1 5.997
Hence, P1 ( x ) 3.336 5.997x
Example 5
Find the least-squares polynomial approximation of degree two to y e x on
[ 0, 1].
74
THE METHOD OF LEAST SQUARES PART 3
Solution
0 0
a 0 1
0 0
1 1 1
xdx
x dx
2
x 3dx a1 xe x dx
0 0
1 a 2 1
0 0
1 1
x 2 dx x e dx
x dx
3 4 2 x
x dx
0 0 0 0
1 1
75
THE METHOD OF LEAST SQUARES PART 3
Definition
The functions listed below are called Legendre polynomials and are defined for
-1 x 1:
P0 (x ) 1
P1 ( x ) x
P2 ( x )
1
2
3x 2 - 1
P3 ( x )
1
2
5x 3 - 3x
P4 ( x )
1
8
35x 4 - 30x 2 + 3
P5 ( x )
1
8
63x 5 - 70x 3 + 15x
.
.
2m + 1 m
Pm 1 ( x ) x.Pm - Pm -1
m +1 m +1
This set of Legendre polynomials is said to be orthogonal on [-1,1] with respect
to the weight function w(x) 1 .The criteria required is that these functions are
designed to satisfy the following orthogonality condition:
1 0 if n m
Pm ( x ) Pn ( x ) dx 2
if n m
1 2n 1
(details of orthogonality and weight function shall not be discussed here)
76
THE METHOD OF LEAST SQUARES PART 3
Pn ( x )
1 dn
n
2 n! dx n
( x 2 - 1)n
Suppose y(x) is a function continuous on [-1,1]. Here, the approach to finding the
least-squares approximating polynomial f(x) to fit the function y(x) (or, the
continuous data) is done in a similar manner. Let f(x) be of polynomial of degree
m defined using sequences of Legendre polynomials such that
f ( x ) a0P0 ( x ) a1P1 ( x ) ... am Pm ( x )
-1
1
∫ a P (x) + a P (x) + a - y(x) dx
2
0 0 1 1 m Pm (x) (1)
-1
From the calculus of functions of several variables, a necessary condition for the
values a0, a1, …, am to minimize L is that
∂L
= 0, for each k = 0, 1, …, m.
∂ ak
Hence, using (1),
1
L
ak
2
-1
∫ a P (x) a P (x) a
0 0 1 1 m Pm ( x ) - y ( x )dx
0
With the orthogonality property of Legendre polynomials, this term can be
simplified to:
1
∫ a P ( x )P ( x ) - y ( x )P ( x )dx 0
-1
k k k k
1 1
∫ P ( x ) dx ∫
2
ak . k y ( x ) Pk ( x )dx
-1 -1
1
2
2k 1
ak ∫
-1
y ( x ) Pk ( x )dx
77
THE METHOD OF LEAST SQUARES PART 3
Thus,
1
2k 1
ak
2 -1∫ y ( x ) Pk ( x ) dx
Notice that ak calculates the coefficients a0, a1, …, am with the condition that x is
defined for y(x) on the interval [-1,1].
Theorem
Suppose y(x) is continuous and defined on [-1,1], then y(x) can be approximated
by a least-squares polynomial f(x) of degree m, using series of Legendre
polynomials such that:
f ( x ) a0P0 ( x ) a1P1 ( x ) ... am Pm ( x ) where, the coefficients a0, a1, …, am is
be determined by
1
2k 1
ak
2 1
y ( x ) Pk ( x ) dx for k = 0,1,2,3,……….,m
Example 3
Find the least-squares polynomial approximation of degree one to
78
Solution
THE METHOD OF LEAST SQUARES PART 3
since,
P0 ( x ) 1 , P1 ( x ) x
1
2(0) 1
k 0: a0
2 1
( x 2 4 x 4) P0 ( x ) dx
1
1
2 1
( x 2 4 x 4)(1)dx
13
3
1
2(1) 1
k 1 : a1
2 1
( x 2 4 x 4) P1 ( x ) dx
1
3
2 1( x 2 4 x 4)x dx
4
Thus, the polynomial of degree one to fit y(x) is:
f ( x ) a0 P0 ( x ) a1P1 ( x )
13
4x
3
Another method which can be applied is by solving for the unknowns a0 and a1:
1 1
1
dx
1
xdx
y ( x )dx
a0 1
a 1
1
1 1
1
xdx
1
1
x 2 dx
1
xy ( x )dx
79
THE METHOD OF LEAST SQUARES PART 3
In the case where x (or the independent variable) is defined on [m,n], where
m 1 and n 1 , a suitable linear transformation is required so that the
interval range of the independent variable is normalized to be on [-1,1].
Example 4
Find the least-squares polynomial approximation of degree one to
Solution
80
THE METHOD OF LEAST SQUARES PART 3
Thus,
x t 1 or t x 1
1
2(0) 1
k 0: a0
2 1
(t 2 6t 9) P 0(t ) dt
1
1 28
2 1
(t 2 6t 9)(1) dt
3
1
2(1) 1
k 1: a1
2 y (t ) P (t ) dt
1
1
1
3
2 1
(t 2 6t 9)(t ) dt 6
81
THE METHOD OF LEAST SQUARES PART 3
Example 5
Find the least-squares polynomial approximation of degree two to f ( x ) e x
on the interval
a) [-1, 1] b) [ 0, 1]
Solution
a) No transformation is required since x [1, 1 ] .
f ( x ) a0 P0 ( x ) a1P1 ( x ) a2 P2 ( x )
1
ao a1 x a2 (3x 2 1)
2
1
since P0 ( x ) 1 , 1P1 ( x ) x and P2 ( x ) (3x 2 1)
2
Determine the coefficients a0 ,, a1 and a2:
1
2k 1
ak
2 1
y ( x ) Pk ( x ) dx for k = 0,1,2
1
2(0) 1
k 0 : a0
2 1
e x P0 ( x ) dx
1
1
2 1
e x dx
1.1752
1
2(1) 1
k 1: a1
2 1
e x P1 ( x ) dx
1
3
2 1
e x x dx
1.1036
1
2( 2) 1
k 2: a1
2 1
e x P2 ( x ) dx
1
5 1
e x (3x 2 1) dx
2 1 2
0.3578
82
THE METHOD OF LEAST SQUARES PART 3
Hence,
f ( x ) a0 P0 ( x ) a1P1 ( x ) a2 P2 ( x )
1
ao a1 x a2 (3x 2 1)
2
1
1.1752 1.1036x 0.3578 (3x 2 1)
2
0.9963 1.1036x 0.5367x 2 .
b)
Need a suitable linear transformation since x [1, 1 ] and x [0 ,1 ]
Make a suitable linear transformation of x to t:
x [0,1 ] t [1,1 ]
Let x at b , solve for a and b
When x 0 : t 1 thus 0 a(1) b …(1)
When x 1 : t 1 thus 1 a(1) b …(2)
Solving for a and b:
1 1
a and b
2 2
Thus,
1 1 t 1
x t or 2x t 1 t 2x 1
2 2 2
Write y(x) in the term of y(t):
y(x) ex
t 1
y (t ) e 2
83
THE METHOD OF LEAST SQUARES PART 3
1 t 1
2( 0) 1
k 0: a0
2 1
e 2 P0 (t ) dt
1 t 1
1
2 1
e 2 dt
1.7183
1 t 1
2(1) 1
k 1: a1
2 1
e 2 P1 (t ) dt
1 t 1
3
2 1
e 2 t dt
0.8452
1 t 1
2( 2) 1
k 2 : a2
2 1
e 2 P2 (t ) dt
1 t 1
2 .
5 1
(3t 1) dt
2
e
2 1 2
5
0.0559
2
0.1398
Thus,
f (t ) a0 P0 (t ) a1P1 (t ) a2 P2 (t )
1
1.7183 0.8452t (0.1398) (3t 2 1)
2
1.6484 0.8452t 0.8388t 2
Hence,
f ( x ) 1.6484 0.8452( 2 x 1) 0.8388( 2 x 1)2
1.3097 14.7872x 13.0968x 2
Warm up exercise
Find the least-squares polynomial approximation of degree two to:
(i) f ( x ) sin 2x on the interval [-1,1] and [0, 1]
84
THE METHOD OF LEAST SQUARES PART 3
Exercise 5
1. Find the values of A and B to fit a curve of the following form to the given data.
1 A
a) y b) y B
Ax B x
x -1 0.1 1 2 3
y 6.62 3.94 2.17 1.35 0.89
3. One of the following data sets, (x,y1) and (x,y2) , follows an exponential law
y Ae bx and the other follows a power law y Ax b . Which is which? Fit the
data to the respective model and calculate the sum square errors.
4. Find the least-squares polynomial approximation of degree one to the function f(x)
on the indicated interval.
a) f ( x ) e 2 x ; [-1, 1] d) f ( x ) sin x ; [-1, 1]
1
b) f (x) ; [1, 3] e) f ( x ) cos 2x f(x); [0, 0.5] ,
x
c) f ( x ) x 3 1 ; [0,2] f) f ( x ) ln x ; [1, 2]
85
THE METHOD OF LEAST SQUARES PART 3
a) Find the least-squares polynomial approximation of degree one in 0,
2
and , .
2
b) Find the least-squares polynomial approximation of degree two in [0, ].
c) Plot the graph to show the polynomials in a) and b) together with the
function f(x).
d) Approximate f(0.2) and f(2) using a) and b). Which polynomial gives a
better approximation.
e) Calculate the total least-squares error for a) and b).
f) If f(x) is approximated using least-squares polynomial of degree three
i.e., f ( x ) ao a1 x a2 x 2 a3 x 3 , find a3 .
86