Curve Fitting and interpolation in MATLAB

Source: Internet
Author: User

Curve Fitting and Interpolation
In a large number of application fields, people often face the task of using a parsing function to describe data (usually measured values. There are two ways to solve this problem. In the interpolation method, the data assumption is correct, and some method is required to describe the situation between data points. This method is discussed in the next section. The method discussed here is curve fitting or regression. People try to figure out a smooth curve, which best fits the data but does not need to go through any data point. Figure 11.1 illustrates the two methods. The line labeled 'O' is the data point. The solid line of the connection point depicts linear interpolation, And the dotted line is the best fit for data.
11.1 Curve Fitting
Curve Fitting involves answering two basic questions: what does best fit mean? What kind of curve should I use? There are many different ways to define the best fit and there is an infinite number of curves. So from here on, where are we going? As it confirms, when the best fit is interpreted as the minimum sum of squared errors of data points and the curve used is limited to a polynomial, the curve fit is quite simple. In mathematics, it is called the least square Curve Fitting of polynomials. If this description obfuscated you, study figure 11.1. The vertical distance between the dotted line and the data point of the flag is the error at this point. Calculate the square distance of each data point and add the square distance to the sum, that is, the sum of squares of errors. This dotted line is the curve that minimizes the sum of squares of errors, that is, the best fit. The term "Least Square" is omitted to minimize the sum of squares of errors.

In Matlab, the function polyfit solves the problem of least square curve fitting. To illustrate the usage of this function, let's start with the data in figure 11.1 above.
X = [0.1.2.3.4.5.6.7.8.91];
Y = [-. 4471.9783.286.167.087.347.669.569.489.3011.2];
To use polyfit, we must give the function the order or degree of the above data and the polynomial that we want to best fit the data. If n = 1 is selected as the order, the simplest linear approximation is obtained. It is usually called linear regression. On the contrary, if we select n = 2 as the order, we can obtain a 2-order polynomial. Now we select a polynomial of level 2.
N = 2; % polynomial order
P = polyfit (X, Y, n)
P =
-9.8000000001293-0.0317
The output of polyfit is a row vector with polynomial coefficients. The solution is y =-9.8108X2 + 20171293x-0.0317. In order to compare the curve fitting solution with the data point, let's plot both.
Xi = linspace (0, 1,100); % x-axis data for plotid
Z = polyval (p, xi );
To calculate the polynomial value of the XI data point, call the polyval function of Matlab.
Plot (X, Y, 'O', X, Y, XI, Z ,':')
Plot the raw data X and Y, use 'O' to mark the data point, re-draw the original data between data points, and use the dot ':' line, draw the polynomial data XI and Z.
Xlabel ('x'), ylabel ('y = f (x) '), title ('second Order Curve Fitting ')
Mark the image. The results of these steps are shown in figure 11.1 above.
The selection of Polynomial order is somewhat arbitrary. Two points determine linear or first-order polynomials. Three points determine a square or 2-order polynomial. According to this, the n + 1 data point uniquely determines the n-order polynomial. Therefore, in the above case, there are 11 data points. We can choose a polynomial of up to 10th order. However, high-order polynomials provide poor numerical characteristics, and we should not choose polynomials that are higher than the desired order. In addition, as the polynomial order increases, the approximation is not smooth enough, because higher subpolynomials are before zero, and can be obtained multiple times. For example, select a 10th polynomial.
Pp = polyfit (X, Y, 10 );
Format short E % change display format
Pp. '% display polynomial coefficients as a column
Ans =
-4.6436e + 005
2.2965e + 006
-4.8773e + 006
5.8233e + 006
-4.2948e + 006
2.0211e + 006
-6.0322e + 005
1.0896e + 005
-1.0626e + 004
4.3599e + 002
-4.4700e-001
Note that in the current situation, the scale of the polynomial coefficient is compared with the first level 2 fitting. Note that the magnitude difference between the minimum (-4.4700e-001) and the maximum (5.8233e + 006) coefficient is 7 orders of magnitude. What is the result of plotting this solution and comparing it with the original data and the fitting of the Second-Order Curve?
ZZ = polyval (PP, xi); % evaluate 10th order polynomial
Plot (X, Y, 'O', XI, Z, ':', XI, ZZ) % plot data
Xlabel ('x'), ylabel ('y = f (x) '), title ('2nd and 10th Order Curve Fitting ')
In Figure 11.2 below, the raw data is labeled as 'O', the level 2 curve is a dotted line, and the Level 10 curve is a solid line. Note that a large ripple appears between data points at the extreme values on the left and right in the 10th order fitting. This pattern wave often occurs when a higher-order curve is attempted. As shown in figure 11.2, the 'more, the better 'concept is not applicable here.

11.2 one-dimensional Interpolation
As described in the previous section on curve fitting, interpolation is defined as a method for evaluating functions between data points, which are given by certain sets. Interpolation is a valuable tool when people cannot quickly find the function value of the desired Intermediate Point. For example, this is the case when the data point is the result of some experimental measurements or a long calculation process.
Perhaps the simplest example of interpolation is matlab plot. By default, Matlab uses the data points used for linear connection for plotting. This linear interpolation predicts that the median falls on a straight line between data points. Of course, linear interpolation is more accurate when the number of data points increases and the distance between them decreases by hours. For example,
X1 = linspace (0, 2 * Pi, 60 );
X2 = linspace (0, 2 * Pi, 6 );
Plot (x1, sin (X1), X2, sin (X2 ),'-')
Xlabel ('x'), ylabel ('sin (x) '), title ('linear interpolation ')

Like curve fitting, interpolation is required for decision making. Based on the assumptions, there are multiple kinds of interpolation. In addition, interpolation can be performed in a space larger than one dimension. That is, if there is an interpolation that reflects two variable functions, Z = f (x, y), then we can find the median value of Z between x and y for interpolation. MATLAB provides many interpolation options in one-dimensional functions interp1 and two-dimensional functions interp2. Each function is described below.
In order to describe one-dimensional interpolation, the following problems are taken into consideration. The outdoor temperature is measured once an hour within 12 hours. Data is stored in two MATLAB variables.
Hours = :12; % index for hour data was recorded
Temps = [1, 89152529313022252724]; % recorded temperatures
Plot (hours, temps, hours, temps, '+') % view temperatures
Title ('temperature ')
Xlabel ('hour'), ylabel ('degrees celsius ')

 

MATLAB draws a linear interpolation line for data points. To calculate the temperature at any given time, people can try to explain the visible graph. Another method is to use the interp1 function.
T = interp1 (hours, temps, 9.3) % estimate temperature at hour = 9.3
T =
22.9000
T = interp1 (hours, temps, 4.7) % estimate temperature at hour = 4.7
T =
22
T = interp1 (hours, temps, [3.26.57.111.7]) % find temp at invalid points!
T =
10.2000
30.0000
30.9000
24.9000
The default use of interp1 is described by interp1 (X, Y, XO). Here, X is an independent variable (abscissa), and Y is an expected variable (ordinate ), XO is an array of numeric values for interpolation. In addition, this default assumption is linear interpolation.
If linear connection data points are not used, we can use some smoother curves to fit data points. The most common method is to use a cubic polynomial, that is, a cubic polynomial, to model each segment between successive data points, the first two derivatives of each cubic polynomial are consistent with the data point. This type of interpolation is called a cubic spline or a spline for short. The interp1 function can also perform three spline interpolation.
T = interp1 (hours, temps, 9.3, 'spline') % estimate temperature at hour = 9.3
T =
21.8577
T = interp1 (hours, temps, 4.7, 'spline') % estimate temperature at hour = 4.7
T =
22.3143
T = interp1 (hours, temps, [3.26.57.111.7], 'spline ')
T =
9.6734
30.0427
31.1755
25.3820
Note that the result obtained by spline interpolation is different from the linear interpolation result shown above. Because interpolation is an estimation or prediction process, it means that different estimation rules are applied to lead to different results.
One of the most common splines is data smoothing. That is, given a set of data, use spline interpolation to evaluate values at a finer interval. For example,
H = 1:0.; % estimate temperature every 1/10 hour
T = interp1 (hours, temps, H, 'spline ');
Plot (hours, temps, '-', hours, temps, '+', h, t) % plot comparative results
Title ('springfield temperature ')
Xlabel ('hour'), ylabel ('degrees celsius ')
In Figure 11.5, the dotted line is a linear interpolation, the solid line is a Smooth Spline interpolation, and the marked with '+' is the original data. If a finer resolution is required on the timeline and splines are used for interpolation, we have a smoother but not necessarily more accurate temperature estimate. In particular, the slope of the spline solution does not change suddenly at the data point. As a return of this smooth interpolation, cubic spline interpolation requires a greater amount of computation, because three polynomials must be found to describe the features between given data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.