plot many sphere with variant radii

조회 수: 8 (최근 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