Numerical Methodes - Chapter 4
Numerical Methodes - Chapter 4
4.Curve Fitting
4.1 Introduction
Fitting of curves to a set of numerical data is of
considerable importance- theoretical as well as practical. Theoretically it
is useful in the study of correlation and regression. In practice it enables
us to represent the relationship between two variables by simple
algebraic expressions (polynomials, exponential, or logarithmic functions
or any). Moreover, it may be used to estimate the values of one variable
which would correspond to the specified values of the other variable(s).
This chapter covers how to fit a curve for a given set of data points using
different methods. We will focus on the points of
Regression
- linear regression
- quadratic regression
- polynomial regression
- multiple regression
1
In most of the fields of engineering and science, we
come across experiments which involve many variables, and most of the
time data is collected or given for discrete values along a continuum; the
relation between these variables can be discussed so easily and for many of
variables:
little bit difficult because to write one variable in terms of the other
2
Suppose (xi , y i ) , i = 1,2,3,..., n be n sets of observations and the
(xi , yi ) . Actually, here we may have different approaches to fit the given
data, and one system may approximate better than the other system on
the arbitrary constants ' a ' and ' b '. The problem now is to determine ' a '
and ' b ' so that the line (1) is the line of "best fit". The term best fit is
which consists of the deviations of the actual values as given by the line
of best fit. As a matter of chance all the points may lie on a straight line
and in this case the line is a 'perfect fit' and the sum of the squares of
3
Let Pi ( xi , y i ) be any point in the scatter diagram. Draw
Pi H i = Pi M − H i M
= y i − (a + bxi )
so that
n n
E = ∑ Pi H i = ∑ ( y i − a − bxi ) 2 is the minimum.
2
i =1 i =1
vanish separately.
∂E n
That is, = −2∑ ( yi − a − bxi ) = 0
∂a i =1
n n n
⇒ ∑ y = ∑ a + b∑ x
i =1
i
i =1 i =1
i
n
= na + b ∑ xi (i )
i =1
4
and
∂E n
= −2∑ xi ( y i − a − bxi ) = 0
∂b i =1
. n n n
⇒ ∑ x i y i = a ∑ x i + b ∑ xi
2
(ii )
i =1 i =1 i =1
Solving for a and b from (i) and (ii), we get the values of a and b , and
with these values of a and b so obtained, equation (1) is the line of best
x: 5 10 15 20 25
y: 15 19 23 26 30
Solution
n n
∑ y i = 5a + b ∑ x i
i =1 i =1
n n n
∑ xi yi = a ∑ xi + b∑ xi
i =1 i =1 i =1
2
5
We calculate ∑ x, ∑ y, ∑ x , ∑ xy
2
and form the table below
x y x2 xy
5 15 25 75
10 19 100 190
15 23 225 345
20 26 400 520
25 30 625 750
75 114 1375 1885
Using these values in the normal equations, we get
5a + 75b = 114
75a + 1375b = 1885
Solving for a and b we get a=12.3 and b=0.7 and thus the line of best fit
is y = 12.3 + 0.7 x
Example 2. Find the best fitting straight line to the data given below
x is 70.
x: 71 68 73 69 67 65 66 67
y: 69 72 70 70 68 67 68 64
Solution
b ∑ X + 8a = ∑ Y
b∑ X 2 + a ∑ X = ∑ XY
6
Calculations:
x y X Y X2 XY
71 69 3 -1 9 -3
68 72 0 2 0 0
73 70 5 0 25 0
69 70 1 0 1 0
67 68 -1 -2 1 2
65 67 -3 -3 9 9
66 68 -2 -2 4 4
67 64 -1 -6 1 6
2 -12 50 18
2b + 8a = −12
50b + 2a = 18
35 16
Solving for a and b , we get b = and a =
99 99
35 16
Thus the line of best fit is of the form Y = X+
99 99
35 16
This implies y − 70 = ( x − 68) +
99 99
35 4566
y= x+
99 99
When x = 70 ⇒ y = 70.87
7
4.2.2 QUADRATIC REGRESSION
∂E
( )
n
= −2∑ yi − a − bxi − cxi2 = 0
∂a i =1
n n n
⇒ ∑ yi = na + b∑ xi + c ∑ xi2 …………………. (1)
i =1 i =1 i =1
∂E
( )
n
= −2∑ yi − a − bxi − cxi2 xi = 0
∂b i =1
n n n n
⇒ ∑ xi yi = a ∑ xi + b ∑ xi2 + c ∑ xi3 ........................( 2)
i =1 i =1 i =1 i =1
∂E
( )
n
= −2∑ yi − a − bxi − cxi2 xi2 = 0
∂c i =1
n n n n
⇒ ∑ xi2 yi = a ∑ xi2 + b∑ xi3 + c ∑ xi4 .......................(3)
i =1 i =1 i =1 i =1
Solving for a , b, and c from (1), (2) and (3), we get with these values of
8
Example 1 Fit a parabola of second degree to the following data
X: 0 1 2 3 4
Solution
X Y X2 X3 X4 XY X2Y
0 1 0 0 0 0 0
Exercise Find the best fitting parabola to the data given below by the
x: 71 68 73 69 67 65 66 67
y: 69 72 70 70 68 67 68 64
9
4.2.3 POLYNOMIAL REGRESSION
FITTING OF A POLYNOMIAL OF KTH DEGREE
obtained so that
( )
n
E = ∑ yi − a0 − a1 xi − a2 xi2 ...ak xik
2
is minimum.
i =1
separately.
∂E
= 0 ⇒ ∑ yi = na0 + a1 ∑ xi + a2 ∑ xi2 + ... + ak ∑ xik
∂a0
∂E n
= 0 ⇒ ∑ xi yi = a0 ∑ xi + a1 ∑ xi2 + ... + ak ∑ x k +1
∂a1 i =1
∂E n
= 0 ⇒ ∑ xik yi =a0 ∑ xik + a1 ∑ xik +1 + ... + ak ∑ xi2 k
∂ak i =1
Exercise Find the polynomial of degree three that best fits the data
x: 71 68 73 69 67 65 66 67
y: 69 72 70 70 68 67 68 64
10
4.2.4. MULTIPLE REGRESSION
There are different multiple regression forms. For the sake of discussion
n
E = ∑ ( zi − axi − bxi yi − cyi )
2
i =1
∂E n
= −2∑ ( zi − axi − bxi yi − cyi )xi = 0
∂a i =1
n n
⇒ ∑ xi zi = a ∑ xi2 + b ∑ xi2 yi + c∑ xi yi ..............................(1)
i =1 i =1
∂E n
= −2∑ ( zi − axi − bxi yi − cyi )xi yi
∂b i =1
n n n n
⇒ ∑ zi xi yi = a ∑ xi2 yi + b∑ xi2 yi2 + c ∑ xi yi2 ..............................( 2)
i =1 i =1 i =1 i =1
∂E n
= −2∑ ( zi − axi − bxi yi − cyi )yi
∂b i =1
n n n n
⇒ ∑ zi yi = a ∑ xi yi + b∑ xi yi2 + c ∑ yi2 ..............................(3)
i =1 i =1 i =1 i =1
By solving (1), (2) and (3) for a ,b, and c , we get the best approximation.
11
4.3 INTERPOLATION
12
f ( x, x0 , x1 ) − f ( x0 , x1 , x2 )
f ( x, x0 , x1 , x2 ) =
x − x2
∴ f ( x, x0 , x1 ) = f ( x0 , x1 , x2 ) + ( x − x2 ) f ( x, x0 , x1 , x2 )
⇒ f ( x ) = f ( x0 ) + ( x − x0 ) f ( x0 , x1 ) + ( x − x0 )( x − x1 ) f ( x0 , x1 , x2 ) +
(x − x0 )(x − x1 )(x − x2 ) f (x, x0 , x1 , x2 )
Continuing in this fashion, we get
f ( x ) = f ( x 0 ) + (x − x 0 ) f ( x0 , x1 ) + ( x − x0 )( x − x1 ) f ( x 0 , x1 , x 2 ) + ... +
(x − x0 )(x − x1 )...(x − x n ) f (x, x0 , x1 , x 2 ,..., x n ).................................(∗)
f ( x ) = f ( x 0 ) + ( x + x0 ) f ( x0 , x1 ) + ( x − x0 )( x − x1 ) f ( x0 , x1 , x 2 ) + ... +
(x − x0 )(x − x1 )...(x − x n−1 ) f (x0 , x1 , x2 ,..., xn )
This equation is called Newton’s divided difference interpolation formula for
unequal intervals.
then x − x1 = ( x − x0 ) − ( xi − x0 ) = uh − h = (u − 1)h
x − x2 = ( x − x0 ) − ( x2 − x0 ) = uh − 2h = (u − 2 )h,...
Let y 0 = f ( x0 )
13
u (u − 1) 2
∴ f ( x ) = y 0 + u ∆y 0 + ∆ f ( x0 ) + ... this is exactly Newton’s Gregory forward
2!
Examples
x: 4 5 7 10 11 13
Solution
x f ( x) ∆f ( x ) ∆2 f ( x ) ∆3 f ( x ) ∆4 f ( x )
4 48
52
15
5 100
97 1
7 294 21
202 0
1
10 900 27
310 0
33 1
11 1210
409
13 2028
f ( x) = f ( x0 ) + ( x − x0 ) f ( x0 − x1 ) + ( x − x 0 )( x − x1 ) f ( x0 , x1 , x 2 ) + ... +
14
2. From the following table find f ( x ) and hence f (12) using Newton’s
interpolation formula.
x: 1 2 7 8
f ( x) : 1 5 5 4
Solution
x f ( x) ∆f ( x ) ∆2 f ( x ) ∆3 f ( x ) ∆4 f ( x )
1 1
4 4
2 5 −
0 6 1
7 5 1 14
−
-1 6
8 4
f ( x ) = f ( x0 ) + ( x − x0 ) f ( x0 , x1 ) + ( x − x0 )( x − x1 ) f ( x0 , x1 , x2 ) + (x − x0 )( x − x1 )( x − x2 ) f ( x0 , x1 , x2 , x3 )
∴ f (12 ) = 10.95238095
3. Find the function f from the following table and find f(3)
x: 0 1 2 4 5 7
Solution
15
f (x )
⇒ φ (x ) =
x( x − 1)( x − 4 )
− 12
Now φ (2 ) = =3
2(2 − 1)(2 − 4 )
f (5) f (7 )
φ (5) = and φ (7 ) =
600
= = 30 = 58
5(4 )(1) 20 7(6 )(3)
x φ (x ) ∆f ( x ) ∆2 f ( x )
2 3
9
5 30 1
14
7 58
= 3 + 9 x − 18 + x 2 − 7 x + 10 = x 2 + 2 x − 5
Hence, f ( x ) = x( x − 1)( x − 4 )( x 2 + 2 x − 5)
x: 0 2 3 4 7 9
Solution
Since the third differences are constants, we extend the table by introducing
16
x f ( x) ∆f ( x ) ∆2 f ( x ) ∆3 f ( x )
0 4
11
2 26 7
32 1
3 58 11
1
54
4 112 16
118 1
7 466 22
b − 22
228 =1
p − 228 1
9 922 =b
a − 922 −2
= p d −b
5 a −4
q− p 5−7
=d
5 q 5−9
k −d
k =1
5 5−9
b − 22 p − 228
= 1 ⇒ b = 23 = 23 ⇒ p = 182
1 −2
a − 922
= 182 ⇒ a = 194
−4
d −b q− p
= 1 ⇒ d = 21, = 21 ⇒ q = 98
−2 −4
k −d
= 1 ⇒ k = 17
−4
Now
= a + ( x − 5)q + ( x − 5) k + ( x − 5)
2 3
17
5. Using the following table, find f ( x ) as a polynomial in power of ( x − 6) .
x: -1 0 2 3 7 10
Solution
Since the third differences are constants we extend the table by introducing
x f ( x) ∆f ( x ) ∆2 f ( x ) ∆3 f ( x ) ∆4 f ( x )
-1 -11
12
0 1 -4
1
0
2 1 0 0
0 1
3 1 7 0
35 1
7 141 15
140 q − 15
p − 140 =1
10 561 =q 3
a − 561 −1
= p r−q
6 a −4 =1
b− p −1
=r
6 b −4
c−r
6 c =1
−4
q − 15 p − 140
= 1 ⇒ q = 18 = 18 ⇒ p = 122
3 −1
a − 561
= 122 ⇒ a = 73
−4
r−q
= 1 ⇒ r = 17
−4
18
b− p
= r ⇒ b = 122 − 4(17 ) = 54
−4
c−r
= 1 ⇒ c = 17 − 4 = 13
−4
Thus, f (5) = 31 .
Newton can be used only when the values of independent variable x are
cases where the values of independent variable are not equally spaced and in
cases when the differences of dependent variable are not small, ultimately, we
f ( x) = a 0 ( x − x1 )( x − x 2 )...( x − x n ) + a1 ( x − x0 )( x − x 2 )( x − x3 )...( x − x n ) +
a 2 ( x − x0 )( x − x1 )( x − x3 )...( x − x n ) + ... + a i ( x − x 0 )( x − x1 )...( x − xi )( x − xi +1 )...( x − x n )
+ ... + a n ( x − x0 )( x − x1 )( x − x 2 )...( x − x n −1 ) ..........(1)
19
The main point now what it remains is to decide the value of a i ' s .
y 0 = a 0 ( x0 − x1 )( x0 − x 2 )...( x0 − x n )
y0
∴ a0 =
( x0 − x1 )( x 0 − x 2 )...( x0 − x n )
y1 = a1 ( x1 − x0 )( x1 − x 2 )...( x1 − x n )
y1
∴ a1 =
( x1 − x0 )( x1 − x 2 )...( x1 − x n )
y2
∴ a2 =
( x 2 − x0 )( x 2 − x1 )( x 2 − x3 )...( x 2 − x n )
……..
yn
∴ an =
( x n − x0 )( x n − x1 )...( x n − x n −1 )
( x − x1 )( x − x 2 )...( x − x n ) ( x − x0 )( x − x 2 )( x − x3 )...( x − x n )
f ( x) = y0 + y1 +
( x0 − x1 )( x 0 − x 2 )...( x0 − x n ) ( x1 − x0 )( x1 − x 2 )...( x1 − x n )
( x − x0 )( x − x1 )( x − x3 )...( x − x n ) ( x − x0 )( x − x1 )( x − x 2 )...( x − x n −1 )
y 2 + ... + y n .....(2)
( x 2 − x0 )( x 2 − x1 )( x 2 − x3 )...( x 2 − x n ) ( x n − x0 )( x n − x1 )( x n − x 2 )...( x n − x n −1 )
20
Examples
1. Using Lagrange’s interpolation formula, find y (8) from the following table
x: 5 6 9 11
y: 12 13 14 16
Solution
y (8) = 13.7
2. Find the parabola that passes through the points (1,2), (3,12), (6,30).
Solution
1 2
y= ( x + 21x − 12)
5
the values of the frequency density f ( x ) for x = 8.9, 9, and 9.3 are
respectively 0.30, 0.35, and 0.25. Calculate the approximate value of the
mode.
Solution
21
By Lagrange’s interpolating formula, we have
1
y= ( −25 x 2 + 453.5 x − 2052.3)
12
1
f ′( x ) = ( −50 x + 453.5) = 0
12
⇒ x = 9.07
50
f ′′( x) = − <0
12
corresponding value of y for some x . Now if we change the problem to find the
corresponding value of x for a given value of y ; this process of finding the value
The main point here is to interchange the dependent and independent variable
22
23
24
25