簡體   English   中英

在Matlab中顯示3-D繪圖時出錯

[英]error in displaying a 3-D Plot in 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM