interp2 problem in size dimesion

16 次查看(过去 30 天)
I want to make cubic spline for density as function of P and T , I tried to use interp2 to evaluate density at specific p and T but I got the error of Interpolation requires at least two sample points for each grid dimension.
Here is the code
num = readtable("co2_T.xls");
P= table2array(num(:,1));% first column
T= table2array (num(:,2));%second column
z= table2array (num(:,5));% third column
v3=interp2(P,T,z,14.7,120)

回答(1 个)

John D'Errico
John D'Errico 2022-11-3
编辑:John D'Errico 2022-11-3
Your problem is you are trying to use the wrong tool. interp2 allows you to interpolate in two dimensions, but ONLY for data that lies on a grid in those variables. You have what is commonly known as scattered data. So you need to use a tool that can perform interpolation on scattered data.
help scatteredInterpolant
scatteredInterpolant Scattered data interpolation scatteredInterpolant performs interpolation on scattered data that resides in 2-D or 3-D space. A scattered data set is defined by sample points X and corresponding values v. A scatteredInterpolant object F represents a surface of the form v = F(X). Interpolated values vq at query points Xq are obtained by evaluating the interpolant, vq = F(Xq). F = scatteredInterpolant creates an empty scattered data interpolant. F = scatteredInterpolant(X,v) creates an interpolant that fits a surface of the form v = F(X) to the sample data set (X,v). The sample points X must have size NPTS-by-2 in 2-D or NPTS-by-3 in 3-D, where NPTS is the number of points. Each row of X contains the coordinates of one sample point. The values v must be a column vector of length NPTS. F = scatteredInterpolant(x,y,v) and F = scatteredInterpolant(x,y,z,v) also allow the sample point locations to be specified in alternative column vector format when working in 2-D and 3-D. F = scatteredInterpolant(...,METHOD) specifies the method used to interpolate the data. METHOD must be one of the following: 'linear' - (default) Linear interpolation 'nearest' - Nearest neighbor interpolation 'natural' - Natural neighbor interpolation The 'natural' method is C1 continuous except at the sample points. The 'linear' method is C0 continuous. The 'nearest' method is discontinuous. F = scatteredInterpolant(...,METHOD,EXTRAPOLATIONMETHOD) also specifies the extrapolation method used for query points outside the convex hull. EXTRAPOLATIONMETHOD must be one of the following: 'linear' - (default if METHOD is 'linear' or 'natural') Linear extrapolation based on boundary gradients 'nearest' - (default if METHOD is 'nearest') Evaluates to the value of the nearest neighbor on the convex hull boundary 'none' - Queries outside the convex hull return NaN Example: % Construct a scatteredInterpolant F from locations x,y and values v t = linspace(3/4*pi,2*pi,50)'; x = [3*cos(t); 2*cos(t); 0.7*cos(t)]; y = [3*sin(t); 2*sin(t); 0.7*sin(t)]; v = repelem([-0.5; 1.5; 2],length(t)); F = scatteredInterpolant(x,y,v) % Evaluate F at query locations xq,yq to obtain interpolated values vq tq = linspace(3/4*pi+0.2,2*pi-0.2,40)'; xq = [2.8*cos(tq); 1.7*cos(tq); cos(tq)]; yq = [2.8*sin(tq); 1.7*sin(tq); sin(tq)]; vq = F(xq,yq); % Plot the sample data (x,y,v) and interpolated query data (xq,yq,vq) plot3(x,y,v,'.',xq,yq,vq,'.'), grid on title('Linear Interpolation') xlabel('x'), ylabel('y'), zlabel('Values') legend('sample data','interpolated query data','Location','best') % Change the interpolation method from 'linear' to 'nearest' F.Method = 'nearest' % Perform nearest neighbor interpolation and plot the result vq = F(xq,yq); figure plot3(x,y,v,'.',xq,yq,vq,'.'), grid on title('Nearest Interpolation') xlabel('x'), ylabel('y'), zlabel('Values') legend('sample data','interpolated query data','Location','best') scatteredInterpolant properties: Points - Locations of the scattered sample points Values - Values associated with each sample point Method - Method used to interpolate at query points ExtrapolationMethod - Extrapolation method used outside the convex hull scatteredInterpolant methods: vq = F(Xq) evaluates the scatteredInterpolant F at scattered query points Xq and returns a column vector of interpolated values vq. Each row of Xq contains the coordinates of one query point. vq = F(xq,yq) and vq = F(xq,yq,zq) also allow the scattered query points to be specified as column vectors of coordinates. Vq = F(Xq,Yq) and Vq = F(Xq,Yq,Zq) evaluates F at gridded query points specified in full grid format as 2-D and 3-D arrays created from grid vectors using [Xq,Yq,Zq] = NDGRID(xqg,yqg,zqg). Vq = F({xqg,yqg}) and Vq = F({xqg,yqg,zqg}) also allow a grid of query points to be specified in compact form as grid vectors. Use this syntax to conserve memory when the query points form a large grid. Vq has the same size as the grid: LENGTH(xqg)-by-LENGTH(yqg) or LENGTH(xqg)-by-LENGTH(yqg)-by-LENGTH(zqg). See also griddedInterpolant, griddata, delaunayTriangulation Documentation for scatteredInterpolant doc scatteredInterpolant
Of course, you are asking to do a cubic spline interpolation, and scatteredInterpolant does not have that as an option. Instead, you can do a little better using griddata. As you can see, griddata does off a cubic interpolant, though I think I recall it is not a true cubic spline, in the sense of being twice differentiable everywhere. Even so, it may be sufficient for your needs. Or you can use the 'v4' method, which is also smooth, though it has been some time since I looked at it to remember the exact nature of that interpolation method.
help griddata
GRIDDATA Interpolates scattered data - generally to produce gridded data Vq = griddata(X,Y,V,Xq,Yq) fits a surface of the form V = F(X,Y) to the scattered data in (X, Y, V). The coordinates of the data points are defined by the vectors (X,Y) and V defines the corresponding values. griddata interpolates the surface F at the query points (Xq,Yq) and returns the values in Vq. The query points (Xq, Yq) generally represent a grid obtained from NDGRID or MESHGRID, hence the name GRIDDATA. Vq = griddata(X,Y,Z,V,Xq,Yq,Zq) fits a hyper-surface of the form V = F(X,Y,Z) to the scattered data in (X, Y, Z, V). The coordinates of the data points are defined by the vectors (X,Y,Z) and V defines the corresponding values. griddata interpolates the surface F at the query points (Xq,Yq,Zq) and returns the values in Vq. Vq = griddata(X,Y,V,xq,yq) where xq is a row vector and yq is a column vector, expands (xq,yq) via [Xq,Yq] = meshgrid(xq,yq). [Xq,Yq,Vq] = griddata(X,Y,V,xq,yq) also returns the expanded grid coordinate arrays (Xq,Yq) obtained from meshgrid. GRIDDATA(..., METHOD) where METHOD is one of 'nearest' - Nearest neighbor interpolation 'linear' - Linear interpolation (default) 'natural' - Natural neighbor interpolation 'cubic' - Cubic interpolation (2D only) 'v4' - MATLAB 4 griddata method (2D only) defines the interpolation method. The 'nearest' and 'linear' methods have discontinuities in the zero-th and first derivatives respectively, while the 'cubic' and 'v4' methods produce smooth surfaces. All the methods except 'v4' are based on a Delaunay triangulation of the data. Example 1: % Interpolate a 2D scattered data set over a uniform grid xy = -2.5 + 5*gallery('uniformdata',[200 2],0); x = xy(:,1); y = xy(:,2); v = x.*exp(-x.^2-y.^2); [xq,yq] = meshgrid(-2:.2:2, -2:.2:2); vq = griddata(x,y,v,xq,yq); mesh(xq,yq,vq), hold on, plot3(x,y,v,'o'), hold off Example 2: % Interpolate a 3D data set over a grid in the x-y (z=0) plane xyz = -1 + 2*gallery('uniformdata',[5000 3],0); x = xyz(:,1); y = xyz(:,2); z = xyz(:,3); v = x.^2 + y.^2 + z.^2; d = -0.8:0.05:0.8; [xq,yq,zq] = meshgrid(d,d,0); vq = griddata(x,y,z,v,xq,yq,zq); surf(xq,yq,vq); See also scatteredInterpolant, GRIDDATAN, MESHGRID, NDGRID, DELAUNAY, INTERPN. Documentation for griddata doc griddata

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by