Numerical Methods
Lecture 07
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 1
Curve Fitting: Interpolation
❑ Curve Fitting
– Scientists and engineers are often faced with the task of estimating the value of
dependent variable y for an intermediate value of the independent variable x,
given a table of discrete data points (xi, yi), i = 0, 1, 2, . . . . . , n. This task can
be accomplished by constructing a function y(x) that will pass through the
given set of points and then evaluating y(x) for the specified value of x. The
process of constructing of y(x) to fit a table of data points is called curve
fitting.
– A table of data may belong to one of the two categories:
• Table of values of well-defined functions: Examples of such tables are logarithmic
tables, trigonometric tables, interest tables, etc.
• Data tabulated from measurements made during an experiment: Values of the
dependent variable are recorded at various values of the independent variable.
Example- the relationship between voltage applied and speed of a fan.
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 2
Curve Fitting: Interpolation
❑ Curve Fitting: Interpolation
– For the table of values of well-defined functions, the function is constructed
such that it passes through all the data points. This method of constructing a
function and estimating values at non-tabular points is called interpolation.
– The functions are known as interpolation polynomials.
– Different methods of interpolation:
• Lagrange interpolation
• Newton’s interpolation
• Newton-Gregory forward interpolation
• Spline interpolation
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 3
Curve Fitting: Interpolation
❑ Curve Fitting: Regression
– Data collected from measurements are not accurate and therefore, it will be
meaningless to try to pass the curve through every point. The best strategy
would be to construct a single curve that would represent the general trend of
the data without necessarily passing through the individual points. Such
functions are called approximating functions.
– One popular approach for finding an approximate function to fit a given set of
experimental data is called least-square regression.
– The approximating functions are known as least-square polynomials.
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 4
Curve Fitting: Interpolation
❑ Interpolation
Linear Interpolation
– Simplest form of interpolation.
– This considers only two data points: P1(x1, f(x1)) and P2(x2, f(x2)).
– These two points can be connected linearly as shown below.
f(x)
f(x2 ) p2
p
f(x)
p1
f(x1 )
x1 x x2 x
Graphical representation of linear interpolation
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 5
Curve Fitting: Interpolation
❑ Interpolation
Linear Interpolation
– Therefore,
– This is known as linear interpolation formula.
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 6
Curve Fitting: Interpolation
❑ Example: The table below gives square roots for integers:
x 1 2 3 4 5
f(x) 1 1.4142 1.7321 2 2.2361
Determine the square root of 2.5.
Solution:
The value of 2.5 lies between the points 2 and 3. Therefore,
x1 = 2, f(x1) = 1.4142
x2 = 3, f(x2) = 1.7321
Therefore,
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 7
Curve Fitting: Interpolation
❑ Example: The table below gives square roots for integers:
x 1 2 3 4 5
f(x) 1 1.4142 1.7321 2 2.2361
Determine the square root of 2.5.
Solution:
The correct answer is 1.5811. The difference is due to the use of a linear model.
If we consider the points 2 and 4, then
x1 = 2, f(x1) = 1.4142
x2 = 4, f(x2) = 2
Therefore,
Here the error has increased from 0.0079 to 0.0204. In general, the smaller the interval
between the interpolating data points, the better will be the approximation.
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 8
Curve Fitting: Interpolation
❑ Lagrange Interpolation Polynomial
Let x0, x1, . . . ., xn denote n distinct real numbers and let f 0, f 1, . . . ., f n be arbitrary
real numbers. The points (x0, f 0), (x1, f 1), (x2, f 2), . . . . . . ., (xn, f n) can be imagined
to be data values connected by a curve. Any function p(x) satisfying the conditions
p(xk) = f k for k = 0, 1, …., n
is called interpolation function. An interpolation function is, therefore, a curve that
passes through the data points as pointed out.
Let us consider a second order polynomial of the form
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 9
Curve Fitting: Interpolation
❑ Lagrange Interpolation Polynomial
If (x0, f 0), (x1, f 1), and (x2, f 2) are the three interpolating points, then we have
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 10
Curve Fitting: Interpolation
❑ Lagrange Interpolation Polynomial
Substituting b1, b2 and b3 in Eq. (1), we get
Eq. (2) may be represented as
Where
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 11
Curve Fitting: Interpolation
❑ Lagrange Interpolation Polynomial
In general, for n+1 points we have n-th degree polynomial as
--------------------------------- (3)
Where
Eq. (3) is called the Lagrange interpolation polynomial. The polynomials li(x)
are known as Lagrange basis polynomials.
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 12
Curve Fitting: Interpolation
❑ Example: Finding the Lagrange interpolation polynomial to fit the following data
i 0 1 2 3
0 1 2 3
0 1.7183 6.3891 19.0855
using the polynomial to estimate value of
Solution:
Lagrange basis polynomials are:
Therefore,
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 13
Curve Fitting: Interpolation
❑ Example: Finding the Lagrange interpolation polynomial to fit the following data
i 0 1 2 3
0 1 2 3
0 1.7183 6.3891 19.0855
using the polynomial to estimate value of
Solution:
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 14
Curve Fitting: Interpolation
❑ Example: Finding the Lagrange interpolation polynomial to fit the following data
i 0 1 2 3
0 1 2 3
0 1.7183 6.3891 19.0855
using the polynomial to estimate value of
Solution:
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 15
Curve Fitting: Interpolation
❑ Example: Finding the Lagrange interpolation polynomial to fit the following data
i 0 1 2 3
0 1 2 3
0 1.7183 6.3891 19.0855
using the polynomial to estimate value of
Solution:
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 16
Curve Fitting: Interpolation
❑ Example: Finding the Lagrange interpolation polynomial to fit the following data
i 0 1 2 3
0 1 2 3
0 1.7183 6.3891 19.0855
using the polynomial to estimate value of
Solution:
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 17
Curve Fitting: Interpolation
❑ Example: Finding the Lagrange interpolation polynomial to fit the following data
i 0 1 2 3
0 1 2 3
0 1.7183 6.3891 19.0855
using the polynomial to estimate value of
Solution:
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 18
Curve Fitting: Interpolation
❑ Example: Finding the Lagrange interpolation polynomial to fit the following data
i 0 1 2 3
0 1 2 3
0 1.7183 6.3891 19.0855
using the polynomial to estimate value of
Solution:
The interpolation polynomial is
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 19
Curve Fitting: Interpolation
❑ Example: Finding the Lagrange interpolation polynomial to fit the following data
i 0 1 2 3
0 1 2 3
0 1.7183 6.3891 19.0855
using the polynomial to estimate value of
Solution:
The interpolation polynomial is
That is, e1.5-1 = 3.3674
Therefore, e1.5 = 3.3674 + 1 = 4.3674
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 20
Curve Fitting: Interpolation
❑ Example: The table below gives square roots for integers:
x 1 2 3 4 5
f(x) 1 1.4142 1.7321 2 2.2361
Find the square root of 2.5 using the second order Lagrange interpolation
polynomial.
Solution:
Let us consider the following three points:
x0 = 2 x1= 3 x2 = 4
f0 =1.4142 f1 = 1.7321 f2 = 2
Second order Polynomial:
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 21
Curve Fitting: Interpolation
❑ Example: The table below gives square roots for integers:
x 1 2 3 4 5
f(x) 1 1.4142 1.7321 2 2.2361
Find the square root of 2.5 using the second order Lagrange interpolation
polynomial.
Solution:
For x = 2.5
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 22
Curve Fitting: Interpolation
❑ Example: The table below gives square roots for integers:
x 1 2 3 4 5
f(x) 1 1.4142 1.7321 2 2.2361
Find the square root of 2.5 using the second order Lagrange interpolation
polynomial.
Solution:
The correct answer is 1.5811. The error is 0.0017 which is much less than the error
obtained using a linear model.
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 23
Curve Fitting: Interpolation
❑ Lagrange Interpolation Polynomial: Limitations
– It requires 2(n+1) multiplications/divisions and 2n+1 additions and
subtractions.
– If we want to add one more data point, we have to compute the polynomial
from the beginning. It does not use the polynomial already computed.
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 24
Curve Fitting: Interpolation
❑ NEWTON INTERPOLATION POLYNOMIAL
Let us consider another form of polynomial known as Newton form. The Newton
form of polynomial is
pn(x) = a0 + a1(x – x0) + a2(x – x0)(x - x1) + ……. + an(x – x0)(x - x1) …..
(x - xn-1) …… (1)
where the interpolation points x0, x1, …… xn-1 act as centres.
To construct the interpolation polynomial, we need to determine the coefficients a0,
a1, …… an. Let us assume that (x0, f 0), (x1, f 1), ……… (xn-1, f n-1) are the
interpolating points. That is,
pn(xk ) = f k k = 0, 1, 2, …….., n-1
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 25
Curve Fitting: Interpolation
❑ NEWTON INTERPOLATION POLYNOMIAL
Now, at x = x0, we have
pn(x0) = a0 = f 0
Similarly, at x = x1
pn(x1) = a0 + a1(x1 – x0) = f 1
Substituting for a0, we get
Similarly, at x = x2,
pn(x2) = a0 + a1(x2 – x0) + a2(x2 – x0)(x2 - x1) = f 2
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 26
Curve Fitting: Interpolation
❑ NEWTON INTERPOLATION POLYNOMIAL
Substituting for a0 and a1, and rearranging the terms, we get,
Let us define a notation,
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 27
Curve Fitting: Interpolation
❑ NEWTON INTERPOLATION POLYNOMIAL
Thus,
Here a1 represents the first divided difference and a2 the second divided difference
and so on.
Substituting for ai coefficients in equation (1), we get
pn(x) = f[x0]+ f[x0, x1] (x – x0) + f[x0, x1, x2] (x – x0)(x - x1)
+ …….
+ f[x0, x1, ….., xn] (x – x0)(x - x1) ….. (x - xn-1)
This can be written more compactly as,
----------------- (2)
Equation (2) is called Newton’s divided difference interpolation polynomial.
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 28
Curve Fitting: Interpolation
❑ Example: Given below is a table of data for log x. Estimate log 2.5 using second
order Newton interpolation polynomial.
i 0 1 2 3
xi 1 2 3 4
log x i 0 0.3010 0.4771 0.6021
Solution:
Second order polynomials require only three data points. Here we use the first the
points.
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 29
Curve Fitting: Interpolation
❑ Example: Given below is a table of data for log x. Estimate log 2.5 using second
order Newton interpolation polynomial.
i 0 1 2 3
xi 1 2 3 4
log x i 0 0.3010 0.4771 0.6021
Solution:
Therefore,
Second order Newton interpolation polynomial:
p2(x) = a0 + a1(x – x0) + a2(x – x0)(x - x1)
= 0 + 0.3010 (x - 1) + (-0.06245)(x - 1)(x - 2)
Estimated Value of log 2.5:
p2(2.5) = 0.3010 (1.5) - 0.06245(1.5)(0.5)
= 0.4515 – 0.0468
= 0.4047
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 30
Curve Fitting: Interpolation
❑ Example: Given below is a table of data for log x. Estimate log 2.5 using second
order Newton interpolation polynomial.
3/23/2020 Md. Golam Moazzam, Dept. of CSE, JU 31