Clear Filters
Clear Filters

Interpolation of a point from contourf

4 views (last 30 days)
Tal Shavit
Tal Shavit on 1 Aug 2022
Answered: Saarthak Gupta on 14 Sep 2023
I need to interpolate the x,y coordinates for Z=1 in the following contour:
the point 1 doesn't exist in my data.
and my data:
x:
y:
I have figured the existing point closest to one, but I need 1 exactly.
Thank you very much!

Answers (1)

Saarthak Gupta
Saarthak Gupta on 14 Sep 2023
Hi,
I understand you are trying to find the X and Y values for the level Z = 1. According to the query, the z-coordinates you have given as input to the ‘contourf’ function do not contain 1.
You can use a list of levels where the contour lines will be displayed and include 1 in it.
Refer to the following code:
x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X) + cos(Y);
% Specify 10 levels, and explicitly include 1 in the list of levels
levels = [linspace(-pi/2,1,4) 1 linspace(1,pi/2,5)];
M = contourf(X,Y,Z,levels);
% To check if 1 is part of the levels, get the list of levels from the contour matrix
levelList = findAllLevels(M);
function levels = findAllLevels(M)
% This helper function extracts all levels/heights from the contour matrix returned by contourf
n=size(M,2);
j=1;
i=1;
while i+M(2,i)+1<n
i = i+M(2,i)+1;
levels(j) = M(1,i);
j = j+1;
end
end
This would ensure a contour line is plotted at Z = 1, and the x and y coordinates at this contour line can be obtained through the contour matrix ‘M’. You can observe the same by inspecting ‘M’ in the editor window:
Please refer to the following MATLAB documentation for further reference:

Categories

Find more on Contour Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!