How to create 3d solid cylinder in matlab with help of meshgrid with each of its coordinates fetchable
64 views (last 30 days)
Show older comments
% Define the parameters of the cylinder
radius = 1; % Radius of the cylinder
height = 2; % Height of the cylinder
num_points = 100; % Number of points along the circumference
% Generate the grid for the x, y, and z coordinates
theta = linspace(0, 2*pi, num_points); % Angles along the circumference
z = linspace(0, height, num_points); % Heights along the axis
theta_indeg = rad2deg(theta); %converting radians to degree
A=ones(100) %making a unit 100x100 matrix
% converting to cartesian coordinates
x= A.*cos(theta_grid)
y= A.*sin(theta_grid)
surf(x,y,z_grid)
0 Comments
Answers (1)
Naman
on 4 Jul 2023
Hi Sakshi,
There are some changes required in your code to create a 3d solid cylinder in MATLAB with the help of meshgrid and with each of its coordinates fetchable.
Below is the updated code :
% Define the parameters of the cylinder
radius = 1; % Radius of the cylinder
height = 2; % Height of the cylinder
num_points = 100; % Number of points along the circumference
% Generate the grid for the x, y, and z coordinates
theta = linspace(0, 2*pi, num_points); % Angles along the circumference
z = linspace(0, height, num_points); % Heights along the axis
% Convert to cartesian coordinates
[theta_grid, z_grid] = meshgrid(theta, z);
x = radius * cos(theta_grid);
y = radius * sin(theta_grid);
% Visualize the cylinder
surf(x, y, z_grid);
axis equal;
xlabel('X');
ylabel('Y');
zlabel('Z');
title('3D Solid Cylinder');
% Store the coordinates in a structure for easy access
coordinates.x = x;
coordinates.y = y;
coordinates.z = z_grid;
% Fetch a specific coordinate
x_value = coordinates.x(10, 20); % Fetch x-coordinate at index (10, 20)
y_value = coordinates.y(10, 20); % Fetch y-coordinate at index (10, 20)
z_value = coordinates.z(10, 20); % Fetch z-coordinate at index (10, 20)
% Display the fetched coordinates
disp(['Fetched Coordinates: (', num2str(x_value), ', ', num2str(y_value), ', ', num2str(z_value), ')']);
I have taken indices (10,20) for example. You can modify the indices (10, 20) to fetch the desired coordinate.
Hope it helps!
0 Comments
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!