You are on page 1of 20

Numerical

Methods
Lecture-14
[MA-200]
ARISHA ALI
Curve Fitting
Curve Fitting
It is a process of finding “best fit”
curve, since different approximation
curves can be obtained for the same
data.
Least Square Method

 𝑦 = 𝑎 + 𝑏𝑥 (Straight Line or Linear)


 𝑦 = 𝑎 + 𝑏𝑥 + 𝑐𝑥 2 (Parabola or quadratic)
Curve Fitting by Method of Least
Square for Straight Line
Let the point (𝑥1 , 𝑦1 ) (𝑥2 , 𝑦2 )… (𝑥𝑛 , 𝑦𝑛 )
Let 𝑦 = 𝑎 + 𝑏𝑥 ---------- (1)
Is the equation of straight line to fitted above data here
σ 𝑦𝑖 = 𝑛𝑎 + 𝑏 σ 𝑥𝑖 ------- (2)
σ 𝑥𝑖 𝑦𝑖 = 𝑎 σ 𝑥𝑖 + 𝑏 σ 𝑥𝑖2 ---------- (3)
Eq (3) and Eq (2) are called the normal equations.
Working Rule of Straight-Line fitting
 Let the point 𝑥1 , 𝑦1 , (𝑥2 , 𝑦2 )… (𝑥𝑛 , 𝑦𝑛 ) are the given data
 Find σ 𝑥 , σ 𝑦 , σ 𝑥𝑦 , σ 𝑥 2
 Construct the normal equation for 𝑎 and 𝑏
σ 𝑦𝑖 = 𝑛𝑎 + 𝑏 σ 𝑥𝑖
σ 𝑥𝑖 𝑦𝑖 = 𝑎 σ 𝑥𝑖 + 𝑏 σ 𝑥𝑖2
 Solve the normal equations and find 𝑎 and 𝑏
 Put them into equation of line i.e., 𝑦 = 𝑎 + 𝑏𝑥
Curve Fitting by Method of Least
Square for Parabola
Let the point (𝑥1 , 𝑦1 ) (𝑥2 , 𝑦2 )… (𝑥𝑛 , 𝑦𝑛 )
Let 𝑦 = 𝑎 + 𝑏𝑥 + 𝑐𝑥 2 ---------- (1)
Is the equation of parabola to fitted above data here
σ 𝑦𝑖 = 𝑛𝑎 + 𝑏 σ 𝑥𝑖 + 𝑐 σ 𝑥 2 ------- (2)
σ 𝑥𝑖 𝑦𝑖 = 𝑎 σ 𝑥𝑖 + 𝑏 σ 𝑥𝑖2 + 𝑐 σ 𝑥 3 ---------- (3)
σ 𝑥𝑖2 𝑦𝑖 = 𝑎 σ 𝑥𝑖2 + 𝑏 σ 𝑥𝑖3 + 𝑐 σ 𝑥𝑖4 --------- (4)
Eq (2), (3) and Eq (4) are called the normal equations.
Working Rule of Parabola Curve
fitting
 Let the point 𝑥1 , 𝑦1 , (𝑥2 , 𝑦2 )… (𝑥𝑛 , 𝑦𝑛 ) are the given data
 Find σ 𝑥 , σ 𝑦 , σ 𝑥𝑦 , σ 𝑥 2 , σ 𝑥 2 𝑦, σ 𝑥 3 , σ 𝑥 4
 Construct the normal equation for 𝑎, 𝑏 and 𝑐
σ 𝑦𝑖 = 𝑛𝑎 + 𝑏 σ 𝑥𝑖 + 𝑐 σ 𝑥 2
σ 𝑥𝑖 𝑦𝑖 = 𝑎 σ 𝑥𝑖 + 𝑏 σ 𝑥𝑖2 + 𝑐 σ 𝑥 3
σ 𝑥𝑖2 𝑦𝑖 = 𝑎 σ 𝑥𝑖2 + 𝑏 σ 𝑥𝑖3 + 𝑐 σ 𝑥𝑖4
 Solve the normal equations and find 𝑎, 𝑏 and 𝑐
 Put them into equation of parabola i.e., 𝑦 = 𝑎 + 𝑏𝑥 + 𝑐𝑥 2
Example # 1
Using the method of least square, find the straight line 𝑦 = 𝑎 + 𝑏𝑥, that fit the
following data.

x 0.5 1.0 1.5 2.0 2.5 3.0


y 15 17 19 14 10 7
Solution:
The normal equations of least square fit are
σ 𝑦 = 𝑛𝑎 + 𝑏 σ 𝑥
σ 𝑥𝑦 = 𝑎 σ 𝑥 + 𝑏 σ 𝑥 2
x y xy 𝒙𝟐
0.5 15 7.5 0.25
1.0 17 17.0 1.00
1.5 19 28.5 2.25
2.0 14 28.0 4.00
2.5 10 25.0 6.25
3.0 17 21.0 9.00

෍ 𝑥 = 10.5 ෍ 𝑦 = 82 ෍ 𝑥𝑦 = 127 ෍ 𝑥 2 = 22.75


Put values in normal equation
𝑛=6
σ 𝑦 = 𝑛𝑎 + 𝑏 σ 𝑥 ⟹ 82 = 6𝑎 + 10.5𝑏------ (1)
σ 𝑥𝑦 = 𝑎 σ 𝑥 + 𝑏 σ 𝑥 2 ⟹ 127 = 10.5𝑎 + 22.75𝑏------- (2)
6𝑎 + 10.5𝑏 = 82
82−10.5𝑏
𝑎= 6

Put value of 𝑎 in equation (2)


127 = 10.5𝑎 + 22.75𝑏
82−10.5𝑏
127 = 10.5 + 22.75𝑏
6

127 = 143.5 − 18.375𝑏 + 22.75𝑏


𝑏 = −3.7714
82−10.5𝑏 82−10.5(−3.7714)
𝑎= 6
= 6
⟹ 𝑎 = 20.2667

Hence the required fit for the given data is


𝑦 = 𝑎𝑥 + 𝑏 ⟹ 𝑦 = 20.2667 − 3.7714𝑥
Error Evaluation
i 𝑥𝑖 𝑦𝑖 𝑔 𝑥𝑖 = 𝑎𝑥𝑖 + 𝑏 𝐷𝑒𝑣𝑖𝑎𝑡𝑖𝑜𝑛 = 𝑦𝑖 − 𝑔(𝑥𝑖 )

1 0.5 15 −3.7714(0.5) + 20.2667 = 18.381 15 − 18.381 = −3.381


2 1.0 17 −3.7714(1.0) + 20.2667 = 16.4953 17 − 16.4953 = 0.5047
3 1.5 19 −3.7714 1.5 + 20.2667 = 36.3043 19 − 36.3043 = 55.3043
4 2.0 14 −3.7714 2.0 + 20.2667 = 12.7239 14 − 12.7239 = 1.2761
5 2.5 10 −3.7714 2.5 + 20.2667 = 10.8382 10 − 10.8382 = −0.8382
6 3.0 17 −3.7714 3.0 + 20.2667 = 8.9525 17 − 8.9525 = 8.0475
Example # 2
Applying the method of least square to find an equation of the form
𝑦 = 𝑎𝑥 + 𝑏𝑥 2 that fit the following data

x 1 2 3 4 5 6
y 2.6 5.4 8.7 12.1 16.0 20.2
Solution:
𝑦 = 𝑎𝑥 + 𝑏𝑥 2 ⟹ 𝑦 = 𝑥(𝑎 + 𝑏𝑥)
𝑦
= 𝑎 + 𝑏𝑥 ⟹ 𝑌 = 𝑎 + 𝑏𝑥
𝑥
x Y xY 𝑥2
1 2.6 2.6 1
2 2.7 5.4 4
3 2.9 8.7 9
4 3.025 12.1 16
5 3.2 16.0 25
6 3.367 20.2 36

෍ 𝑥 = 21 ෍ 𝑦 = 17.792 ෍ 𝑥𝑦 = 65.0 ෍ 𝑥 2 = 91
Put values in normal equation
𝑛=6
σ 𝑦 = 𝑛𝑎 + 𝑏 σ 𝑥 ⟹ 17.792 = 6𝑎 + 21𝑏------ (1)
σ 𝑥𝑦 = 𝑎 σ 𝑥 + 𝑏 σ 𝑥 2 ⟹ 65.0 = 21𝑎 + 91𝑏------- (2)
𝑎 = 2.41973, 𝑏 = 0.15589
𝑌 = 0.15589𝑥 + 2.41973
𝑦
𝑌 = 𝑥 = 0.15589 + 2.41973 𝑥

𝑦 = 0.15589𝑥 + 2.41973𝑥 2
Example # 3 (quadratic fit)

Solution:
Let the equation of the parabolic fit is 𝑦 = 𝑎 + 𝑏𝑥 + 𝑐𝑥 2
The corresponding normal equations are
σ 𝑦 = 𝑛𝑎 + 𝑏 σ 𝑥 + 𝑐 σ 𝑥 2
σ 𝑥𝑦 = 𝑎 σ 𝑥 + 𝑏 σ 𝑥 2 + 𝑐 σ 𝑥 3
σ 𝑥2𝑦 = 𝑎 σ 𝑥2 + 𝑏 σ 𝑥3 + 𝑐 σ 𝑥4
x y 𝑥2 𝑥3 𝑥4 xy 𝑥2y

1.0 0.98 1.00 1.0 1.0000 0.980 0.9800

1.2 1.40 1.44 1.728 2.0736 1.680 2.0160

1.4 1.86 1.96 2.744 3.8416 2.604 3.6456

1.6 2.55 2.56 4.096 6.5536 4.080 6.5280

1.8 2.28 3.24 5.832 10.4976 4.104 7.3872

2.0 3.20 4.00 8.000 16.0000 6.400 12.8000

12.27 14.20 23.4 39.9664 19.848 33.3568


෍𝑥 = 9
Put values in normal equation
𝑛=6
σ 𝑦 = 𝑛𝑎 + 𝑏 σ 𝑥 + 𝑐 σ 𝑥 2 ⟹ 12.27 = 60 + 9.0𝑏 + 14.20𝑐
σ 𝑥𝑦 = 𝑎 σ 𝑥 + 𝑏 σ 𝑥 2 + 𝑐 σ 𝑥 3 ⟹ 19.848 = 9𝑎 + 14.20𝑏 + 23.4𝑐
σ 𝑥 2 𝑦 = 𝑎 σ 𝑥 2 + 𝑏 σ 𝑥 3 + 𝑐 σ 𝑥 4 ⟹ 33.3568 = 14.20𝑎 + 23.4𝑏 + 39.9664𝑐
𝑎 = −0.1875, 𝑏 = 2.6239, 𝑐 = −1.4471
𝑦 = −0.1875 + 2.6239𝑥 − 1.4471𝑥 2

You might also like