Mack
Mack

Reputation: 675

Matlab: Initial Positions of Particles In Space

I have a cube whose side are length L. Inside of this cube is a smaller one, and is at the center. Evenly space along this inner cube, I would like to place particles. I have the matrix 'walker', which is a three dimensional matrix that stores the the position of the particle at regular time steps. Specifically, walker(i,j,k) is the information of the i-th particle, at the time step j, and the i-th particle's k-position, where k=1,2, and 3, which represent the x,y and z direction.

Here is my attempt at giving each particle an initial position:

L=16
W=5 %Number of walkers (particles)

%Initial Positions of Walkers (particles):
for i=1:W
    for j=1:3
    walker(i,1,j) = L/2 + i - 1;
    end
end

However, this just places each of the particles along a straight line. Could someone give me some suggestions as to how I might accomplish this?

EDIT:

x=linspace(0,L,W);
y=linspace(0,L,W);
z=linspace(0,L,W);

[X Y Z] = meshgrid(1:L,1:L,1:L);
xyz = [X(:),Y(:),Z(:)];
xyz = cat(1,xyz(:,[1 2 3]),xyz(:,[2 3 1]),xyz(:,[3 1 2]));
xyz = unique(xyz,'rows');

plot3(xyz(:,1),xyz(:,2),xyz(:,3),'.')

axis([-L L -L L -L L])

Upvotes: 0

Views: 240

Answers (1)

RDizzl3
RDizzl3

Reputation: 846

Try this code, this will place points along the inside of the cube evenly:

x = linspace(-L,L,5);
y = linspace(-L,L,5);
z = linspace(-L,L,5);

[X Y Z] = ndgrid(x,y,z);
xyz = [X(:),Y(:),Z(:)];
xyz = cat(1,xyz(:,[1 2 3]),xyz(:,[2 3 1]),xyz(:,[3 1 2]));
xyz = unique(xyz,'rows')
plot3(xyz(61:65,1),xyz(61:65,2),xyz(61:65,3),'.'), grid on
axis([-L L -L L -L L])

x, y and z will be 5 evenly spaced points from -L to L. This will give you 5^3 = 125 points but you can choose which points you want in pairs of 5 from the xyz matrix. The image for the plot is shown below:

enter image description here

This is 5 evenly spaced points vertically along the origin. The pairs in xyz start from 1:5, then 6:10, etc. You can choose the pairing that works best for you.

Upvotes: 1

Related Questions