使该点以一定速度移动

时间:2019-07-14 21:42:37

标签: matlab animation graph matlab-figure linear-algebra

总体思路

  • 一组点应以不同的速度开始移动,然后再以相同的速度移动。计算速度并将其存储在名为x的矩阵中,每列表示每个项目的速度。

即,这应该开始不均匀地移动,然后均匀地移动。

问题

  • 对象开始以相同速度/均匀移动,并且 物体没有按照保存的速度移动。
  • 它应该同时弹出2个窗口,一个用于动画,一个用于图形,但是一旦完成动画,图形的图形就会显示出来。 我不知道如何解决它(也许是一个单独的函数文件,例如调用了功能cons,但是我想避免文件过多)

代码

clc
clear
%%%Laplacian matrix
B=[1 1 0 0 0;-1 0 1 1 0;0 0 0 -1 0;0 -1 0 0 0;0 0 -1 0 1;0 0 0 0 -1];
L=B*B';


%% Eigenvalues and kernel
lambda = eig(L) %% eigenvalues
Lr = rref(L)  %% Gauss reduction

%%  Numerical solution for the diferential eq. % % % %

t = linspace(0,20,1000);    % simulation time  0-20s 
x0 = [1;2;3;4;5;6] % initial speeds
%x0 = [1.5;2;10;2.5;6;8]; 
[t,x] = ode45(@cons,t,x0);   % ODE45 in function cons
y = [0;1;2;3;4;5];
px= [0;0;0;0;0;0];
v=1:size(x,1)

%Video = VideoWriter('file');
%myVideo.FrameRate = 10;
%open(Video)
figure(1)
for time=1:size(t)
    for k=1:length(x0)% quantity of objects
        for j=1:size(x,2) 
            x(j) = px(j) + v(time)*time;
            x0(k)=x(k);
            plot(x(j), y(k),'r*');
            hold on
        end
    end    
  hold off
  axis([-1 1000 -1 7])
  drawnow();
  pause(0.1)
  %frame = getframe(gcf);
  %writeVideo(Video, frame);
end
%close(Video)

%% %Graphs
subplot(2,1,1)
plot(t,x,'linewidth',1.5) 
xlabel('time [s]') 
ylabel('speed [m/s]')% 
grid

subplot(2,1,2)
plot (real (lambda), imag (lambda),'bs','linewidth',1.5 ),grid 
axis ([-2 10 -2 2]) 
xlabel('Real axis') 
ylabel('I axis ')% 
legend('Eigenvalues')

这给出了下一个动画 enter image description here

它不会读取保存的值,只会从1到1000开始计数。

更新
我更改了这一行(如Patrick Happel所建议的那样)

 x(j) = px(j) + v(time)*time;

 x(j) = px(j) + v(j)*time;

v=1:size(x,1)

对于

v=x(1:end,:)

这确实会生成一个包含速度的新矩阵,但它会以相同的速度使这些点动画化

0 个答案:

没有答案