0% found this document useful (0 votes)
34 views14 pages

5 - Filled

Uploaded by

jamesg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views14 pages

5 - Filled

Uploaded by

jamesg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter 5:

Curve Fitting and Interpolation


5.1 Basic Concepts

Consider a set of (x, y) data pairs (points) collected during an experiment,

Curve fitting: is a procedure to develop or evaluate mathematical formulas (functions or


equations) that best fit the relationship between x and y. The fitted function & data may not
exactly agree but fit well overall.

Interpolation: is a procedure to use a mathematical formula to represent a set of measured


data points, such that the formula gives exact value at all given points and estimate a value
between known values of data points, i.e., a technique to estimate y at some x where there’s
no measured data. Thus, interpolated function passes exactly through known data.

5.2 Curve Fitting

Suppose you sampled a set of (x, y) data pairs, curve fitting is to find a BEST function (curve)
which minimizes the sum of error between the sampled and the predicated by the function
(curve) for all data.

Curve fitting includes:


(1). Linear regress
(2). Polynomial regress
(3). Multiple linear regress

5.2.1 Linear Regress

Linear Regress: is to find a line that will best fit the sampled data.

51
Because the sampled data points may not all fall on the line found, the fitted is not an exact
solution but an approximated solution. There are errors between the fitted model and the
observation. The BEST line is the one which minimizes the sum of error for all data.
The error may be defined as:

But, the best line is found when the sum of squared error is minimized,

the curve found by this way is called least-square fit.

For linear fitting, to minimize the sum of the square of the errors equals to minimize

Differentiate with respect to each coefficient:

Setting derivatives = 0 :

From Σa0 = n a0, express equations as set of 2 unknowns (a0, a1)

Solve equations simultaneously:

52
Example 5.1 Fit a straight line to x and y values listed below

xi yi xi2 xi*yi
1 0.5
2 2.5
3 2.0
4 4.0
5 3.5
6 6.0
7 5.5

Error analysis:
Sum of the square of the errors:

Standard errors of the estimation:

Sum of the square around the mean:

Standard deviation:

Coefficient of determination:

It is easily found that for perfect fit E = 0 and r =


Example 5.1cont. Analyze the error of the linear fit found above.

xi yi

1 0.5 8.5765 0.1687


2 2.5 0.8622 0.5626
3 2.0 2.0408 0.3473
4 4.0 0.3265 0.3265
5 3.5 0.0051 0.5896
6 6.0 6.6122 0.7972
7 5.5 4.2908 0.1993

53
Linear model explains 86.8% of original uncertainty.
Linear regress is only good for those problems for which relationship between the quantities
is linear. However, for most of the problems in science and engineering, the relationship
between the quantities being considered is not linear. Some nonlinear problems can be
converted into linear form, e.g.
Power function:

Exponential function:

Reciprocal function:

then linear regress still can be used. For other nonlinear problems, polynomial regress should
be used.

5.2.2 Polynomial Regress

Polynomial Regress: is a procedure to determine the coefficients of a polynomial of a second


or higher order such that the polynomial curve will best fit the sampled data.

e.g. 2nd-order polynomial:

Sum of the squares of the errors:

Take derivative with respect to each coefficient (unknown):

Then, we can get 3 linear equations:

54
Solve above equations, we can get the coefficients of the 2nd-order polynomial function.

For mth-order polynomial: y = a0 + a1x + a2 x2+ . . . + amxm


We have to solve m+1 simultaneous linear equations.

Example 5.2 Fit a second-order polynomial to the data listed below.

xi yi
0 2.1
1 7.7
2 13.6
3 27.2
4 40.9
5 61.1

5.2.3 MATLAB Function for Polynomial Regress

The most widely used MATLAB functions for polynomial regress is:
polyfit()

C = polyfit(X, Y, n)
where
X = vector of independent variable values of the data points
Y = vector of dependent variable values of the data points
n = degree of polynomial
C = coeff. of polynomial in descending power
y = c1*xn + c2*xn-1 + ... + cn*x + cn+1
For example: y = 1.86071x2+ 2.35929x + 2.47857
C = [1.86071 2.35929 2.47857]

55
You can evaluate polynomial at the points defined by the input vector
>> y = polyval(C, X)
where X = Input vector
y = Value of polynomial evaluated at x

Example 5.3 Fit a second-order polynomial to the data in Example 5.2 and calculate the
coefficient of determination by MATLAB.

5.2.4 Multiple Linear Regress

Multiple Linear Regress: is to find a linear function of multiple variables (x1,x2,…xn) that
will fit the sampled data.

y = c0 + c1x1 + c2x2 + . . .+ cpxp

For example: two independent variables y = c0 + c1x1 + c2x2

Sum of squares of the error: E =

Differentiate with respect to unknowns:

56
Setting partial derivatives = 0 and expressing result in matrix form:

Example 5.4 Fit a linear polynomial with two variables to the data listed.

x1 x2 y
0 0 5
2 1 10
2.5 2 9
1 3 0
4 6 3
7 2 27

5.3 Interpolation

Interpolation estimates the data point value between the known points.
Extrapolation predicts the data point value outside the range over which the given data points
are measured.

57
For any number of points n there is a polynomial of order n-1 that passes through all of the
points. The interpolation methods can be divided into the following types:
(1). Lagrange Interpolation
(2). Newton Interpolation
(3). Piecewise (spline) Interpolation

5.3.1 Lagrange Polynomial Interpolation

Lagrange polynomial interpolation: uses a polynomial of n-1 order consisting of Lagrange


functions to fit n given data points, and apply this function to determine the y value of any
point with provided x value. The polynomial is

Where are called Lagrange functions.

For two points, (x1,y1) and (x2,y2), the 1st-order Lagrange polynomial that passes through the
points is

For three points, (x1,y1), (x2,y2), and (x3,y3),the 2nd-order Lagrange polynomial that passes
through the points is

Example 5.5 For the data listed below, 1) determine the 4th-order Lagrange polynomial that
passes through the points, 2) use the polynomial obtained in 1) to estimate the value at x=3.

xi yi
1 52
2 5
4 -5
5 -40
7 10

58
5.3.2 Newton Polynomial Interpolation

Newton polynomial interpolation: uses a polynomial of n-1 order to fit n given data points,
and apply this function to determine the y value of any point with provided x value. The
polynomial is in the form of

Where the coefficients ak are calculated by k-th divided difference f[xk,xk-1,…,x2,x1]

f[xk,xk-1,…,x2,x1]=

The divided differences between two points (xi,yi),(xj,yj) are

The divided differences for 5 data points can be calculated by the table below,

59
Example 5.6 Repeat Example 5.5 using Newton polynomial interpolation.

Note: Both Lagrange and Newton polynomial interpolation methods are using a single
polynomial passing through all given points and estimating other points between. These
methods works well when the number of given points is small. When the number of given
points is large, the order of polynomial is high, and the error will be large.

5.3.3 Piecewise Polynomial Interpolation

Piecewise Polynomial Interpolation: When a large number of points is involved, a better


interpolation can be obtained by using many low-order polynomials instead of a single high-
order polynomial. Normally, all the polynomials are in same order but with different
coefficients, and each polynomial is only valid in one interval between two or several given
points. Interpolation in this way is called piecewise or spline interpolation.

Typical spline interpolation includes linear (1st-order polynomial), quadratic (2nd-order) and
cubic (3rd-order).

Linear spline, for n given points, there are n-1 intervals. For each interval, using Lagrange
form, the straight line that connects the two points xi and xi+1 in the interval i is

60
Example 5.7 For the data listed below, 1) determine all the linear splines that fit the points,
2) Estimate the interpolated value at x=12.7.

xi yi
8 5
11 9
15 10
18 8

Quadratic spline, for n given points, there are n-1 intervals. For each interval, using a 2nd-
order polynomial to interpolate, the 2nd-order (quadratic) polynomial that connects the two
points xi and xi+1 in the interval i is

Overall, there are 3(n-1) polynomial coefficients to be determined, need 3(n-1) equations:
1). Each polynomial passes through the two endpoints of its corresponding interval

2). Except at two outside endpoints, the slopes at both sides of each point are equal.

3). The 2nd-order derivative at the 1st point is 0.

61
Example 5.8 For the data listed below, 1) determine all the quadratic splines that fit the
points, 2) Estimate the interpolated value at x=12.7.

xi yi
8 5
11 9
15 10
18 8
22 7

62
5.4 MATLAB Functions for Curve Fitting and Interpolation

The MATLAB built-in function for polynomial curve fitting is C=polyfit(x,y,m) which has
been explained in Section 5.2.2.

The MATLAB function for interpolation is yi=interpl(X, Y, xi, 'method') in which


X: vector of independent variable values of the data points
Y: vector of dependent variable values of the data points
xi: is the value of x at which y is to be interpolated
method: can be 1) nearest---returns the value of the data point that is nearest to the
interpolated point; 2) linear---uses linear spline interpolation; 3) spline---uses cubic spline
interpolation

Example 5.9 Use MATLAB built-in function with linear spline to repeat Example 7.7, and
sketch the interpolated curve.

63
10

9.5

8.5

7.5

6.5

5.5

5
8 10 12 14 16 18 20 22

64

You might also like