plot many sphere with variant radii

10 次查看(过去 30 天)
amir ansari
amir ansari 2020-11-11
回答: Aashray 2025-4-24
hi to all
i have spheres centers and radii and i just want to plot n spheres with variant radii in a cube ,like below figure.how can i do that?
  1 个评论
amir ansari
amir ansari 2020-11-11
i forgot to say that the links are not important and just sphere's need

请先登录,再进行评论。

回答(1 个)

Aashray
Aashray 2025-4-24
Hello Amir
MATLAB’s built-in surf function can be used to plot surfaces in three dimensions. I have included an example code below for better understanding:
n = 35; % Number of spheres
cubeSize = 10; % Cube dimensions
% Using placeholder values for radii and centers
radii = 0.5 + rand(n, 1)*0.4;
centers = radii + (cubeSize - 2 * radii) .* rand(n, 3);
% These values can be replaced by actual values as below:
% radii = [1.2; 0.8; 1.5; ...];
% centers = [2, 3, 4;
% 5, 5, 5;
% 1, 1, 1;
% ...];
% Plot setup
figure;
hold on;
axis equal;
xlim([0 cubeSize]);
ylim([0 cubeSize]);
zlim([0 cubeSize]);
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Spheres with Given Centers and Radii');
% Plot each sphere using the given center and radius
for i = 1:n
r = radii(i);
center = centers(i, :);
[X, Y, Z] = sphere(20); % Resolution of sphere
X = r * X + center(1);
Y = r * Y + center(2);
Z = r * Z + center(3);
% Plot the sphere
surf(X, Y, Z, 'EdgeColor', 'none', 'FaceAlpha', 0.9);
end
view(3);
grid on;
You can also adjust the transparency of the spheres by modifying the FaceAlpha parameter, set it from 0 (fully transparent) to 1 (fully opaque).
Below are some example images of the plot generated using the above code:
For more details on the functions used, you can refer to the MATLAB documentation:
  1. https://www.mathworks.com/help/matlab/ref/figure.html
  2. https://www.mathworks.com/help/matlab/ref/sphere.html
  3. https://www.mathworks.com/help/matlab/ref/surf.html

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by