Plot only the surfaces within a bounding surface?

6 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

更多回答(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