动画圆圈,基于Matlab中的ODE求解器输出

时间:2017-09-21 08:08:01

标签: matlab animation matlab-figure ode45

我是Matlab的新手,我正在尝试根据初始速度和位置两个直线移动动画。

我已经得到ode45求解器为我吐出速度和位移图(随着时间的推移),现在想要在15秒的时间间隔内对2D进行动画处理,根据ODE求解器的输出改变位置。 / p>

功能:

function rt = odes(t,x)

k = 0.9;     %Friction Konstant
m =1;        %Mass Konstant

rt(1) = (-k*x(1))/m ;   % Acceleration   
rt(2) = x(1);           % x-component Velocity
rt(3) = 0;              % z-component Velocity

rt = rt(:);  

实际程序代码:

cla
clc
clear all
close all

%Define constants and initial conditions

speedvar = 0.08 ; %Slows down animation
t = 0;
t_end = 15;
d = 1;
a = 1
radii = d/2;

v1_0 = 1;   %Initial Velocities
v2_0 = 2;

x1_0 = 0;   %Initial absolute positions x-axys
x2_0 = a+d;

z0 = 0; %Initial absolute position z-axys

IC1 = [v1_0;x1_0;z0];       %Initial Conditions
IC2 = [v2_0;x2_0;z0];
timerange = [t t_end ];     %Define Timerange

%Call ode45 for each ball and plot absolute x-position & Velocity over time
[t,x1] = ode45(@(t,x)odes(t,x),timerange,IC1);
figure 
plot (t,x1(:,1))
xlabel ('Time in s')
ylabel ('Velocity in m/s')
figure
plot (t,x1(:,2))
xlabel ('Time in s')
ylabel ('Position x-axys')

[t,x2] = ode45(@(t,x)odes(t,x),timerange,IC2);
figure 
plot (t,x2(:,1))
xlabel ('Time in s')
ylabel ('Velocity in m/s')
figure
plot (t,x2(:,2))
xlabel ('Time in s')
ylabel ('Position x-axys')

%// Animate 
for id = 1:length(t)
    cla
    axis square
    xlim([-1.1 10*x1(2)])
    ylim([-x1(2)*5 x1(2)*5])

    center1 = [x1(id,2) z0];
    center2 = [x2(id,2) z0];

    hc1 = viscircles(center1,radii);
    hc2 = viscircles(center2,radii);
    pause(speedvar) %slow down animation according to speedvar

end

然而,当我运行代码时,圆圈运动不会同时发生。似乎程序首先解决x2,为它设置动画然后重复x1的过程。我怎样才能让它们彼此平行解决/动画?

0 个答案:

没有答案