5 - Filled
5 - Filled
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.
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,
For linear fitting, to minimize the sum of the square of the errors equals to minimize
Setting derivatives = 0 :
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 deviation:
Coefficient of determination:
xi yi
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.
54
Solve above equations, we can get the coefficients of the 2nd-order polynomial function.
xi yi
0 2.1
1 7.7
2 13.6
3 27.2
4 40.9
5 61.1
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.
Multiple Linear Regress: is to find a linear function of multiple variables (x1,x2,…xn) that
will fit the sampled data.
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
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
f[xk,xk-1,…,x2,x1]=
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.
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.
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.
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