Plotting a 3D matrix on a normal 2D plot
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Animation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


