Plotting outputs from for loops in a 3D space

3 visualizzazioni (ultimi 30 giorni)
So, I managed to get a simple function to work approximately the way that I wanted, which was to take a point in 3 dimensional space, described by a vectors (x,y,z) and ascribe forces to it, based on its proximity to other points ( in this case, fixed points) described by vectors. The code is as follows.
%particle vector [x,y,z] position
pm= [1;1;1];
%particle mass, arbitrary metric
mm1=2.5;
%inital velocity of particle
vm0=[.00001;-.0000003;.000006];
%initial acceleration of particle
am0=[-.00001;.00005;-.000002];
%two forces, respectively defined by proximity to two points [5,5,5] and [-3,-3,-3]
f1= .00000006*(pm - ([5;5;5])).^2;
f2= (.0000006*(1./(pm-[-3;-3;-3]).^2));
%acceleration of particle based on the previous two forces
am= am0+(f1+f2)./mm1;
%a for loop that iteratively calculates new positions based on the previous
%values, with respect to the time variable
for t=1:.01:100
pm= ((am*(t.^2))./2)+(vm0*t)+pm
plot(pm)
end
I want to display the progression of my particle, its trajectory, in a 3D plot. How would I go about this?
Also, I would like to be able to generalize this to 'n' particles, all starting at various positions (only interacting with [5,5,5] and [-3,-3,-3], not each other). Does anyone have any thoughts on how that could be achieved elegantly? Again, I'd like to be able to visualize the trajectories of these particles as well.
I appreciate any and all help.

Risposta accettata

John Chilleri
John Chilleri il 12 Gen 2017
Modificato: John Chilleri il 12 Gen 2017
Hello,
This should be somewhat like you wanted:
%particle vector [x,y,z] position
pm= [1;1;1];
%particle mass, arbitrary metric
mm1=2.5;
%inital velocity of particle
vm0=[.00001;-.0000003;.000006];
%initial acceleration of particle
am0=[-.00001;.00005;-.000002];
%two forces, respectively defined by proximity to two points [5,5,5] and [-3,-3,-3]
f1= .00000006*(pm - ([5;5;5])).^2;
f2= (.0000006*(1./(pm-[-3;-3;-3]).^2));
%acceleration of particle based on the previous two forces
am= am0+(f1+f2)./mm1;
%a for loop that iteratively calculates new positions based on the previous
%values, with respect to the time variable
figure(1)
hold on
%axis([-200 50 0 900 -30 10]) % uncomment this if you dont want the axes to ever change
for t=1:.01:100
pm= ((am*(t.^2))./2)+(vm0*t)+pm;
%if (mod(t,1) == 0) % uncomment this if you want to plot only 100 equally spaced points
plot3(pm(1),pm(2),pm(3),'*');
drawnow
%end % uncomment this if you want to plot only 100 equally spaced points
end
If you want other specifications, such as color, one developing line, size of points, or anything, I can help you with that as well. It's also important to note that this runs somewhat slowly because you're plotting 9901 times. There are ways to make this faster, such as plotting every 10 points which is also simple to implement.
You may also notice that I included three lines to plot only 100 points rather than all 9901 and an axis call that will maintain the view. I would suggest running it without these once as it does an intriguing curve at the beginning which is hard to see once it's fully plotted.
Hope this helps!
  4 Commenti
John Chilleri
John Chilleri il 13 Gen 2017
Modificato: John Chilleri il 13 Gen 2017
Hello,
I'm glad you found my answer helpful!
I think the simplest way to generate a whole set of positions without rewriting your existing code would be to define pm as a cell.
pm = cell(n,1);
for i = 1:n
pm{i} = %whatever;
% Run your code
end
As an important note, when you access pm in the code, you'll need to access the elements with:
pm{i}(element numbers)
% If pm{i} = [3;5;7];
% pm{i}(1) = 3
% pm{i}(2) = 5
% pm{i}(3) = 7
I hope this helps!
John Chilleri
John Chilleri il 13 Gen 2017
Also, checkout addpoints for improved animation!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by