MATLAB: Cumulative radial distribution of particle centers

geometric centerradial distribution

Hi, I have just started working with Matlab. Need some help to initiate my following task.
I want to find out the cumulative radial distribution of particle centers by drawing spheres of radius 'R'. Initially the value of radius 'R' will be zero and it will increment with a value 'delta R' in each step. In each step the program should calculate the number of particle centers falling inside the sphere and it will terminate when all the given particle centers covered. In the end I want the code to plot a graph between 'reduced radial distance from centers vs Included particles'
I have particle centers in 3D and also their radius as they are itself considered as spheres. The starting point for the sphere of radius 'R' is the geometric center of the system which I also need to calculate.

Best Answer

See if this simulation does what you want:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Make 99,000 spheres in a cube 100 by 100 by 100
numParticles = 99000;
x = 100 * rand(1, numParticles);
y = 100 * rand(1, numParticles);
z = 100 * rand(1, numParticles);
% Find the center of all of these
xCentroid = mean(x);
yCentroid = mean(y);
zCentroid = mean(z);
% Find the distances of all the particles from the centroid
distances = sort(sqrt((x - xCentroid).^2+(y - yCentroid).^2+(z - zCentroid).^2));
% Get a distribution (count) of how many particles are at each distance
subplot(2, 1, 1);
histObj = histogram(distances)
grid on;
title('Histogram', 'FontSize', fontSize);
xlabel('Distance', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
% Compute the count within a radius by computing the CDF
cdf = cumsum(histObj.Values);
radius = histObj.BinEdges(1:end-1);
subplot(2, 1, 2);
plot(radius, cdf, 'b-', 'LineWidth', 2);
grid on;
title('CDF, Cumulative Distribution Function', 'FontSize', fontSize);
xlabel('Radius (Distance from centroid)', 'FontSize', fontSize);
ylabel('Number Within that Radius', 'FontSize', fontSize);