Interpolation of a point from contourf

9 次查看(过去 30 天)
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!

回答(1 个)

Saarthak Gupta
Saarthak Gupta 2023-9-14
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:

类别

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

产品

Community Treasure Hunt

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

Start Hunting!

Translated by