How to place random CNT’s in a box domain with random curves and orientation ?
Hi all, what i want is a code that can actually place random carbon nanotubes in a cube domain so that i can use it in comsol livelink MATLAB bellow is a code that puts spheres which i found in this forum i need it based on that.. :
function P = GetRandomSpheres(nWant, Width, Radius)
% INPUT:
% nWant: Number of spheres
% Width: Dimension of 3d box as [1 x 3] double vector
% Radius: Radius of spheres
% OUTPUT:
% P: [nWant x 3] matrix, centers
P = zeros(nWant, 3);
R2 = (2 * Radius) ^ 2; % Squared once instead of SQRT each time
W = Width – 2 * Radius; % Avoid interesction with borders
iLoop = 1; % Security break to avoid infinite loop
nValid = 0;
while nValid < nWant && iLoop < 1e12
newP = rand(1, 3) .* W + Radius;
% Auto-expanding, need Matlab >= R2016b. For earlier versions:
% Dist2 = sum(bsxfun(@minus, P(1:nValid, :), newP) .^ 2, 2);
Dist2 = sum((P(1:nValid, 🙂 – newP) .^ 2, 2);
if all(Dist2 > R2)
% Success: The new point does not touch existing sheres:
nValid = nValid + 1; % Append this point
P(nValid, 🙂 = newP;
end
iLoop = iLoop + 1;
end
% Stop if too few values have been found:
if nValid < nWant
error(‘Cannot find wanted number of points in %d iterations.’, iLoop)
end
end
n = 100;
R = 5;
P = GetRandomSpheres(n, [100, 100, 100], R);
figure
axes(‘NextPlot’, ‘add’, …
‘XLim’, [0, 100], ‘YLim’, [0, 100], ‘ZLim’, [0, 100]);
view(3);
[X, Y, Z] = sphere();
for k = 1:n
surf(X * R + P(k, 1), Y * R + P(k, 2), Z * R + P(k, 3));
endHi all, what i want is a code that can actually place random carbon nanotubes in a cube domain so that i can use it in comsol livelink MATLAB bellow is a code that puts spheres which i found in this forum i need it based on that.. :
function P = GetRandomSpheres(nWant, Width, Radius)
% INPUT:
% nWant: Number of spheres
% Width: Dimension of 3d box as [1 x 3] double vector
% Radius: Radius of spheres
% OUTPUT:
% P: [nWant x 3] matrix, centers
P = zeros(nWant, 3);
R2 = (2 * Radius) ^ 2; % Squared once instead of SQRT each time
W = Width – 2 * Radius; % Avoid interesction with borders
iLoop = 1; % Security break to avoid infinite loop
nValid = 0;
while nValid < nWant && iLoop < 1e12
newP = rand(1, 3) .* W + Radius;
% Auto-expanding, need Matlab >= R2016b. For earlier versions:
% Dist2 = sum(bsxfun(@minus, P(1:nValid, :), newP) .^ 2, 2);
Dist2 = sum((P(1:nValid, 🙂 – newP) .^ 2, 2);
if all(Dist2 > R2)
% Success: The new point does not touch existing sheres:
nValid = nValid + 1; % Append this point
P(nValid, 🙂 = newP;
end
iLoop = iLoop + 1;
end
% Stop if too few values have been found:
if nValid < nWant
error(‘Cannot find wanted number of points in %d iterations.’, iLoop)
end
end
n = 100;
R = 5;
P = GetRandomSpheres(n, [100, 100, 100], R);
figure
axes(‘NextPlot’, ‘add’, …
‘XLim’, [0, 100], ‘YLim’, [0, 100], ‘ZLim’, [0, 100]);
view(3);
[X, Y, Z] = sphere();
for k = 1:n
surf(X * R + P(k, 1), Y * R + P(k, 2), Z * R + P(k, 3));
end Hi all, what i want is a code that can actually place random carbon nanotubes in a cube domain so that i can use it in comsol livelink MATLAB bellow is a code that puts spheres which i found in this forum i need it based on that.. :
function P = GetRandomSpheres(nWant, Width, Radius)
% INPUT:
% nWant: Number of spheres
% Width: Dimension of 3d box as [1 x 3] double vector
% Radius: Radius of spheres
% OUTPUT:
% P: [nWant x 3] matrix, centers
P = zeros(nWant, 3);
R2 = (2 * Radius) ^ 2; % Squared once instead of SQRT each time
W = Width – 2 * Radius; % Avoid interesction with borders
iLoop = 1; % Security break to avoid infinite loop
nValid = 0;
while nValid < nWant && iLoop < 1e12
newP = rand(1, 3) .* W + Radius;
% Auto-expanding, need Matlab >= R2016b. For earlier versions:
% Dist2 = sum(bsxfun(@minus, P(1:nValid, :), newP) .^ 2, 2);
Dist2 = sum((P(1:nValid, 🙂 – newP) .^ 2, 2);
if all(Dist2 > R2)
% Success: The new point does not touch existing sheres:
nValid = nValid + 1; % Append this point
P(nValid, 🙂 = newP;
end
iLoop = iLoop + 1;
end
% Stop if too few values have been found:
if nValid < nWant
error(‘Cannot find wanted number of points in %d iterations.’, iLoop)
end
end
n = 100;
R = 5;
P = GetRandomSpheres(n, [100, 100, 100], R);
figure
axes(‘NextPlot’, ‘add’, …
‘XLim’, [0, 100], ‘YLim’, [0, 100], ‘ZLim’, [0, 100]);
view(3);
[X, Y, Z] = sphere();
for k = 1:n
surf(X * R + P(k, 1), Y * R + P(k, 2), Z * R + P(k, 3));
end matlab, random, java, comsol, geometry, matlab code, programming MATLAB Answers — New Questions