Plotting a 3D matrix on a normal 2D plot

1 次查看(过去 30 天)
I have a 3D matrix that contains the positions of different objects over time.
There are
  • four rows, one for each object
  • two colums, giving the xy position of each object
  • 5000+ pages, that tracks how these values change over time.
Now I would like to draw these objects on a plot as they change over time. So four points for every page, preferably connected by lines. So I only need a 2D surface.
This is the code that generates the 3D matrix:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% PLANETARY MOTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
A CLEAN SLATE
clear
close all
%%%%%%%%%%%%%%%%%%%%%%%%%%%
TITLE
disp('PLANETARY MOTION')
disp(' ')
%%%%%%%%%%%%%%%%%%%%%%%%%%%
SETUP
%%Based on F = G*M*m/r^2
% Program constants
delta_t = 0.1;
t = 1;
G = 1;
% In lieu of user input
nbodies = 4;
%%INITIAL CONDITIONS %%
mass = zeros(1,nbodies); % preallocation of vectors
position = zeros(nbodies,2);
distance = zeros(nbodies,nbodies);
alpha = zeros(nbodies,nbodies); % xy angle, as seen from body n
velocity = zeros(nbodies,nbodies);
acceleration = zeros(nbodies,nbodies);
% In lieu of user input
position = [0 0;100 30;500 150;840 325];
mass = [3 0.01 0.02 0.03];
%%START LOOP %%
for t = [1:5000]
for n = [1:nbodies]
for m = [1:nbodies]
if n == m || distance(m,n) > 0 % this hopefully speeds up computation
distance(n,m) = distance(m,n);
alpha(n,m) = pi-alpha(m,n);
else
distance(n,m) = sqrt( (position(n,1,t)-position(m,1,t))^2 + (position(n,2,t)-position(m,2,t))^2);
alpha(n,m) = atan( (position(n,1,t)-position(m,1,t)) / (position(n,2,t)-position(m,2,t)) );
end
if n ~= m % only compute necessary elements
acceleration(n,m) = G*mass(m)/distance(n,m)^2;
velocity(n,m) = velocity(n,m) + acceleration(n,m)*delta_t;
position(n,1,t+1) = position(n,1,t) + cos(alpha(n,m))*velocity(n,m)*delta_t;
position(n,2,t+1) = position(n,2,t) + sin(alpha(n,m))*velocity(n,m)*delta_t;
else
end
end
end
distance = zeros(nbodies,nbodies); % clear the distance matrix for next iteration
end
disp('initial positions = ')
disp(position(:,:,1))
disp('final positions = ')
disp(position(:,:,t))
%%%%TODO
%%Fix acceleration (1/r per element)
%%Make delta_t depend on acceleration. High acc => small delta_t
%%Only capture certain timesteps for the position vector, not every t
I appreciate any input. Thank you.

采纳的回答

Mike Garrity
Mike Garrity 2015-9-29
The animatedline function is usually a good tool for this type of problem. Here's an animation of one of your objects:
h = animatedline;
xlim([min(position(1,1,:)), max(position(1,1,:))])
ylim([min(position(1,2,:)), max(position(1,2,:))])
for i=1:size(position,3)
addpoints(h,position(1,1,i),position(1,2,i))
drawnow
end
As you can see, it is basically moving in a straight line, so it's not terribly interesting as an animation.
The problem is that your four objects are quite a ways away from each other relative to the amount of motion each one has. This means that if we animate all of them together, we basically don't see anything:
h = [animatedline, animatedline, animatedline, animatedline];
for i=1:size(position,3)
for j=1:4
addpoints(h(j),position(j,1,i),position(j,2,i))
end
drawnow
end
Another option would be to use markers instead of animatedline.
h = [line('Marker','o'), line('Marker','x'), line('Marker','s'), line('Marker','p')];
for i=1:size(position,3)
for j=1:4
h(j).XData = position(j,1,i);
h(j).YData = position(j,2,i);
end
drawnow
end
If we do that, we can at least see where the objects are. But you really can't see any relative motion. That's because the motion is quite small relative to the overall size of the scene.
Hopefully there are some ideas there that can get you started.
  2 个评论
mirshafie
mirshafie 2015-9-29
编辑:mirshafie 2015-9-29
Thank you so much Mike, this helps a lot.
I'm going to use a lot more data once the program is finished, but this way of plotting will do the job and look beautiful. (I will use the tic/toc trick described here to get a suitable animation speed.)
Mike Garrity
Mike Garrity 2015-9-29
Another thing to look at when you're animating while simulating is the new "limitrate" option to drawnow. I talked about that a bit in this post about simulating reaction diffusion systems .
BTW, I'd be interested in seeing what you've built when you're done. It looks like it'll be pretty interesting.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by