Plot only the surfaces within a bounding surface?

조회 수: 3 (최근 30일)
iontrap
iontrap 2023년 5월 30일
댓글: Mathieu NOE 2023년 5월 31일
I have a large cylinder with base in the x-y plane intersected by smaller cylinders with base in the x-z plane. I'd like to plot only the surfaces belonging to the larger cylinder, however my smaller cylinder sticks out of the larger volume. How can I make the surface of the small cylinder bounded by the large cylinder?
Below is the code.
rs = 9.0;
Cxt = -6.0;
Czt = 3.0;
dia_tube = 1.0;
% for the larger cylinder plotting
[xs1 ys1] = GetCircle(rs, 0, 0, 0, 2*pi);
Ns = length(xs1);
zmins = zeros(1,Ns);
zmaxs = zeros(1,Ns) + 6;
xs = [xs1;xs1];
ys = [ys1;ys1];
zs = [zmins;zmaxs];
surf(xs,ys,zs);
alpha 0.5;
hold on
% for smaller cylinder(s)
[xt zt] = GetCircle(dia_tube*0.5, Cxt, Czt, 0, 2*pi);
for i = 1:Ns
ymin = -(rs^2 - xt(i)^2)^(1/2);
ymax = (rs^2 - xt(i)^2)^(1/2);
end
xt = [xt;xt];
yt = [ymin;ymax];
zt = [zt;zt];
surf(xt,yt,zt);
axis equal
% with the circle generator
function [x y] = GetCircle(r, h, k, a, b)
t = linspace(a, b, 40);
x = r*cos(t) + h;
y = r*sin(t) + k;
end
I was hoping ymin and ymax would allow me to do this, but we just find the min and max of the x data such that the edge of the tubes are aligned.
I also tried changing
yt = [ys1;ys1];
to match the cylinder case, but this left me with a weird 2D shape. Thanks for your help.

채택된 답변

iontrap
iontrap 2023년 5월 31일
편집: iontrap 2023년 5월 31일
I was able to figure out the solution:
[xt zt] = GetCircle(dia_tube*0.5, Cxt, Czt, 0, 2*pi); %% make a circle for the small cylinder
for i = 1:Ns
y_temp(i) = sqrt(rs^2 - (xt(i))^2); %% the y value belonging to the large cylinder corresponding to a given x value of small cylinder.
end
xt3 = [xt;xt]; %% change this to a different variable for clarity.
yt3 = [y_temp;-y_temp];
zt3 = [zt;zt];
surf(xt3,yt3,zt3);
gives the result -
  댓글 수: 3
iontrap
iontrap 2023년 5월 31일
Great! Thanks for your help.
Mathieu NOE
Mathieu NOE 2023년 5월 31일
my pleasure !

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mathieu NOE
Mathieu NOE 2023년 5월 31일
hello
seems to me there is no need for a for loop to compute ymin & ymax
also ymin = - ymax , so we can avoid creating yet another variable in the workspace
and ymax can be directly computed as
ymax = (rs^2 - (abs(Cxt)+0.5*dia_tube)^2)^(1/2);
final result :
rs = 9.0;
Cxt = -6.0;
Czt = 3.0;
dia_tube = 1.0;
% for the larger cylinder plotting
[xs1, ys1] = GetCircle(rs, 0, 0, 0, 2*pi);
Ns = length(xs1);
zmins = zeros(1,Ns);
zmaxs = zeros(1,Ns) + 6;
xs = [xs1;xs1];
ys = [ys1;ys1];
zs = [zmins;zmaxs];
surf(xs,ys,zs);
alpha 0.5;
hold on
% for smaller cylinder(s)
[xt, zt] = GetCircle(dia_tube*0.5, Cxt, Czt, 0, 2*pi);
ymax = (rs^2 - (abs(Cxt)+0.5*dia_tube)^2)^(1/2);
xt = [xt;xt];
yt = [ymax;-ymax];
zt = [zt;zt];
surf(xt,yt,zt);
axis equal
% with the circle generator
function [x, y] = GetCircle(r, h, k, a, b)
t = linspace(a, b, 40);
x = r*cos(t) + h;
y = r*sin(t) + k;
end
  댓글 수: 1
iontrap
iontrap 2023년 5월 31일
Thanks for your response. The reason I tried to use a loop was to create a curved bounding surface rather than a planar bounding surface. There is still the problem shown in the attached picture.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by