matlab - 在 matlab 中显示 3-D 图时出错

标签 matlab

我想在matlab中绘制以下函数:
f(x,y) = sqrt(1-x^2-4y^2) ,(if (x^2+4*y^2) <=1 )

        =  0                  ,otherwise.

我在matlab中编写了以下代码:

  x=0:0.1:10;  
  y=0:0.1:10;
  z=x.^2+4*y.^2;
  if (z <=1)
   surf(x,y,z);

  else
   surf(x,y,0);
  end

但显示以下错误:
surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length (X)
我该如何避免这个错误...

最佳答案

我认为你应该真正检查你在做什么......逐行

x = 0:0.1:10; % define x-array 1x101
y = 0:0.1:10; % define y-array 1x101
z = x.^2+4*y.^2; % define z-array 1x101

但是,surf 需要一个矩阵作为 z 的输入,因此您在此处使用的语法不正确。

相反,创建一个 x 网格和 y 网格:

[xx, yy] = meshgrid(x, y); % both being 101x101 matrices

zCheck = xx.^2+4*yy.^2; % 101x101
zz     = sqrt(1-xx.^2-4*y.^2)

关于 if 语句,最好在绘图之前更改值:

zz(zCheck > 1) = 0; % replace the values larger than 1 by zero (use logical indexing)

figure(100);
surf(x, y, zz);

关于matlab - 在 matlab 中显示 3-D 图时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26294667/

相关文章:

matlab - matlab中如何从文本文件中读取矩阵

matlab - Matlab 中的列选择

c++ - 从 float* 转换为 vector<float >

MATLAB - 每个条形图具有不同颜色和刻度标签的条形图

excel - 使用 matlab 添加迷你图到 Excel

algorithm - 如何在 MATLAB 中的 RELIEFF 算法中选择 k 的值

android - 搭建matlab eclipse界面

string - MATLAB 中的数字到字符串的转换

arrays - MATLAB 中整个多维数组(不仅仅是一维)的中位数

c++ - cat(3,A,B) Matlab 函数。相当于 opencv3 (c++)