Curve Fitting: Fitting A Straight Line
Curve Fitting: Fitting A Straight Line
Curve Fitting
In many applications, it often becomes necessary to establish a mathematical relationship
between experimental values. This relationship may be used for either testing existing
mathematical models or establishing new ones. The mathematical equation can also be used
to predict or forecast values of dependant variable. The process of establishing such
relationships in the form of a mathematical equation is known as curve fitting or regression
analysis.
Suppose the values of y for different values of x are given. If we want to know the effect of x
on y, then we may write a functional relationship y = f (x) where the variable y is called the
dependent variable and the variable x is called the independent variable. In curve fitting, the
general problem is to find a mathematical relationship of the form y = f (x), suggested by the
given values. The relationship may be either linear or non linear as shown in Fig-10.1 [page-
324, Balagurusamy].
It is a standard practice to prepare a scatter diagram as shown in Fig-10.2 [page-325,
Balagurusamy] and try to determine the functional relationship needed to fit the points. The
curve should best fit the plotted points. This means that the average error introduced by the
assumed curve should be minimum.
Fitting a Straight Line
Fitting a straight line is the simplest approach of regression analysis. Let us consider the
mathematical equation for a straight line
y = f (x) = a + bx ........... (1)
Usually fitting a straight line means finding the values of the parameter a and b of the above
equation, as well as actually constructing the line itself.
Consider a point (xi, yi) as shown in Fig-10.2. The vertical distance of this point from the line
f (x) = a + bx is the error qi. Then, qi = yi – f (xi) = yi - a – bxi
There are various approaches that could be tried to fitting a best line through the data. They
include:
a. Minimize the sum of errors i.e. minimize qi = (yi - a – bxi)
b. Minimize the sum of absolute values of errors i.e. minimize | qi | = |(yi - a – bxi)|
c. Minimize the sum of squares of errors i.e. minimize qi2= (yi - a – bxi)2
It can be easily verified that the first two strategies do not yield a unique line for a given set
of data. The third strategy overcomes this problem and guarantees a unique line. The
technique of minimizing the sum of squares of errors is known as least square method.
Least Squares Method
Let the sum of squares of individual errors be expressed as
n n n
Q= i 1
qi2 =
i 1
[ yi – f (xi)]2 = i 1
(yi - a – bxi)2
In the method of least squares, 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
Q Q
=0 and =0
a b
Q n
Then, = - 2 (yi - a – bxi) = 0
a i 1
Q n
= - 2 xi (yi - a – bxi) = 0
b i 1
Page 1 of 3
MSA / L-8 CSE-3512 - Numerical Methods 2nd February, 2005
In the method of least squares, we have to choose a0, a1 and a2 such that Q is minimum.
Equating, as before, the first partial derivatives to zero and simplifying, we get the following
normal equations
n a0 + a1 xi + a2 xi2 = yi
a0 xi + a1 xi2 + a2 xi3 = xi yi
a0 xi2 + a1 xi3 + a2 xi4 = xi2 yi
Fitting a Polynomial of nth Degree
Let the curve be represented by the equation
y = f (x) = a0 + a1x + a2x2 + …………. + anxn
The sum of squares of individual errors be expressed as
n n n
Q= i 1
qi2 =
i 1
[ yi – f (xi)]2 = i 1
(yi - a0 - a1xi - a2xi2 - …… - anxin)2
Page 2 of 3
MSA / L-8 CSE-3512 - Numerical Methods 2nd February, 2005
In the method of least squares, we have to choose a0, a1, …., an such that Q is minimum.
Equating, as before, the first partial derivatives to zero and simplifying, we get the following
normal equations
n a0 + a1 xi + a2 xi2 + ……… + an xin = yi
a0 xi + a1 xi + a2 xi3 + ……… + an xin+1 = xi yi
2
……………………………………………………………….
a0 xin + a1 xin+1 + a2 xin+2 + ……… + an xi2n = xin yi
Example 10.4: See Page335 [Balagurusamy]
Fitting a Power Function
Let y = axc be the function to be fitted to the given data. Taking logarithms of both sides, we
obtain the relation
log y = log a + c log x
which is the form of Y = A + B X where Y = log y, A = log a, B = c and X = log x. Hence,
we can evaluate the value of A and B by the normal equations of straight line. Then a and c
can be calculated from the formulae A = log a and B = c.
Example 10.2: See Page331 [Balagurusamy]
Fitting an Exponential Function
Let the curve y = a0 ea1 x be fitted to the given data. Then taking logarithms of both sides, we
get ln y = ln a0 + a1 x which can be written in the form Z = A + Bx where
Z = ln y, A = ln a0 and B = a1. The problem therefore reducesto finding a least-squares
straight line through the given data.
Example 4.4: See Page110 [S. S. Sastry]
Spline Interpolation
Self Study: See Page 299-314 [Balagurusamy].
Page 3 of 3