简体   繁体   English

Matlab中的3D图

[英]3D Plot in Matlab

I am trying to draw the figure of a 3D polar plot by converting it from polar to cartesian coordinates and using surf() to plot it. 我试图通过将3D极坐标图从极坐标转换为笛卡尔坐标并使用surf()进行绘制来绘制该图。

Problem: I get the error ??? 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 ??? 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 Where did I go wrong? 我哪里做错了? I am quite new to MATLAB and do not understand what is happening. 我对MATLAB很陌生,不了解发生了什么。

MATLAB Code MATLAB代码

clear all; 清除所有 close all; 关闭所有;

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 plots a surface: so each (x,y) point has a height, z. surf绘制一个表面:因此,每个(x,y)点都有一个高度z。

Eg 例如

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM