plot many sphere with variant radii
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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?

Antworten (1)
Aashray
am 24 Apr. 2025
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:
0 Kommentare
Siehe auch
Kategorien
Mehr zu Surface and Mesh Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!