Interp2 with changing y and z-axis

3 vues (au cours des 30 derniers jours)
Christoph
Christoph le 12 Fév 2024
Réponse apportée : Hassaan le 12 Fév 2024
Hello,
I'd like to interpolate my map below based on z = f(x,y) with interp2, but it seems like interp2 can't handle it if the y-coordinate is not fixed, but changes depending on it's position in the map. This leads to an area in the contour-plot that is not allocated with data.
contour(x,y,z)
e.g. a snippet of y:
-688.2593 -661.9470
-687.8115 -661.3321
-687.3288 -660.7753
-687.0811 -660.3903
all the variables have the same size and are plottable, but i cant use interp2:
% Define the input data
x = x(:,1:18); % size 18x18
y = y(:,1:18); % size 18x18
z = z(:,1:18); % size 18x18
% Define the interpolation grid
x_interp = 2000;
y_interp = 50;
% Perform interpolation
interp_func = interp2(x, y, z,x_interp,y_interp);
ERROR:
Error using griddedInterpolant
Grid arrays must have NDGRID structure.
Error in interp2>makegriddedinterp (line 226)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 134)
F = makegriddedinterp(X, Y, V, method,extrap);
Error in parameter_interpolation (line 13)
interp_func = interp2(x, y, z,x_interp,y_interp);
Have you encountert similar issues? Or give me a hint how to overcome this?
Thank you very much!!!

Réponses (1)

Hassaan
Hassaan le 12 Fév 2024
@Christoph Try this a quick overview:
Step 1: Create a Regular Grid
First, determine the range of your x and y data and create a regular grid using meshgrid. For example:
% Determine the range and create a regular grid
x_range = linspace(min(x(:)), max(x(:)), 100); % Adjust 100 to your desired resolution
y_range = linspace(min(y(:)), max(y(:)), 100); % Adjust 100 to your desired resolution
[xq, yq] = meshgrid(x_range, y_range);
Step 2: Use griddata for Interpolation
Now, use griddata to interpolate your z values onto the regular grid:
% Interpolate z onto the regular grid
zq = griddata(x, y, z, xq, yq, 'linear'); % You can also try 'cubic' or 'nearest'
Step 3: Use interp2 if Further Interpolation is Needed
After you have zq defined on a regular grid, you can use interp2 for any further interpolation, as xq, yq, and zq now have the required structure.
% Further interpolation (if needed)
z_interp = interp2(xq, yq, zq, x_interp, y_interp);
Additional Note
  • If your data contains NaNs or if the interpolation results in unexpected values, consider using different interpolation methods with griddata or cleaning your data beforehand.
  • The choice between linear, cubic, and nearest in griddata can significantly affect your results, especially near the boundaries of your data set. Experiment with these to see which gives the best results for your particular data.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Catégories

En savoir plus sur Interpolating Gridded Data dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by