我正在尝试学习如何绘制具有分段条件的曲面图,但无法自己解决。这是我到目前为止:
[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
第二个块在某种程度上取决于如何在枫树中完成。但是,基于MATLAB文档,这似乎是尝试轻微变化后最正确的版本。
此解决方案使用简单的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
免责声明:我承认我缺乏MATLAB的符号功能。我敢肯定,如果需要,另一位用户可以提供答案。
其他可视化:未来访问者可能对3个变量(例如X,Y,Z)的其他绘图类型感兴趣。好例子here。