绘制分段表面图

时间:2019-03-13 20:02:20

标签: matlab plot matlab-figure surface piecewise

我正在尝试学习如何以分段条件绘制表面图,但是我自己无法解决。到目前为止,这是我所拥有的:

[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

第二个块在某种程度上基于如何在maple中完成。但是根据MATLAB文档,在尝试稍作改动后,这似乎是最正确的版本。

1 个答案:

答案 0 :(得分:1)

此解决方案使用简单的anonymous function。通常,最好确保对它们进行矢量化处理(使用.*而不是*.^而不是^)以最大程度地发挥其效用并与其他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

Surface plot

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

其他可视化效果:将来的访问者可能会对3种变量(例如X,Y,Z)的其他地块类型感兴趣。很好的例子here