How to add multiple cylinders to a surface in matlab - matlab

I am trying to add multiple cylinders to the surface in order to 3D print the model. I want to remove the points of the surface that fall within the cylinder.
The following is the code used to generate the surface
%% Generate Clebsch %%
CubeSize = 10;
Thickness = 0.1;
min = -CubeSize/2;
max = CubeSize/2;
v = linspace(min,max,200);
[x, y, z] = meshgrid(v);
a = sqrt(5);
c = sqrt(2);
figure(111)
clebsch_surface = 2*c*y.^3+2*x.^2.*z-8*y.^2.*z+3*c*y.*z.^2-z.^3-2*x.^2+8*y.^2-10*c*y.*z+3*z.^2+3*c*y-3*z+1;
fv = isosurface(x,y,z,clebsch_surface,0);
[f,v] = isosurface(x,y,z,clebsch_surface,0);
p = patch(fv);
isonormals(x,y,z,clebsch_surface,p);
p.FaceColor = 'red';
p.EdgeColor = 'none';
view([-65,20]);
camlight left;
lighting gouraud;
axis tight;
Then I need to find the lines that fall on the cylinders, so I have 27 formulas to determine these cylinders (I show two of the lines below) and then I want to determine a cylinder around each line and then remove the points within the cylinder of the surface and then convert the surface with the cylinders to a STL file
%% Generate the cylinders
for i=1:100
a17(i,1) = 11/(a*c+4*c)*s(i)+3/(a*c+2*c);
a17(i,2) = 5*s(i)/(a*c)-1/(a*c+2*c);
a17(i,3) = s(i);
a18(i,1) = (3/2*a*c+3*c)*s(i)-(a*c/2+2*c);
a18(i,2) = -s(i)/(a*c-2*c)+a*c/2;
a18(i,3) = s(i);
end
R = 0.05;
N = 500;
[a17X,a17Y,a17Z]=cylinder2P(R,N,[a17(1,1) a17(1,2) a17(1,3)],[a17(100,1) a17(100,2) a17(100,3)]);
[a18X,a18Y,a18Z]=cylinder2P(R,N,[a18(1,1) a18(1,2) a18(1,3)],[a18(100,1) a18(100,2) a18(100,3)]);

Related

How do I label lines in a MatLab plot?

What my plot looks like
What the plot should look like
The code is working like it should but im trying to get the labels to show up on each line from (1-8). Just like the picture above.
I have read a bunch of posts and tried to search Matlab but i havent been able to figure it out.
clc;clear;close all;
V_inf = 20; % freestream velocity
R = 1; % cylinder radius
n = 8; % number of panels
d_theta = 2*pi/n; % resolution of angles
alpha = 0; % angle of attack
theta = pi+pi/n:-d_theta:-pi+pi/n; % angles of boundary points of panels
X = R*cos(theta); % X coordinates of boundary points of panels
Y = R*sin(theta); % Y coordinates of boundary points of panels
Phi = zeros(n,1); % angle from Vinf to bottom of panel
beta = zeros(n,1); % angle from Vinf to outward normal of panel
conX = zeros(n,1); % X coordinates of control points
conY = zeros(n,1); % Y coordinates of control points
S = zeros(n,1); % panel length
for i = 1:n
Phi(i) = -alpha + atan2((Y(i+1)-Y(i)),(X(i+1)-X(i)));
beta(i) = Phi(i)+pi/2;
if beta(i)>2*pi, beta(i)=beta(i)-2*pi;
elseif beta(i)<0, beta(i)=beta(i)+2*pi; end
conX(i) = (X(i+1)+X(i))/2;
conY(i) = (Y(i+1)+Y(i))/2;
S(i) = sqrt((X(i+1)-X(i))^2 + (Y(i+1)-Y(i))^2);
end
close all
plot(R*cos(0:0.01:2*pi),R*sin(0:0.01:2*pi),'b', X,Y,'r',conX,conY,'g^');
axis equal; legend('Exact shape','Panel approximation','Control points')
xlabel('x, m'); ylabel('y, m'); title('Fig. 1 Panel layout (n = 8, R = 1m)');
Possibly plotting the labels along the points of a circle using the text() function may suffice. There's some shifting of points and flipping that needs to be done to get the order you wish but otherwise it's just 8 points taken along a circle that is smaller in diameter in comparison to the octagon. An alternative would be using the green triangles as reference instead but that involves more math. As long as your octagon is expected to be symmetrical vertically and horizontally this should work alright.
clc;clear;close all;
V_inf = 20; % freestream velocity
R = 1; % cylinder radius
n = 8; % number of panels
d_theta = 2*pi/n; % resolution of angles
alpha = 0; % angle of attack
theta = pi+pi/n:-d_theta:-pi+pi/n; % angles of boundary points of panels
X = R*cos(theta); % X coordinates of boundary points of panels
Y = R*sin(theta); % Y coordinates of boundary points of panels
Phi = zeros(n,1); % angle from Vinf to bottom of panel
beta = zeros(n,1); % angle from Vinf to outward normal of panel
conX = zeros(n,1); % X coordinates of control points
conY = zeros(n,1); % Y coordinates of control points
S = zeros(n,1); % panel length
for i = 1:n
Phi(i) = -alpha + atan2((Y(i+1)-Y(i)),(X(i+1)-X(i)));
beta(i) = Phi(i)+pi/2;
if beta(i)>2*pi, beta(i)=beta(i)-2*pi;
elseif beta(i)<0, beta(i)=beta(i)+2*pi; end
conX(i) = (X(i+1)+X(i))/2;
conY(i) = (Y(i+1)+Y(i))/2;
S(i) = sqrt((X(i+1)-X(i))^2 + (Y(i+1)-Y(i))^2);
end
close all
plot(R*cos(0:0.01:2*pi),R*sin(0:0.01:2*pi),'b', X,Y,'r',conX,conY,'g^');
axis equal; legend('Exact shape','Panel approximation','Control points')
xlabel('x, m'); ylabel('y, m'); title('Fig. 1 Panel layout (n = 8, R = 1m)');
%*************************************************************************%
%ADDING LABELS BY PLOTTING LABELS ALONG CIRCLE%
%*************************************************************************%
Radius = 0.8;
Number_Of_Data_Points = 9;
theta = linspace(0,2*pi,Number_Of_Data_Points);
X_Circle = Radius*cos(theta);
X_Circle = X_Circle(1:end-1);
Y_Circle = Radius*sin(theta);
Y_Circle = Y_Circle(1:end-1);
X_Circle = flip(circshift(X_Circle,3));
Y_Circle = flip(circshift(Y_Circle,3));
for Point_Index = 1: numel(conX)
X_Displacement = X_Circle(Point_Index);
Y_Displacement = Y_Circle(Point_Index);
text(X_Displacement,Y_Displacement,num2str(Point_Index),'HorizontalAlignment','center','fontsize',20);
end
To Plot on Control Points:
%*************************************************************************%
%ADDING LABELS BY PLOTTING LABELS ALONG CONTROL POINTS%
%*************************************************************************%
for Point_Index = 1: numel(conX)
text(conX(Point_Index),conY(Point_Index),num2str(Point_Index),'HorizontalAlignment','center','fontsize',20);
end

How do I plot a curved surface in matlab?

I am trying to make a cut out of a pipe and I want to make a curved surface to represent the outside of the pipe. However when I plot the surface I only get the diagonal of the surface instead of the surface itself. How can I fix this?
MWE:
r = 0:0.1:3;
z = 0:0.1:10;
[rr, zz] = meshgrid(r,z);
% set cut planes angles
theta1 = 0;
theta2 = pi*135/180;
nt = 101; % angle resolution
figure(1);
clf;
t3 = linspace(theta1, (theta2 - 2*pi), nt);
[rr3, tt3] = meshgrid(r,t3);
% Create curved surface
xx5 = r(end) * cos(tt3);
yy5 = r(end) * sin(tt3);
h5 = surface(xx5, yy5,zz)
The mesh-grid you created is based on theta and the radius. However, the radius is constant for the outside of the pipe so instead it should be based on theta and z since those are the two independent variables defining the grid. Based on this reasoning I believe the following is what you're after.
r = 0:0.1:3;
z = 0:0.1:10;
% set cut planes angles
theta1 = 0;
theta2 = pi*135/180;
nt = 101; % angle resolution
figure(1);
clf;
% create a grid over theta and z
t3 = linspace(theta1, (theta2 - 2*pi), nt);
[tt3, zz3] = meshgrid(t3, z);
% convert from cylindical to Cartesian coordinates
xx5 = r(end) * cos(tt3);
yy5 = r(end) * sin(tt3);
% plot surface
h5 = surface(xx5, yy5, zz3, 'EdgeColor', 'none');
% extra stuff to make plot prettier
axis vis3d
axis equal
view(3)
camzoom(0.7);
Try with surf with surf(xx5, yy5, zz). Is this what you are looking for?

Plotting spheres around given coordinates in 3D in Matlab

I am working on model of an object sliding on some rough surface consisting of spheres with a small random variance in position. In the graphics I want the spheres to be of a given radius, however when using scatter3 this wont work, the sizes of the circles change when I zoom in or out. I could easily solve this in 2D by using "rectangle"-function instead but for 3D this doesn't work.
Is there a better function for plotting spheres around points?
I have read this https://se.mathworks.com/matlabcentral/answers/101738-how-do-i-specify-the-size-of-the-markers-created-by-the-scatter-plot-in-units-proportional-to-the-da. But it either doesn't work for scatter3 or I do it wrong.
Sizes change when zooming in.
fig = figure(1);
hold on
daspect([1,1,1]);
surface.xnum = 16;
surface.znum = 16;
surface.r = 1;
circlenumber = 0;
for n = 1:surface.xnum
for m = 1:surface.znum
circlenumber = circlenumber + 1;
surface.circlecentre(circlenumber,:) = [n + 0.1*surface.r*randn , 0, m + 0.1*surface.r*randn ];
plt.surface = scatter3(surface.circlecentre(circlenumber, 1),surface.circlecentre(circlenumber, 2),surface.circlecentre(circlenumber, 3), 850*surface.r,'filled','r','MarkerEdgeColor','k');
end
end
Relevant part of the code. Setting coordinates to center of the spheres and plotting spheres around them.
This was the solution i found by the hint from Ander Biguri. I used surf to plot spheres instead of scatter3.
fig = figure(1);
hold on
daspect([1,1,1]);
colormap summer
shading interp % removes the lines in surfplot
surface.xnum = 8;
surface.znum = 8;
surface.r = 0.75;
circlenumber = 0;
for m = 1:surface.xnum
for n = 1:surface.znum
circlenumber = circlenumber + 1;
surface.circlecentre(circlenumber,:) = [m + 0.1*surface.r*randn ,0 , n + 0.1*surface.r*randn ];
[x,y,z] = sphere; surf(x*surface.r+m*2*surface.r+0.1*surface.r*randn, y*surface.r, z*surface.r+n*2*surface.r+0.1*surface.r*randn,'Edgecolor','none');
end
end

How to plot points on several concentric circles in sequence?

I want to plot several points on several concentric circles just like this:
The number of points on different circles are same and to be determined. The difference of radius are same.
But I face the problem that if I want to use the for function, I define i = 1:total number of points. I don't know how to select correct angle value. Can anyone help me?
Here is the code that I wrote:
R_steplength = 1; %difference of radius
angle_point = 20; %total number of points on one circle
max_R = 4; %outer radius of circle
central_x = 1; % origin of concentric circle
central_y = 1;
total_circle_points = (max_R/R_steplength) * angle_point; %calculate total
points
fin_x= zeros(1, total_circle_points); %define output points position
fin_y = zeros(1, total_circle_points);
for i = 1:total_circle_points
for j = 1:angle_point
if rem(i+1, 20)~= 1
k = floor(i/20);
angles = linspace(0,2*pi,angle_point);
fin_x(i) = R_steplength*(k+1)*cos(angles(j))+central_x;
fin_y(i)= R_steplength*(k+1)*sin(angles(j))+central_y;
else
fin_x(i) = central_x + R_steplength*(k+2);
fin_y(i) = central_y + R_steplength*(k+2);
end
plot(fin_x(:),fin_y(:),'ro')
end
end
You can use polarplot for that:
ax = polaraxes; % create polar axes
% calculate all points locations:
[angles,rad] = meshgrid(0:angle_point:360,1:R_steplength:max_R);
polarplot(ax,deg2rad(angles),rad,'r.') % plot all the points
ax.GridColor = 'k'; % set grid color to black
ax.GridAlpha = 1;
ax.ThetaAxis.TickValues = 10:20:360; % set radius grid between the points
ax.RAxis.TickValues = 1.5:R_steplength:(max_R+0.5); % set circles between the points
ax.RAxis.Limits = [0 max_R+0.5]; % show the outer circle
Here I use the axes grid to draw the circles. If this is not needed you can just write:
[angles,rad] = meshgrid(0:angle_point:360,1:R_steplength:max_R);
polarplot(ax,deg2rad(angles),rad,'r.') % plot all the points

How create asimetric cone in matlab?

I need to create a surface generate by rotation on y axis of shape formed by two curves. I already have the equation for this two curves. Here is the shape
I already create the surface but for simetric cone formed by one of this curve, here is my script:
h=20;
rw=1;
hw=5;
L=25;
r=linspace(rw,L,50);
theta=linspace(0,2*pi,25);
[r,theta] = meshgrid(r,theta);
xx=r.*cos(theta);
yy=r.*sin(theta);
z=sqrt(log(r/rw)*(h^2-hw^2)/log(L/rw)+hw^2); %Fuction of curve
surf(xx,yy,z)
Here is my result
In the other curve is the same fuction (z) but change r,h,L
Thanks for your help,
you should use parameter grids instead of scalars:
% parameters
h=[20 25];
rw=1;
hw=5;
L=[25 30];
nr = 50;
nt = 25;
theta=linspace(0,2*pi,nt);
rmax = linspace(L(1),L(2),floor((nt+1)/2));
% generating **grids** for all parameters
l = [linspace(L(1),L(2),floor((nt+1)/2)) linspace(L(2),L(1),floor((nt)/2))];
hh = [linspace(h(1),h(2),floor((nt+1)/2)) linspace(h(2),h(1),floor((nt)/2))];
[~,thetaGrid] = meshgrid(1:nr,theta);
[~,lGrid] = meshgrid(1:nr,l);
[~,hGrid] = meshgrid(1:nr,hh);
rGrid = zeros(size(thetaGrid));
for ii = 1:floor((nt+1)/2)
rGrid(ii,:) = linspace(rw,rmax(ii),nr);
end
rGrid(ceil((nt+1)/2):end,:) = rGrid(floor((nt+1)/2):-1:1,:);
% set coordinate grids
xx=rGrid.*cos(thetaGrid);
yy=rGrid.*sin(thetaGrid);
z=sqrt(log(rGrid./rw).*(hGrid.^2-hw^2)./log(lGrid./rw)+hw.^2); %Fuction of curve
% plot
surf(xx,yy,z)