Interpolation and fitting

Source: Internet
Author: User
definition

In practical problems, a function y=f (x) y=f (x) is generally obtained by actual observation, and a finite number of points are known.
Yi=f (xi), i=0,1,..., n yi = f (xi), i = 0, 1, ..., n

When it is necessary to know the function value of point x between x0,x1,..., xn x0,x1,..., xn, then interpolation is required, and some simpler, more simple, function g (x) g (x) to satisfy the condition is used instead of f (x) f (x), which is the interpolation method . To pass a known data point.

Fitting is also known as a finite number of points, the approximate function , does not require the known quantity stronghold, only requires in some sense it at the point of the total deviation of the least , that is, the best fit data . piecewise linear interpolation

Connect each adjacent point with a straight line. The resulting polyline is piecewise linear interpolation. spline interpolation

Makes the interpolation function more smooth . A piecewise polynomial with a certain smoothness is called a spline function. MATLAB implementation interpolation Lagrange Interpolation method:

The values of n nodes are set to the array x0, y0 input (note the Matlat array subscript starting from 1), m interpolation points with array x input, output array y is M interpolation
function Y=lagrange (x0,y0,x);
N=length (x0); m=length (x);
For i=1:m
    z=x (i);
    s=0.0;
    For K=1:n
        p=1.0;
        For j=1:n
            if J~=k
                p=p* (z-x0 (j))/(x0 (k)-x0 (j));
            End
        End
        S=p*y0 (k) +s;
    End
    y (i) =s;
End
Hermitian (Hermite) interpolation
The data of the N nodes is set to the array x0 (the horizontal axis of the known point), the y0 (function value), the Y1 (value of the values) input (note the Matlat array subscript starting from 1), m interpolation points with the array x input, the output array y is M interpolation.
function Y=hermite (x0,y0,y1,x);
N=length (x0); m=length (x);
    For k=1:m
        yy=0.0;
        For I=1:n
            h=1.0;
            a=0.0;
            For j=1:n
                if J~=i
                    h=h* ((x (k)-x0 (J))/(x0 (i)-x0 (j))) ^2;
                    a=1/(x0 (i)-x0 (j)) +a;
                End
            End
        yy=yy+h* ((x0 (i)-X (k)) * (2*a*y0 (i)-y1 (i)) +y0 (i));
        End
    y (k) =yy;
End
one-dimensional interpolation functions

Syntax:y = interp1 (x0, y0, X, ' method ')
METHOD specifies how the interpolation is interpolated by default to linear interpolation. There are several interpolation methods:

' Nearest ' recent interpolation
' Linear ' linear interpolation
' Spline ' cubic spline interpolation
' Cubic ' cubic interpolation
All interpolation methods require x0 x0 to be monotonous .
When the x0 x0 bit equidistant, the fast interpolation method can be used, and the format of the fast interpolation method is ' *nearest ', ' *linear ', ' *spline ', ' *cubic '. three secondary spline interpolation

one-dimensional interpolation functions:
The spline function is used to interpolate, that is, to take the interpolation function as a spline function, called spline interpolation.
y = interp1 (x0, y0, X, ' spline ');
y = spline (x0, y0, x);
pp = Csape (x0, y0, conds);
pp = Csape (x0, y0, Conds, valconds); y = ppval (pp, x);
where X0,y0 x0, y0 is the known quantity stronghold, X is the interpolation point, Y is the value of the interpolation point function.

Example: 12 hours, hourly measurement of outdoor temperature, and spline interpolation

Comparison of several one-dimensional interpolation methods:

clc,clear x0=[0 3 5 7 9 []; y0=[0 1.2 1.7 2.0 2.1 2.0 1.8 1.2 1.0 1.6]; x=0:0.1:15; Y1=lagrange (X0,Y0,X);
% calls the Lagrange interpolation function written earlier Y2=INTERP1 (x0,y0,x);
Y3=INTERP1 (x0,y0,x, ' spline '); Pp1=csape (X0,Y0);
Y4=ppval (PP1,X); Pp2=csape (X0,y0, ' second ');
Y5=ppval (PP2,X);
fprintf (' Compare the results of different interpolation methods and boundary conditions: \ n ') fprintf (' x y1 y2 y3 y4 y5\n ') xianshi=[x ', y1 ', y2 ', Y3 ', Y4 ', Y5 ']; fprintf ('%f\t%f\t%f\t%f\t%f\t%f\n ', Xianshi ') subplot (2,2,1), Plot (x0,y0, ' + ', x,y1), title (' Lagrange ') subplot (2,2,2 ), Plot (x0,y0, ' + ', X,y2), title (' piecewise linear ') subplot (2,2,3), Plot (x0,y0, ' + ', X,y3), title (' Spline1 ') subplot (
2,2,4), Plot (x0,y0, ' + ', X,y4), title (' Spline2 ') dyx0=ppval (Fnder (PP1), x0 (1))% seek the derivative x=0 at Ytemp=y3 (131:151);
Index=find (Ytemp==min (ytemp)); Xymin=[x (130+index), Ytemp (index)] 

Effect:

two-dimensional interpolation functions:
z = interp2 (x0, y0, z0, x, Y, ' method ')
Where the x0,y0 are to think of M and n-dimensional vectors, representing nodes, Z0 is a n*m-dimensional matrix, representing the node value, x, Y is a one-dimensional array, representing the interpolation point, and y should be a vector of different directions, that is, a row vector, a column vector, Z is a matrix.
If you use three-time spline interpolation, you can use the command
pp = Csape ({x0, y0}, Z0, Conds, valconds); z = Fnval (pp, {x, y});

Example:
matlab for fitting Linear least squares

Problem: A set of data, known as N points on a plane, i=1,2,..., n,xi, seeks a function y = f (x) so that f (x) is closest to all data points under a certain criterion, that is, the curve is best fitted.
Make f (x) =a1r1 (x) +a2r2 (x) +...+AMRM (x) f (x) = A1R1 (x) + A2R2 (x) + ... + AMRM (x), where RK (x) RK (x) is a previously selected set of linearly unrelated functions, AK AK is a pending factor.
The fitting criterion is to make Yi Yi, i=1,2,..., n i = 1, 2,..., N, and the distance from F (xi) F (xi)

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.