Interpolating Data in 2D Mesh or Surf Plot?
7 views (last 30 days)
Show older comments
Hello dear Matlab Community!
I'm struggling with interpolating data to refine the resolution for a mesh and/or surf plot. I've tried using interp2, datagrid and scatteredinterpolant however none of them seemed to work with the inputs I gave them. I want the 0 values of my z coordinates to be interpolated and also want a higher resolution such as described in the interp2 documentation (Link)
Here's an example as a dataset. I also attached the .csv files
Thank you in advance!
These are my X-Coordinates:

The Y-Coordinates:

The Z-Coordinates

1 Comment
Star Strider
on 13 Dec 2022
It would help to have your code to understand what you did.
What results did you get?
What results do you want?
Answers (1)
William Rose
on 13 Dec 2022
Edited: William Rose
on 13 Dec 2022
[edit: I hit Submit belfore I was done]
You say you want to interpolate z values at the points where z=0, and you want a finer mesh.
You have too few non-zero z values for interpolation to work reliably. The grid has 102 positions, and you have only 21 non-zero values. All the boundary points are unknown. See plot below. It shows the X,Y locations of the known and unknown (Z=0) points.
X=load('X_Cords.csv'); Y=load('Y_Cords.csv'); Z=load('Z_Cords.csv');
plot(X(abs(Z)>0),Y(abs(Z)>0),'r*',X(Z==0),Y(Z==0),'bo')
xlabel('X'); ylabel('Y')
legend('Known','Unknown','Location','southwest')
You are mostly extrapolating, not interpolating. You need more known points to do interpolation with any confidence. Points on the boundary are especially valuable, since with them, you will be interpolating, and not extrapolating.
Let's add query points to the plot above: locations on a regular grid, where we want to estimate the Z value. We will create an array of query points with the same dimensions as the initial arrays. The query points, at which we want to estimate Z, are shown in green below.
Xq=X; Yq=repmat([0:-0.03:-0.15]',1,17);
plot(X(abs(Z)>0),Y(abs(Z)>0),'r*',X(Z==0),Y(Z==0),'bo',Xq,Yq,'gx')
xlabel('X'); ylabel('Y')
legend('Known','Unknown','Query','Location','southwest')
Let's discard the locaitons where Z=0 and then try to interpolate.
Xnz=X(abs(Z)>0); Ynz=Y(abs(Z)>0); Znz=Z(abs(Z)>0);
We can't use interp2(), since the known data are not on a regular grid. We can use scatteredInterpolant() instead. The arrays must be reshaped to columns for scatteredInterpolant().
Xnz=reshape(Xnz,[],1); Ynz=reshape(Ynz,[],1); Znz=reshape(Znz,[],1);
F=scatteredInterpolant(Xnz,Ynz,Znz);
Zq=F(Xq,Yq);
stem3(Xq,Yq,Zq,'bo'); xlabel('X'); ylabel('Y'); zlabel('Z')
hold on
stem3(Xnz,Ynz,Znz,'r*');
The plot includes vertical stems from the z=0 plane. If you run it in Matlab, you can spin it around in 3D, using the 3D rotate handle in the plot window, and see if you like it. I would never use results like this, since it is almost pure extrapolation.
3 Comments
William Rose
on 13 Dec 2022
Edited: William Rose
on 13 Dec 2022
[Edit: Replace Z with Zo in lines to remove Zo=-99 points. Attach csv files. Add plots.]
Enter a value of Z=-99 at the unknown points in Excel spreadsheet. Keep values of Z=0, where zero is known. Save the spreadsheet arrays in CSV files as before. In your script, read in the data from the files, into arrays Xo,Yo,Zo.
Xo=load('X_Cords.csv');
Yo=load('Y_Cords.csv');
Zo=load('Z_Cords.csv');
etc. Make vectors X, Y, Z which do not have the Z=-99 points, like we did above.
X=reshape(Xo(Zo~=-99),[],1);
Y=reshape(Yo(Zo~=-99),[],1);
Z=reshape(Zo(Zo~=-99),[],1);
Find the interpolating function:
F=scatteredInterpolant(X,Y,Z);
Make grids of query points Xq, Yq, with meshgrid() or however you want.
[Xq,Yq]=meshgrid(0:.215:4.3,0:-.02:-.16);
Then interpolate Z at the query points:
Zq=F(Xq,Yq);
In my earlier answer, I did not interpolate values at the original unknown points. If it is important to you to estimate Z at those points, then do it, using Xo and Yo as query points
Zo=F(Xo,Yo);
Display results graphically:
plot(X,Y,'r*',Xq,Yq,'bo')
xlabel('X'); ylabel('Y')
legend('Known','Query','Location','southwest')
figure
stem3(Xq,Yq,Zq,'bo'); hold on; stem3(X,Y,Z,'r*');
xlabel('X'); ylabel('Y'); zlabel('Z')
You have repeated points at X=0,Y=0 and repeated points at X=4.3,Y=0.
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!