How to get contour plot and line plot intersection values/ coordinates

조회 수: 12 (최근 30일)
Mahi Nazir
Mahi Nazir 2013년 7월 16일
I am plotting a 3D graph and a contour of the same. Then I plot few lines on the contour plot. I want to know the value of the contour where the line crosses the contour and also the coordinates where intersection happens. Can anyone please help me with this

답변 (1개)

Kelly Kearney
Kelly Kearney 2013년 7월 16일
I assume by 3D graph with contour, you mean you have 3D data represented in 2D by a contour plot? Or do you mean something like contour3 or isosurface?
Assuming the former, the coordinates of the contour lines generated via
[c,h] = contour(x,y,z);
are found in the c matrix. There are a few File Exchange functions (like contourcs) that can simplify the task of extracting those coordinates.
If you have the Mapping Toolbox, polyxpoly can calculate the line intersections. Otherwise, there are a bunch of FEX entries to calculate the intersections of two lines/curves (like this one).
  댓글 수: 3
Mahi Nazir
Mahi Nazir 2013년 7월 18일
And yes after I get the intersection coordinates of the line and contour, I require the value of contour (from 3D graph) at those coordinates.
Kelly Kearney
Kelly Kearney 2013년 7월 19일
Yes, to use the contourcs function, you need to download it from the File Exchange, and then either place it in the same folder as where you are working, or add it to your path (You can read up on that under the "Search Path" section of the documentation).
Here's a quick example of how to use those functions to calculate intersections:
% Some example data
[x,y,z] = peaks;
% Calculate the coordinates of the contour lines
% (here, just the contour line at z = 0)
C = contourcs(x(1,:),y(:,1),z,[0 0]);
% Coordinates of the intersecting line
xln = [0 0];
yln = [-3 3];
% Use polyxpoly to find intersections
for ic = 1:length(C)
[xint{ic}, yint{ic}] = polyxpoly(xln, yln, C(ic).X, C(ic).Y);
end
xint = cat(1, xint{:});
yint = cat(1, yint{:});
% Plot the results
figure;
contour(x,y,z,[0 0], 'k');
hold on;
plot(xln, yln, 'b');
plot(xint, yint, 'r*');

댓글을 달려면 로그인하십시오.

카테고리

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