Interpolating Data in 2D Mesh or Surf Plot?
7 views (last 30 days)
Show older comments
Sees on 13 Dec 2022
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:
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?
[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');
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.
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);
stem3(Xq,Yq,Zq,'bo'); xlabel('X'); ylabel('Y'); zlabel('Z')
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.
[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.
etc. Make vectors X, Y, Z which do not have the Z=-99 points, like we did above.
Find the interpolating function:
Make grids of query points Xq, Yq, with meshgrid() or however you want.
Then interpolate Z at the query points:
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
Display results graphically:
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.
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!