Hi, I am trying to implement a 3D cylinder code, and here is the code:
nCS = 2; %number of cross section of cylinder
nNodes = 50; %number of nodes per cross section
rSC = size * ones(1, nNodes); %set length of each vertices
theta = linspace(0, 2* pi, nNodes); %set angle of each vertices
polar(theta, rSC)
zSC = linspace(0, hSC, nCS)'; %create z coordinates, with height hSC
[xSC,ySC] = pol2cart(theta, rSC); %transform from polar to cartisen
XSC = repmat(xSC, nCS, 1); %repeat for array of number of cross sections
YSC = repmat(ySC, nCS, 1);
ZSC = repmat(zSC, 1, nNodes); %repeat for number of z-layers
xSC_lid = zeros(2, nNodes); %creating the top and bottom lid
ySC_lid = zeros(2, nNodes);
zSC_lid = repmat([0, hSC], 1, nNodes);
X = [xSC_lid(1,:); XSC; xSC_lid(2,:)];
Y = [ySC_lid(1,:); YSC; ySC_lid(2,:)];
Z = [zSC_lid(1,:); ZSC; zSC_lid(2,:)];
surf(X, Y, Z)
The error message is
What is wrong and how to solve this?

 採用された回答

Matt J
Matt J 2022 年 12 月 3 日
編集済み: Matt J 2022 年 12 月 3 日

1 投票

Much of your code seems to be an unnecessary reinvention of the cylinder() command.
nCS = 2; %number of cross section of cylinder
nNodes = 50; %number of nodes per cross section
[X,Y,Z]=cylinder(ones(1,nCS),nNodes);
surf(X,Y,Z,'FaceAlpha',0.7)
patch('XData',X(1,:),'YData',Y(1,:), 'ZData',Z(1,:),'FaceColor','r');
patch('XData',X(nCS,:),'YData',Y(nCS,:), 'ZData',Z(nCS,:),'FaceColor','r');

6 件のコメント

Miles Hao Peng Su
Miles Hao Peng Su 2022 年 12 月 3 日
I see, however I wish to stack 2 cylinders together, which I am thinking hard to implement through cylinder(m).
May I know if you know how to implement this?
say cylinder1 has a height of 2, and I want to have the base of cylinder2 (height 3) to be ontop of cylinder1?
Matt J
Matt J 2022 年 12 月 3 日
Do they have different radii? Otherwise, the stacking is equivalent to taking nCS=3.
Miles Hao Peng Su
Miles Hao Peng Su 2022 年 12 月 3 日
Oh you're right! I never thought of this. My model also has an ring on top of the cylinder, which has a different radii than the cylinder. Can I also use the cylinder(m) function? How do you stack the ring onto the cylinder?
Matt J
Matt J 2022 年 12 月 3 日
Just add shifts and scalings to the X,Y,Z coordinates as appropriate
cappedCylinder(2,4,0)
cappedCylinder(4,1,4)
function cappedCylinder(r, h, z0)
nCS = 2; %number of cross section of cylinder
nNodes = 50; %number of nodes per cross section
[X,Y,Z]=cylinder(ones(1,nCS),nNodes);
X=r*X; Y=r*Y; Z=h*Z+z0;
surface(X,Y,Z,'FaceAlpha',0.5,'FaceColor','g')
patch('XData',X(1,:),'YData',Y(1,:), 'ZData',Z(1,:),'FaceColor','r');
patch('XData',X(nCS,:),'YData',Y(nCS,:), 'ZData',Z(nCS,:),'FaceColor','r');
view(3); axis equal
end
Miles Hao Peng Su
Miles Hao Peng Su 2022 年 12 月 4 日
編集済み: Miles Hao Peng Su 2022 年 12 月 4 日
Hi! May I ask what is the function of the ones(1,nSC) in the code?
[X,Y,Z]=cylinder(ones(1,nCS),nNodes);
I am curious, because when I pass just 1 through returns me the same result. Like so:
[XSC,YSC,ZSC]=cylinder(1,nNodes);
Why do we need to pass radius 2 times? Oh and anyway I only need the top and bottom 2 slices, so I don't really know if I need the nCS there.
Miles Hao Peng Su
Miles Hao Peng Su 2022 年 12 月 4 日
Hi, I got any other qusetion >< with this method is it possible to generate a hollow cylinder? like the walls have a certain thickness and the middle is void. Or would you suggest to use multicylinder for this?

サインインしてコメントする。

その他の回答 (1 件)

Torsten
Torsten 2022 年 12 月 3 日

1 投票

zSC_lid is an 1 x nNodes vector:
zSC_lid = repmat([0, hSC], 1, nNodes);
Thus it has only one row.
But you reference the second row in the command
Z = [zSC_lid(1,:); ZSC; zSC_lid(2,:)];
which does not exist.
Thus MATLAB throws an error.

1 件のコメント

Miles Hao Peng Su
Miles Hao Peng Su 2022 年 12 月 3 日
I see! I will try to fix this. Thank you!!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Get Started with Aerospace Blockset についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by