首页 > 解决方案 > 绘制分段曲面图

问题描述

我正在尝试学习如何绘制具有分段条件的曲面图,但我自己无法弄清楚。这是我迄今为止所拥有的:

[X,Y] = meshgrid(-10:0.1:10,0:.1:4);
Z =  ((X.^2)/100).*(1-(((Y-2).^2)/4));
C = X.*Y;
surf(X,Y,Z,C)
colorbar
xlabel('X')
ylabel('Y')
zlabel('Z')
%The block of code above looks great for what I need initially

% Now the commented code below is what I was working on and 
% I feel that I have defined the piece-wise function correctly 
% but cannot plot it properly

% syms p(Y)
% p(Y) = piecewise(Y<2, 1, Y>2, -1)
% [X,Y] = meshgrid(-10:0.1:10,0:.1:4);
% Z = zeros(size(X));
% Z = p(Y).*(((X.^2)/100).*(1-(((Y-2).^2)/4)));
% C = X.*Y;
% surf(X,Y,Z,C)
% colorbar

第二个块在某种程度上基于如何在枫树中完成。但是,根据 MA​​TLAB 文档,这似乎是尝试了细微变化后最正确的版本。

标签: matlabplotmatlab-figuresurfacepiecewise

解决方案


此解决方案使用一个简单的匿名函数。通常,最好确保这些是矢量化的(使用.*而不是*.^而不是^)以最大化它们的效用并与其他 MATLAB 函数集成。

yh =@(y) 1*(y<2) + (-1)*(y>2);  % note yh(2) = 0 (can change this if reqd)

[X,Y] = meshgrid(-10:0.1:10,0:.1:4);
Z = yh(Y).*(((X.^2)/100).*(1-(((Y-2).^2)/4)));
C = X.*Y;
surf(X,Y,Z,C)
colorbar

曲面图

免责声明:我承认我对 MATLAB 的符号功能缺乏技能。如果需要,我确信其他用户可以提供答案。

其他可视化:未来的访问者可能对 3 个变量(例如 X、Y、Z)的其他绘图类型感兴趣。这里有很好的例子。


推荐阅读