# Matlabの3Dプロット

Problem: I get the error ```??? 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コード

すべてクリア;     すべてを閉じる。

``````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]);
``````
1

## 1 答え

`surf` plots a surface: so each (x,y) point has a height, 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);
``````
1