How do I plot the level sets of one function projected onto a surface, or how do I project contours onto a surface object in MATLAB?

18 views (last 30 days)
I would like to plot the level sets of a function onto a surface. I have the equation defining the surface, as well as the set levels in term of the three variables, [X, Y, Z]

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The ability to automatically have contour levels projected onto a surface is not available directly in MATLAB.
To work around this issue, read the commented example below:
The following example demonstrates the plotting of level sets of a function onto the surface as defined by another function, all in three variables.
[X,Y] = meshgrid([-2:.25:2]);
% Plotting the Z-values of the function whose level sets have
% to be determined
Z = X.*exp(-X.^2-Y.^2);
% Plotting the Z-values of the function on which the level
% sets have to be projected
Z1 = X.^2+Y.^2;
% Plot your contour
[cm,c]=contour(X,Y,Z,30);
% Plot the surface on which the level sets have to be projected
s=surface(X,Y,Z1,'EdgeColor',[.8 .8 .8],'FaceColor','none')
% Get the handle to the children i.e the contour lines of the contour
cv=get(c,'children');
% Extract the (X,Y) for each of the contours and recalculate the
% Z-coordinates on the surface on which to be projected.
for i=1:length(cv)
cc = cv(i);
xd=get(cc,'XData');
yd=get(cc,'Ydata');
zd=xd.^2+yd.^2;
set(cc,'Zdata',zd);
end
grid off
view(-15,25)
colormap cool

More Answers (0)

Categories

Find more on Contour Plots in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!