Академический Документы
Профессиональный Документы
Культура Документы
Page
51
Prepared By
Er. Shree Krishna Khadka
f(x) = a + bx
ai
(Xi, yi)
a
x
Fig: Least Square Regression
Consider a point (xi, yi) as shown in figure. The vertical distance of this point from
the line f(x) = a + bx is the error: ai. Then ai = yi f(xi) = yi a bxi .. (i)
The best approach that could be tried for fitting a best line through the data is to
minimize the sum of squares of errors.
ai 2 = (yi a bxi )2
Page
The technique of minimizing the sum of square of errors is known as least square
regression.
52
i.e.
Prepared By
Er. Shree Krishna Khadka
n
2
i=1 a i
n
i=1(yi
a bxi )2 (i)
In this method of least square, we choose a and b such that Q is minimum. Since,
Q depends on a and b, a necessary condition for Q to be minimum is given by:
Q
=0
&
a bxi ) = 0
&
=0
Then:
Q
a
i.e.
= 2
n
i=1(yi
yi = a + b
xi . (ii)
&
= 2xi
xi yi = a
n
i=1(yi
a bxi ) = 0
xi + b
xi 2 (iii)
Equations, (ii) and (iii) are called normal equations, solving for a and b, we get:
b=
xi yi xi yi
n x i 2 ( x i )2
. (iv)
&
a=
yi
n
xi
n
= y bx (v)
Example
Fit a straight line to the following set of data.
x 1 2 3 4 5
y 3 4 5 6 8
Solution:
The various summations are calculated as below:
1
4
9
16
25
xi 2 = 55
3
8
15
24
40
xi yi = 90
Here, n = 5
On calculation using above formula, we get: b = 1.2 and a = 1.6
Therefore, the required linear equation is: y = a + bx = 1.6 + 1.2x
Prepared By
Er. Shree Krishna Khadka
53
yi
3
4
5
6
8
yi = 26
Page
xi
1
2
3
4
5
xi = 15
(iv)
Now, comparing equation (iv) with standard linear equation, y = a + bx, we found:
y ln y , b b, a ln a &
() . (v)
Then, obviously we will have the following results:
ln x i ln (y i ) ln (x i ) ln (y i )
n {ln (x i )}2 { ln (x i )}2
ln a = R =
i.e.
ln (y i )
n
a = eR = exp
. (vi) &
ln (x i )
n
ln(yi )
ln(x )
b n i
n
. (vi)
Prepared By
Er. Shree Krishna Khadka
54
Page
b=
Example:
Given the data table:
x 1 2 3 4
5
y 0.5 2 4.5 8 12.5
Fit a power function model of the form y = axb.
Solution:
xi
1
2
3
4
5
= 15
yi
0.5
2
4.5
8
12.5
= 27.5
ln(xi)
0
0.6931
1.0986
1.3863
1.6094
= 4.7869
{
( )} ln(xi).ln(yi)
0
0
0.4804
0.4804
1.2069
1.6524
1.9218
2.8827
2.5901
4.0649
= 6.1992
= 9.0804
ln(yi)
-0.6931
0.6931
1.5641
2.0794
2.5257
= 6.1092
Now:
b=
ln(a) =
ln x i ln (y i ) ln (x i ) ln (y i )
n {ln (x i )}2 { ln (x i )}2
ln (y i )
n
ln (x i )
n
27.5
5
=b=
59.0804 4.78696.1092
2.2832
56.19924.7869 2
15
5
= 2.2832
= 0.9641
Prepared By
Er. Shree Krishna Khadka
Page
Solution:
Hint: As T = b.et/4 +a (i), we can write this equation in the form: y = b.f(x) +a .
(ii), where, y = T and f(x) = et/4. Now, use linear formula and adjust the values at
final. Ans: 37.56et/4+21.31
55
Q
a 1
= 0;
a 2
= 0; . a = 0;
m
= 2
n
i=1{yi
f xi }
f(x i )
a j
=0;
Where:
f(x i )
a j
= xi j1
Thus we have:
n
i=1{yi
Or
n
i=1
f xi } xi j1 = 0 ;
yi . xi j1 xi j1 . f xi
= 0;
Where: j = 1, 2, . m
. (iii)
+ a2 x + a3 x 2 + a4 x 3 + . +am x m1 ) =
n
j1
i=1 yi . xi
yi
= xi yi
..
..
xi 2m2 = a3
Prepared By
Er. Shree Krishna Khadka
xi m1 yi
Page
a1 n + a2 xi + a3 xi 2 + + am xi m1 =
a1 xi + a2 xi 2 + a3 xi 3 + + am xi m1
..
..
m1
m
m+1
a1 x i
+ a2 xi + a3 xi
+ + am
56
So, there are m equations: (j=1, 2, 3, . ,n) and each summation is for i =1 to i =n.
#Note: For a second order polynomial, we will have the following simultaneous
equations:
a1 n + a2 xi + a3 xi 2 = yi
a1 xi + a2 xi 2 + a3 xi 3 = xi yi
a1 xi 2 + a2 xi 3 + a3 xi 4 = xi 2 yi
Example:
Fit a second order polynomial to the data in the table below:
x 1 2 3 4
y 6 11 18 27
Solution:
Below is the tabulation the values as second order polynomial components.
x
1
2
3
4
= 10
y
6
11
18
27
= 62
x2
1
4
9
16
= 30
x3
1
8
27
64
= 100
x4
1
16
81
256
= 354
xy
6
22
54
108
= 190
x 2y
6
44
162
432
= 644
Prepared By
Er. Shree Krishna Khadka
Page
57
Equations:
4a1 + 10.3a2 + 31.65a3 = 11.5
(i)
10.3a1 + 31.65a2 + 107.029a3 = 32.85
(ii)
31.65a1 + 107.029a2 + 381.3057a3 = 107.745 (iii)
n
i=1{yi
f x, z }2 =
n
i=1{yi
a1 + a2 xi + a3 zi }2 (ii)
Differentiation equation (ii) w. r. t. a1, a2, a3 and equating them to zero, we will
get the condition for minimum error.
Q
a 1
Q
a 2
Q
a 3
= 2 {yi a1 + a2 xi + a3 zi } = 0
= 2 {yi a1 + a2 xi + a3 zi xi } = 0
= 2 {yi a1 + a2 xi + a3 zi zi } = 0
xi yi
yi zi
Example:
Given the table of data:
x 1 2 3
4
z 0 1 2
3
y 12 18 24 30
Page
58
Solution:
x
1
2
3
4
= 10
z
0
1
2
3
= 62
y
12
18
24
30
= 84
x2
1
4
9
16
= 30
z2
0
1
4
9
= 14
z.x
0
2
6
12
= 20
x.y
12
36
72
120
= 240
y.z
0
18
48
90
= 156
Page
59
Ans: y = 10 2x + 5z
Prepared By
Er. Shree Krishna Khadka
SPLINE INTERPOLATION:
Data fitting by means of polynomials has been considered in the previous exercise.
From the viewpoint of cubic splines, the resulting approximation called the cubic
spline approximation, suffers from the disadvantage of being a global
approximation, which means that a change in one point affects the entire
approximation curve. So, a method based on the basis splines, which posses a local
character, viz; a change in one point introduces a change only in the immediate
neighbourhood of that point, called B-spline method that finds important
application in computer graphics and smoothing of data.
Cubic B-Splines:
It resembles the ordinary cubic spline in which a separate cubic is derived for each
interval. Specially, a cubic B-spline (or a B-spline of order-4) denoted by B4i(x), is a
cubic spline with knots Ki-4, Ki-3, Ki-2, Ki-1 and Ki, which is zero everywhere except in
the range Ki-4 < x < Ki. In such case, B4i(x) is said to have a support [Ki-1, Ki]. It may
be noted that a B-spline need not necessarily pass through any or all of the data
points.
Cubic splines are popular because of their ability to interpolate data with smooth
curves. If we consider the construction of a cubic spline function which would
interpolate the points (x0, f0), (x1, f1) (xn, fn). The cubic spline S(x) consists of (n1) cubic corresponding to (n-1) sub intervals. If we denote such cubic by Si(x),
then:
S(x) = Si(x); for I = 1, 2, n.
These cubic must satisfy the following conditions:
a) S(x) must interpolate the function at all the points: x0 , x1, . xn; i.e. for each
i', S(xi) = fi.
b) The function values must be equal at all the interior knots, i.e. Si(xi) = Si+1(xi)
c) The first derivative at the interior knots must be equal; Si(x) = Si+1(x)
d) The second derivative at the interior knots must be equal; Si(xi) = Si+1(xi)
e) The second derivative at the end points are zero; S(x0) = S(xn) = 0
. (i)
Page
Then:
60
Let us consider (n+1) sample points with n number of different section as shown
in the figure below. Suppose, gi(x) represents a cubic polynomial between [xi, f(xi)]
and [xi+1, f(xi+1)].
Prepared By
Er. Shree Krishna Khadka
g(xi)
F(x)
g(xi+1)
xi-2
xi-1
xi
xi+1
x
x
1.) Since, function must pass through all the points; i.e; gi(xi) = di = f(xi) .(i)
2.) Differentiating equation (i) w. r. t. x we get:
Similarly:
gi(xi+1) = Si+1 = 6ai(xi+1 - xi) + 2bi = 6aihi + 2bi
Where; xi+1 - xi = hi
Then; ai = (Si+1 Si)/6hi . (v)
3.) Again, gi(xi+1) = f(xi+1);
But, f(xi+1) = aihi3 + bihi2 + Cihi + di = aihi3 + bihi2 + Cihi + f(xi)
i.e. Ci = [{f(xi+1) f(xi)}/h] - aihi2 - bihi .(vi)
4.) Also from equation (ii) we have: gi-1(x) = 3ai-1(x-xi-1)2 + 2bi-1(x-xi-1) + Ci-1 (vii)
And we also have the condition that, at x = xi, gi(xi) = gi-1(xi), then:
Ci = 3ai-1(hi-1)2 + 2bi-1(hi-1) + Ci-1 . (viii)
5.) Finally on manipulating all these values, we get a final equation as:
hi1 Si1 + 2 hi1 + hi S + hi Si+1 = 6
f i+1 f i
hi
f i f i1
h i1
0.
f x1 , x2 f x0 , x1
S1
h2
S2 =6 f x2 , x3 f x1 , x2
S
2 h2 + h3 3
f x3 , x4 f x2 , x3
Prepared By
Er. Shree Krishna Khadka
61
h1
2(h1 + h2 )
h2
Page
2(h0 + h1 )
h1
0
points that may or may not be equal. For natural spline, we have: S0 =Sn = 0
(Second order derivative at end points is zero)
Example:
Fit the data of table with a natural cubic spline curve and evaluate the spline
values, g(0.66) and g(1.75), with h0 = 1, h1 = 0.5 and h2 = 0.75
x 0
1
1.5
2.25
f(x) 2 4.4366 6.7134 13.9130
(Hint: The relation is described as: f(x) = 2ex x2)
Solution:
1.) From the given table, we have:
h0 = x1 x0 = 1 0 = 1
h1 = x2 x1 = 1.5 1 = 0.5
h2 = x3 x2 = 2.25 1.5 = 0.75
2.) For natural spline, S0 = 0 and S3 = 0
3.) Now, Finding Si = S1 and S2. Generating Matrix Model as follow:
2(h0 + h1 )
h1
f x1 , x2 f x0 , x1
S1
=6
h1
2(h1 + h2 ) S2
f x2 , x3 f x1 , x2
i.e.
2(1 + 0.5)
0.5
0.5
S1
12.702
=6
2(0.5 + 0.75) S2
30.2748
i.e. 3S1 + 0.5S2 = 12.702 (i) and 0.5S1 + 2.5S2 = 30.2742 (ii)
On solving: S1 = 2.292 and S2 = 11.65
4.) Now, finding ai, bi, ci and di
1
3.12
1.146
3.200
4.4366
2
-2.598
5.825
6.680
6.7134
62
0
0.381
0
2.0556
2
Page
i
ai = (Si+1 Si)/6hi
bi = Si/2
Ci = [{f(xi+1) f(xi)}/h] - aihi2 - bihi
di = f(xi)
Home Work:
Given the data points:
i
0 1 2
xi 4 9 16
f(xi) 2 3 4
Estimate the value of f(7)
Solution:
h0 = 5 and h1 = 7
S0 = 0 = S2, but S1 = -0.01428
a0 = -0.000476, a1 = 0.00034
b0 = 0, b1 = -0.00714
c0 = 0.2119, c1 = 0.6259
d0 = 2, d1 = 3
APPROXIMATION OF FUNCTIONS:
The problem of approximating a function is a central problem in numerical
analysis due to its importance in the development of software for digital
computers. Functions evaluation through interpolation techniques over stored
table of values has been found to be quite costlier, when compared to the use of
efficient function approximations.
If f1, f2 fn be the values of the given function and g1, g2, gn be the corresponding
values of the approximating function; then the error vector e, where the
components of e is given by: ei = fi gi ..(i).
The approximation may be chosen in a number of ways. For example, we may find
Page
63
Prepared By
Er. Shree Krishna Khadka
Assignment 4
Full Marks: 25
Pass Marks: 15
40
600.5
50
600.6
60
600.8
70
600.9
[5]
80
601.0
1
70
2
83
3
100
4
124
1
2
2.1
2.5
3.2
3.0
[5]
[5]
4
4
5
3
15
4
-2
8
3
-1
-1
2
4
26
1
0
8
Use multiple linear regression formula to fit the data of the form:
y = a1 + a2(x) +a3(z) & Find f(5.3) and f(3.7).
[5]
5. Fit a natural cubic B-Spline, S to the data given below in the table.
[5]
x
f(x)=x3-1
1.2
-2
2.8
-1
3.5
0
4.1
1
5.9
2
Page
64
Prepared By
Er. Shree Krishna Khadka