Plot only the surfaces within a bounding surface?

2 Ansichten (letzte 30 Tage)
iontrap
iontrap am 30 Mai 2023
Kommentiert: Mathieu NOE am 31 Mai 2023
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.

Akzeptierte Antwort

iontrap
iontrap am 31 Mai 2023
Bearbeitet: iontrap am 31 Mai 2023
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 Kommentare
iontrap
iontrap am 31 Mai 2023
Great! Thanks for your help.
Mathieu NOE
Mathieu NOE am 31 Mai 2023
my pleasure !

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Mathieu NOE
Mathieu NOE am 31 Mai 2023
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 Kommentar
iontrap
iontrap am 31 Mai 2023
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.

Melden Sie sich an, um zu kommentieren.

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by