Lecture Notes Interpolation and Data Fitting
Lecture Notes Interpolation and Data Fitting
Polynomial Interpolation Suppose that we have a set of n discrete data points (xi, yi = f(xi)) and it is required to find the value of y at any x between xi and xi+1. One way to achieve this is by constructing an (n-1)th order polynomial that goes through all these points and then evaluate the polynomial at the x we want. By definition, given any n point it is always possible to find a unique polynomial of the order (n-1) going through these points which is called INTERPOLATING POLYNOMIAL. The interpolating polynomial needs to go through all the data points and should exactly be the same as the tabulated points whereas the BEST FIT is a function which is predetermined function that can model a given data.
Figure shows the polynomial interpolation and data fit to a discrete set of data. The solid line shows the polynomial interpolation whereas the dashed line displays the fit given by the function shown on graph that is being used to model the data. It is to be remembered that the polynomial we will obtain is also an approximation to the true function f(x) which
1
produces the point (xi, yi). Whereas the fit is a requirement which is used when we do not have confidence in our collected data. Finding the Unique Polynomial Suppose there is an (n-1)th order polynomial Pn-1(xi) = yi for (i = 0 to n) that represents all the data points shown above is called interpolating polynomial. We need to develop the techniques that can be used to approximate these polynomials. The unique polynomial can be represented in many ways. Well look at the polynomial development methods which are easier for numerical computations. The basic technique is underlined below. Let us start by considering the simple polynomial P(x) to the function f(x) in the form of P(x) = a0 + a1x + .+ anxn The coefficient ais can be determined by equating P(xi) = f(xi). But due to loss of significant figure this method is not efficient. Example 1: Consider the function f(x) such that it is given that f(6000) = 1/3 and f(6001) = -2/3 (which are infinite digits floating point numbers). The polynomial interpolation function will be of order 1 given by P(x) = a0 + a1x Using five decimal place floating point arithmetic and solving the two equation 1/3 = a0 + a16000 -2/3 = a0 + a16001 we get
2
P(x) = 6000.3 x But now using this P(x) if we compute P(6000) we get a loss of four decimal point digits i.e P(6000) = 0.3 and P(6001) = -0.7 only.
Newtons Polynomial Approximation using Taylor Series We know that if f(x) is a function then its Taylor expansion around a point a is given by
Where (Rn
0 as n
infinity)
Since the series is in form of a polynomial, it can be used to approximate polynomials of a smooth functions by expanding the known points f(xi) around the point xi. This utilization of Taylors series to write a polynomial results in SHIFTED FORM,
P ( x ) = a 0 + a1 ( x x 0 ) + a 2 ( x x1 ) 2 + .... + a n ( x x n 1 ) n
The coefficients ao, a1 etc are actually the derivatives and the factorial quantities of the function f(x) at xi
f i (c ) ai = i!
According to NEWTONS FORM we do not write the P(x) in shifted form as given above but instead we use
P( x) = a 0 + a1 ( x x0 ) + a 2 ( x x0 )( x x1 ) + .... + a n ( x x0 )( x x1 )...( x x n 1 )
Which will become SHIFTED FORM if all the center points xi were to be the same. And it will be so called POWER FORM if all the xi = 0.
4
From
f0 = a o
f1 = a0 + ( x1 x0 ) f [ x0 x1 ]
which gives
f [ x0 x1 ] =
f1 f 0 x1 x0
(a)
f 2 = a0 + ( x2 x0 ) f [ x0 x1 ] + ( x2 x0 )( x2 x1 ) f [ x0 , x1 , x2 ]
and compute f[x0, x1, x2] like before. The second difference can also be taken and also the third one as shown in the table below 1st DD
x0 x1 x2 x3 x4 f0 f1 f2 f3 f4
2nd DD
f1 f 0 x1 x 0 f [ x0 , x1 , x 2 ] = f [ x1 , x 2 ] f [ x0 , x1 ] x 2 x0
f [ x0 , x1 ] =
f [ x1 , x 2 ] = f [ x 2 , x3 ] = f [ x3 , x 4 ] =
f1 x 2 x1
f2
f [ x1 , x 2 , x3 ] = f [ x 2 , x3 , x 4 ] =
f [ x 2 , x3 ] f [ x1 , x 2 ] x3 x1 f [ x3 , x 4 ] f [ x 2 , x 3 ] x4 x2
f3 f2 x3 x 2 f4 f3 x 4 x3
From
f [ x0 , x1 , x 2 ] = f [ x1 , x 2 ] f [ x 0 , x1 ] x1 x 0
f [ x0 , x1 ...x n ] =
f [ x1 , x 2 ....x n ] f [ x0 , x1 .....x n 1 ] x n x0
Example 2: Find the Newton divided difference method, the polynomial of the order three that passes through the points,
X 2.5 3.75 5.0 6.25 F(x) -28.62 -159.36 -513.97 -1265.45 1st DD 104.592 -283.688 -601.184 2nd DD -71.64 -126.998 3rd DD -14.76224
P ( x) = a 0 + ( x x 0 )[ f [ x 0 , x1 ] + ( x x1 ){ f [ x 0 , x1 , x 2 ] + ( x x 2 )[ f [ x 0 , x1 , x 2 , x 3 ]}]
P3(x) = -28.62+(x-2.5)[-104.592+(x-3.75){-71.64+ (x-5.0)[-14.762]}] Exercise 1: Find the sixth order polynomial approximation of log(x) between x = 1 and x = 8 using the Newton divided difference method. Choose different values of xi, i = 1 to 8 in the interval 1 < x <2 and compare the function value at x = 1.43.
Newton-Gregory Forward Polynomial Approximation We know that the Newtons polynomial in its non-nested form can be written as
Therefore
f [ x0 , x1 , x 2 ] = f [ x1 , x 2 ] f [ x 0 , x1 ] f 2 f 1 f 1 f 0 f 2 f1 f 0 = = 2 x 2 x0 2h 2 2h 2
f i + n nf i + n 1 +
n(n 1) f i + n 2 ..... f i = n f 2!
And
x x0 = S etc. 2
8
We get
P( x) = f ( x 0 ) + Sf + S ( S 1) 2 S ( S 1)....(S n 1) n f ... + f 2 n!
Which is the Newton-Gregory forward polynomial. Lagrange Interpolation Polynomial This is a simple reformulation of the Newton polynomial that avoids the computation of divided difference. It can be easily implemented in a computer program as compared to Newtons algorithm. To understand this interpolation consider we have three data point x0, x1 and x2 and correspondingly the function has values f0, f1 and f3. Then the Lagrange polynomial can be written as
f 2 ( x) = ( x x1 )( x x 2 )a 0 + ( x x0 )( x x 2 )a1 + ( x x0 )( x x1 )a 2
Putting x = x0 give
f 2 ( x0 ) = f 0 = ( x x1 )( x x 2 )a 0
a0 =
Similarly we can get
f0 ( x0 x1 )( x0 x 2 )
a1 =
f1 ( x1 x0 )( x1 x 2 )
And
a2 =
f2 ( x 2 x0 )( x 2 x1 )
f 2 ( x) =
( x x0 )( x x 2 ) ( x x0 )( x x1 ) ( x x1 )( x x 2 ) f0 + f1 + f2 ( x0 x1 )( x0 x 2 ) ( x1 x0 )( x1 x 2 ) ( x2 x0 )( x 2 x1 )
n
f n1 ( x) = Li ( x) f ( xi )
i =0
Where
Li ( x) =
j =0 j i
x xj xi x j
Note that Lagrange formulation is that each term Li(x) will be one at x = xi and zero at all the other sample points. Thus each product takes on the value f(xi) at the sample point. Exercise 1: Compare the results by plotting the error between actual and interpolated values of example 1 using Newtons, Newton-Gregory and Lagrange polynomials.
10
Data Fitting Given a discrete set of data points we can interpolate to find the function in between the points. This is what we do when we have complete confidence on the accuracy of given data. Many a time the data may not be accurate and may be noisy, in these cases it is beneficial to use a fit which is a smooth curve that runs through the data points. In such cases we are actually expecting our data to be like a certain function with adjustable parameters. Let us say we expect in an experiment that our measured quantity should behave like = at , where t might be temperature or time etc. In a typical experiment if we measure as a function of t we may get a fluctuating curve. Taking log on both sides we get
11
Chi Square Fitting Suppose we have n data points (xi, yi) and we wish to fit a real linear model function f(x)=a1x2 + a2x + a3 through these points. If y = f(x) has some adjustable parameters ais then we can ask ourselves the following questions What are the values of ais that best represent this data? Or how good is the choice of these values? And having selected some values of ais what is the probability that our function will give values close to the measured values? Or what is the difference of yi from y. Or how is y distributed in yi = y + y. If we assume that each data point yi has an independent random error that is Gaussian distribution P(yi) around the true value of f(x).
P( yi ) = y
Or
1 y yi 2
P( yi ) = y
1 f ( x ) yi 2
Then the standard deviation of this distribution is same at all points. Now the total probability is then simply proportional to the multiplication of all the probabilities from i = 1 to i = n given by
P( yi ) y
i =1
1 f ( x ) yi 2
12
Hence given a model f(x) and measured value yi, then f(x) is in the vicinity of yi with a difference + y. In order to maximize the probability P of getting the true value from our function using our choice of the parameters ais we need to minimize the term appearing as the power of exponential i.e.
1 f ( x ) yi 2
2
. We
1 n = 2 i =1
2
f ( xi ) y i i
Whose sum over all the standard deviations for our data points should be minimized to get maximum P. This method is therefore called chi square fit. Differentiating with respect to the parameters and equating the Chi square equal to zero gives us minimum chi value. For a good fit we must have
2 = nm
Where n is the number of data points and m is the number of parameters that we have chosen. In that case we have a common given by
1 n = 2 i =1
2
( f ( xi ) yi )2
nm
13
Linear Fit using Chi square fitting Consider we want to fit a model function of line to our data given by
f ( x) = ax + b
The Chi square now becomes
1 n = 2 i =1
2
yi axi b i
yi axi b xi = 0 2 i
And
n 2 = b i =1
yi axi b =0 2 i
These two equations will give n simultaneous linear equations which can be solved to find the best values of the parameters a and b. Note that the above equations can be written also as
S xy + aS xx + bS x = 0 S y + aS x + bS = 0
Where following parts can then be individually computed
14
S =
i =1
i
n
,
2
S xy =
i =1
xi y i
Sx =
i =1
xi
Sy =
i =1
yi
S xx =
i =1
xi
i2
and
S yy =
i =1
yi
i2
a=
SS xy S x S y SS xx S x
2
and
b=
S x S xy S xx S y S x SS xx
2
Which will give us the model function we have considered. Example: Consider that a data set of 10 points is xi yi 1 2 3 4 5 6 28.5 7 35.9 8 9 10 48
37.19 45
Consider that i is not given then we can assume that the sum of all the i is equal to 1. Then S = 1, Sx = 55, Sy = 271.245, Sxy = 1882.925, Sxx = 385 and Syy = 9232.4 Which gives a = 4.9377083 b = -0.00587 Giving f(x) = 4.9377083 x - 0.00587
15
The Graph is
Where dotted line is our best fit. We can even fit a general linear function to these data points which gives better results!!! We can also do data fitting for nonlinear functions!!!
16