matlab - 绘制分段曲面图
问题描述
我正在尝试学习如何绘制具有分段条件的曲面图,但我自己无法弄清楚。这是我迄今为止所拥有的:
[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 文档,这似乎是尝试了细微变化后最正确的版本。
解决方案
此解决方案使用一个简单的匿名函数。通常,最好确保这些是矢量化的(使用.*
而不是*
,.^
而不是^
)以最大化它们的效用并与其他 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)的其他绘图类型感兴趣。这里有很好的例子。
推荐阅读
- html - 通过 css 访问特定的 html 标签
- python - 将参数传递给另一个脚本,然后返回计算值
- c - 检查命令行中的字符串
- python - 为什么 Json 解析会破坏 While 循环?
- python - Dash 中的热重载不会自动更新
- mysql - 如何将 created_at 列添加到我的表中。那么,该列的值必须等于插入命令上的 now() 并且对于所有旧行都必须为空?
- printing - CUPS:如何在点阵打印机上修复右边距
- r - 在 R 中合并多个数据集
- r - 开发函数以在 R 中创建新的数据框
- node.js - 错误 [CredentialsError]:配置中缺少凭据,但仅在 Fargate AWS 上运行时