hey guys im trying to plot this in 3D but i keep getting an error, "Data cannot have more than 2 dimensions."

4 views (last 30 days)
here is my code;
% 1) Compute the volume of a cylindrical shell with an inner radius
% r1=5,
% an outer radius
% r2=6, and length=10.
r1 = 5; %inner radius
r2 = 6; %outer radius
h = 10; %height
dr = 0.01;
dz = 0.1;
dphi = 0.01;
% set vector of iteration
r = [r1:dr:r2];
z = [0:dz:h];
phi = [0:dphi:2*pi];
noi_r = length(r);
noi_phi = length(phi);
noi_z = length(z);
x = zeros(noi_r,noi_phi,noi_z);
y = zeros(noi_r,noi_phi,noi_z);
z = zeros(noi_r,noi_phi,noi_z);
plot3(NaN,NaN,NaN,'.')
hold on
%initial volume
Volume =0;
for ir = 1:noi_r
for iphi = 1:noi_phi
for iz = 1:noi_z
dv = r(ir)*dphi*dz*dr;
Volume = Volume +dv;
x(ir,iphi,iz) = r(ir)*cos(phi(iphi));
y(ir,iphi,iz) = r(ir)*sin(phi(iphi));
z(ir,iphi,iz) = z(iz);
plot3(x,y,z,'.')
end
end
end
hold off
disp('volume is = ')
disp(Volume)

Accepted Answer

David K.
David K. on 7 Aug 2019
So the issue is that plot3 takes either a vector or a 2d matrix. Since you are plotting it as points you do not actually need to make your x,y, and z 3 dimensional. If you wanted you could have them as one long vector. So, to fix your issue you can do a few things. First, change your plot3 to
plot3(x(ir,iphi,iz),y(ir,iphi,iz),z(ir,iphi,iz),'.')
However, plotting this many times in a loop is extremely slow. Instead, after the loop you can do this
plot3(x(:),y(:),z(:),'.')
However, right now, your code will create a cylindrical shell at z = 0 and not have any height to it. This is because you are overwriting your z vector with zeros. I would suggest changing those names so you can have it be the proper height.
  3 Comments

Sign in to comment.

More Answers (1)

boutros baqain
boutros baqain on 7 Aug 2019
Thank you so much David k. I will try what you’ve suggested. Thank you!

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!