[英]3D Plot in Matlab
我試圖通過將3D極坐標圖從極坐標轉換為笛卡爾坐標並使用surf()
進行繪制來繪制該圖。
問題:我得到了錯誤??? CData must be an M-by-N matrix or M-by-N-by-3 array
??? CData must be an M-by-N matrix or M-by-N-by-3 array
。 我哪里做錯了? 我對MATLAB很陌生,不了解發生了什么。
MATLAB代碼
清除所有 關閉所有;
N=50;
%define matrices for ploting
x=zeros(N,N,N);
y=zeros(N,N,N);
z=zeros(N,N,N);
f=zeros(N,N,N);
%define basic input variables
r=linspace(0,2,N);
theta=linspace(0,pi,N);
phi=linspace(0,2.*pi,N);
for ii=(1:N) %use ii, jj to avoid confusion with the imaginary units
for jj=(1:N)
for kk=(1:N)
x(ii,jj,kk)=r(ii).*sin(theta(jj)).*cos(phi(kk)); %%not using
%%for loop probably can work too, test later
y(ii,jj,kk)=r(ii).*sin(theta(jj)).*sin(phi(kk));
z(ii,jj,kk)=r(ii).*cos(theta(jj));
f(ii,jj,kk)=r(ii).*exp(-r(ii)).*cos(theta(jj));
end
end
end
figure;
surf(x,y,z,f);
colormap([1,1,1]);
surf
繪制一個表面:因此,每個(x,y)點都有一個高度z。
例如
N=50;
[X Y] = meshgrid(linspace(0,2,N));
Z = zeros(size(X));
for ii=(1:N)
for jj=(1:N)
x = X(ii,jj);
y = Y(ii,jj);
[theta, r] = cart2pol(x,y);
Z(ii,jj) =r * exp(-r) * cos(theta);
end
end
figure;
surf(X,Y,Z);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.