How can I plot x,y,z position of particles over time from a multidimensional array?
23 views (last 30 days)
Show older comments
Michael Rainville
on 4 Mar 2021
Edited: Cris LaPierre
on 8 Mar 2021
Hi everyone,
I'm trying to simulate the positions of a number of molecules in free space over time, occasionally being hit by helium atoms (random velocity) and photons (uniform velocity). I have it set up to work through a number of steps, each time changing the velocity of the particular molecule based on a helium kick and then a photon kick. It then creates a position array, of dimensions (#molecules, 3, #steps). Those mechanics all seem to work. My problem comes when trying to plot the position over time. I'd like to plot each molecule's [x,y,z] position coordinate across the total number of steps, but the code below returns some sort of strange triangular graph in three dimensions that I'm not sure how to interpret. When I had this code set up for one dimension, it worked just fine.
So is there a way to plot an individual row of each "page" of the 3d array across the total number of steps?
numParticles = 10;
numSteps = 100;
HeliumThreshold = 0.1;
PhotonThreshold = .49999;
PhotonKick = [.0005193164, 0, 0];
Sigmav = .000005193164*eye(3);
v = zeros(numParticles, 3, numSteps);
x = zeros(numParticles, 3, numSteps);
for stepNumber = 2 : numSteps
for p = 1 : numParticles
HeliumProb = rand;
PhotonProb = rand;
Deltav = [0, 0, 0; 0, 0, 0];
if HeliumProb > HeliumThreshold
HeliumKick = mvnrnd([0, 0, 0], Sigmav, 1);
Deltav(1, :) = HeliumKick;
else
Deltav = Deltav;
end
if PhotonProb > PhotonThreshold
Deltav(2, :) = HeliumKick + PhotonKick;
else
Deltav = Deltav;
end
v(p, :, stepNumber) = v(p, :, stepNumber - 1) + Deltav(2, :);
x(p, :, stepNumber) = x(p, :, stepNumber - 1) + v(p, :, stepNumber);
X=reshape(x,[],3);
plot3(X(:,1),X(:,2),X(:,3));
hold on
end
hold off
end
Also, here's a picture of the plot:
0 Comments
Accepted Answer
Cris LaPierre
on 4 Mar 2021
Edited: Cris LaPierre
on 8 Mar 2021
Based on my understanding, here's how I would approach this. I swapped your for loops so that it selects a particle, and then works through all the steps. At the conclusion of the inner loop, I can now plot the path of that particle. Rather than reshape, I use indexing to select the 'page' to plot, and use squeeze to remove the singleton dimension (otherwise you'll get an error when plotting).
Let me know if you have any questions.
numParticles = 10;
numSteps = 100;
HeliumThreshold = 0.1;
PhotonThreshold = .49999;
PhotonKick = [.0005193164, 0, 0];
Sigmav = .000005193164*eye(3);
v = zeros(numParticles, 3, numSteps);
x = zeros(numParticles, 3, numSteps);
for p = 1 : numParticles
for stepNumber = 2 : numSteps
HeliumProb = rand;
PhotonProb = rand;
Deltav = [0, 0, 0; 0, 0, 0];
if HeliumProb > HeliumThreshold
HeliumKick = mvnrnd([0, 0, 0], Sigmav, 1);
Deltav(1, :) = HeliumKick;
else
Deltav = Deltav;
end
if PhotonProb > PhotonThreshold
Deltav(2, :) = HeliumKick + PhotonKick;
else
Deltav = Deltav;
end
v(p, :, stepNumber) = v(p, :, stepNumber - 1) + Deltav(2, :);
x(p, :, stepNumber) = x(p, :, stepNumber - 1) + v(p, :, stepNumber);
end
X = squeeze(x(p,:,:));
plot3(X(1,:),X(2,:),X(3,:),'DisplayName',num2str(p));
hold on
end
hold off
legend
More Answers (0)
See Also
Categories
Find more on Analog Devices ADALM1000 Support from Data Acquisition Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!