Three-dimensional data interpolation fitting and cubic spline fitting algorithm based on MATLAB (with complete code)

Table of contents

1. Three-dimensional interpolation

Example 1

2. High-dimensional interpolation fitting

format one

format two

format three

format four

Format five

Example 2

3. Univariate cubic spline interpolation

Example 3

Example 4

4. Multivariate cubic spline interpolation

Example 6


1. Three-dimensional interpolation

First, the 3D grid is generated using the meshgrid() function, and the call format in MATLAB is as follows:

[x,y,z]=meshgrid(x1,y1,z1)
% x1,y1,z1为这三维数据所需要的分割形式,均以向量形式给出
%返回的x,y,z为网格的数据生成,也是三维数组

The three-dimensional interpolation operation mainly uses the griddata() function and the interp() function, as follows:

griddata3()  %三维非网格形式的插值拟合
griddatan()  %n维非网格形式的插值拟合
interpn()    %N维网格数据的插值拟合

In fact, the interp3() and interpn() call formats are consistent with the interp2() function;

The calling format of griddata3() and griddatan() is consistent with the griddata() function.

The previous blogs about interp2() and griddata() functions can be seen:

Two-dimensional and three-dimensional interpolation and fitting operation based on MATLAB (with complete code) Blog-CSDN Blog

Data interpolation operation based on MATLAB: Lagrange and Hermite algorithm (with complete code) Blog-CSDN Blog

Example 1

Use the function V(x,y,z) to generate some grid-type sample points, try to fit them according to the sample points, and give the fitting error.

V(x,y,z)=e^{x^2z+y^2x+z^2y}cos(x^2yz+z^2yx)

untie:

The MATLAB code is as follows:

clc;clear;

[x,y,z]=meshgrid(-1:0.2:1);
[x0,y0,z0]=meshgrid(-1:0.05:1);
V=exp(x.^2.*z+y.^2.*x+z.^2.*y).*cos(x.^2.*y.*z+z.^2.*y.*x);
V0=exp(x0.^2.*z0+y0.^2.*x0+z0.^2.*y0).*cos(x0.^2.*y0.*z0+z0.^2.*y0.*x0);

V1=interp3(x,y,z,V,x0,y0,z0,'spline');
err=V1-V0;
max(err(:))

operation result:

ans =0.041862381154469

2. High-dimensional interpolation fitting

The interpn() function can realize the interpolation of one-dimensional, two-dimensional, three-dimensional and N-dimensional grid data. There are five commonly used formats:

format one

Vq=interpn(X1,X2,...,Xn,V,Xq1,Xq2,...,Xqn)

format two

Vq=interpn(V,Xq1,Xq2,...,Xqn)

format three

Vq=interpn(V)

The interval between sample values ​​​​is divided once

format four

Vq=interpn(V,k)

Repeatedly divide the interval between sample values ​​in each dimension k times to form optimized grids, and return interpolated values ​​on these grids. 2^k-1This will generate insertion points between sample values

Format five

Vq=interpn(_,method,extrapval)

Alternative interpolation methods can be specified, including: 'linear', 'nearest', 'pchip', 'cubic', 'makima' or 'spline'. The default method is 'linear'.

Example 2

Select the data by yourself, and use the interpn() function to perform one-dimensional interpolation, two-dimensional interpolation and three-dimensional difference.

untie:

(1) One-dimensional interpolation

The MATLAB code is as follows:

clc;clear;
x=[1 2 3 4 5];
v=[12 16 31 10 6];
xq=(1:0.1:5);
vq=interpn(x,v,xq,'cubic');
plot(x,v,'o',xq,vq,'-');
legend('Samples','Cubic Interpolation');

operation result:

(2) Two-dimensional interpolation

The MATLAB code is as follows:

clc;clear;
[X1,X2]=ndgrid(-5:1:5);
R=sqrt(X1.^2+X2.^2)+eps;
V=sin(R)./(R);
Vq=interpn(V,'cubic');
mesh(Vq);
size(V);
size(Vq);

 operation result:

(3) Three-dimensional interpolation

This example will give the functionz=f(x,y)=(x^2-2x)e^{-x^2-y^2-xy}

The MATLAB code is as follows:

clc;clear;
[x,y]=ndgrid(-3:.6:3,-2:.4:2);
z=(x.^2-2*x).*exp(-x.^2-y.^2-x.*y);
[x1,y1]=ndgrid(-3:.2:3,-2:.2:2);
z1=interpn(x,y,z,x1,y1);
surf(x1,y1,z1),axis([-3,3,-2,2,-0.7,1.5])

 operation result:

3. Univariate cubic spline interpolation

Given a sample point, n points on the plane (x_i,y_i)(i=1,2,\cdots,n), and satisfy x_1<x_2<\cdots<x_n.

S(x) is a cubic spline function that needs to meet the following three conditions:

  1. S(x_i)=y_i(i=1,2,\cdots,n), that is, the function passes through the sample point
  2. S(x) is a cubic polynomial on each subinterval [x_i,x_{i+1}], namelyS(x)=c_{i1}(x-x_i)^3+c_{i2}(x-x_i)^2+c_{i3}(x-x_i)+c_{i4}
  3. S(x) [x_1,x_n]has continuous first and second derivatives over the entire interval

In MATLAB, define a cubic spline function class as follows:

S=csapi(x,y)

 The above formula x=[x_1,x_2,\cdots,x_n],y=[y_1,y_2,\cdots,y_n]is the sample point. S returns the interpolation result of the spline function object, which includes subinterval points, cubic polynomial coefficients of each interval point, and so on.

You can use fnplt() to draw the interpolation result, the calling format is as follows:

fnplt(S)

For a given vector xp, the fnval() function can be used for calculation, and the calling format is as follows:

yp=fnval(S,xp)

The yp obtained from this formula is the interpolation result of each point on xp.

Example 3

Take the data points generated by sin(x) as an example to perform cubic spline interpolation.

untie:

The MATLAB code is as follows:

clc;clear;
x0=[0,0.4,1,2,pi];
y0=sin(x0);
sp=csapi(x0,y0),fnplt(sp,':');
hold on,ezplot('sin(t)',[0,pi]);
plot(x0,y0,'o')
sp.coefs

operation result:

sp = 

  A struct containing the following fields:

      form: 'pp'
    breaks: [0 0.400000000000000 1 2 3.141592653589793]
     coefs: [4×4 double]
    pieces: 4
     order: 4
       dim: 1


ans =

  -0.162650313526554   0.007585653997624   0.996535644336825                   0
  -0.162650313526551  -0.187594722234240   0.924532017042179   0.389418342308651
   0.024435716847400  -0.480365286582031   0.523756011752416   0.841470984807897
   0.024435716847400  -0.407058136039832  -0.363667410869446   0.909297426825682

According to the result of the operation, in the interval (0.4000,1), the interpolation polynomial can be expressed as follows:

S_2(x)=-0.1627(x-0.4)^3-0.1876(x-0.4)^2+0.9245(x-0.4)+0.3894

Example 4

Select some data points of the function f(x) by yourself, and use the method of cubic spline interpolation to fit these data.

f(x)=(x^2-3x+5)e^{-5x}sinx

untie:

The MATLAB code is as follows:

clc;clear;
x=0:.12:1;
y=(x.^2-3*x+5).*exp(-5*x).*sin(x);
sp=csapi(x,y);
fnplt(sp)
c=[sp.breaks(1:4)' sp.breaks(2:5)' sp.coefs(1:4,:),sp.breaks(5:8)'...
    sp.breaks(6:9)' sp.coefs(5:8,:)]

 operation result:


c =

  columns 1 to 7

                   0   0.120000000000000  24.739556929256214 -19.358812904771273   4.515070686988533                   0   0.480000000000000
   0.120000000000000   0.240000000000000  24.739556929256135 -10.452572410239041   0.937704449187296   0.305791530983672   0.600000000000000
   0.240000000000000   0.360000000000000   4.507107987633279  -1.546331915706831  -0.502164069926209   0.310548976552460   0.720000000000000
   0.360000000000000   0.480000000000000   1.913943736028591   0.076226959841147  -0.678576664630090   0.235810391177767   0.840000000000000

  columns 8 to 12

   0.600000000000000  -0.240386219157313   0.765246704811441  -0.577599824871780   0.158786154419726
   0.720000000000000  -0.477388221775507   0.678707665914812  -0.404325300384630   0.100078340597694
   0.840000000000000  -0.455907214289051   0.506847906075627  -0.262058631745777   0.060507768093483
   0.960000000000000  -0.455907214289066   0.342721308931568  -0.160110325944914   0.035571534465187

 

4. Multivariate cubic spline interpolation

The format for cubic spline interpolation of gridded data with multiple independent variables is as follows:

S=csapi({x1,x2,...,xn},z)
%xi为自变量的网格标志
%z是网格数据的样本点
%得到的S是三次样条函数对象

Example 5

Use the cubic spline interpolation method to obtain the spline interpolation fitting of the z-function grid data, and draw the surface

z=f(x,y)=(x^2-2x)e^{-x^2-y^2-xy}

untie:

The MATLAB code is as follows:

clc;clear;

x0=-3:.6:3;
y0=-2:.4:2;
[x,y]=ndgrid(x0,y0);
%注意此处只能使用ndgrid,否则生成的z矩阵的顺序有问题

z=(x.^2-2*x).*exp(-x.^2-y.^2-x.*y);
sp=csapi({x0,y0},z);
fnplt(sp);

operation result:

In MATLAB, the function spline can also perform cubic spline data interpolation, the format is as follows:

yy=spline(x,y,xx)

Example 6

Perform spline interpolation calculation on data points discretely distributed on the y function curve.

y=e^xsinx

untie:

The MATLAB code is as follows:

clc;clear;

x=[0 2 4 5 8 12 12.8 17.2 19.9 20];
y=exp(x).*sin(x);
xx=0:.25:20;
yy=spline(x,y,xx);
plot(x,y,'o',xx,yy)

 operation result:

 

Guess you like

Origin blog.csdn.net/forest_LL/article/details/124403236