Matlab:逼真地模拟弹跳球(模拟地球的条件)

时间:2015-05-30 11:51:13

标签: matlab animation

我跟着this tutorial关于如何使用matlab动画弹跳球。一切正常,但不符合我的要求,主要是:

  

对于接近地球的重力加速度(~10 m / s ^ 2),球应该看起来像真球在地球上弹跳(最大速度,加速度......)。

但是我得到的是球慢慢上下移动,加速度可见,但与真实的球(教程结尾的youtube视频)相比肯定太慢了。

来自教程:

下一步是添加初始水平速度,但仍会出现同样的问题。

如何让这个动画更快 - >更现实吗?

@EDIT 我必须补充说,为地球正确地获取它是第一步。根据设计,这应该是任何给定的重力加速度和初始速度的适当近似值。

1 个答案:

答案 0 :(得分:0)

球似乎移动缓慢的原因之一可能是脚本中设置的初始位置:

initpos=50;     %Ball's initial vertical position

初始位置的度量单位必须被视为米,以符合g的定义

gravity=10;     %Gravity's acceleration

如果你减少球的初始高度,你可以看到球“看起来”移动得更快,但只是因为figure的大小保持不变,它的“空间”更小。

我修改了你的脚本,删除(注释)了一些可以减慢执行速度的代码行,并且我已经将初始位置从50更改为5(为此,我也不得不减少球的大小。)

%--------------------------------------------------------------------------
%Script01b - Bouncing Ball
%Creating Simple Animation in MATLAB
%MATLAB Undercover
%zerocrossraptor.wordpress.com
%--------------------------------------------------------------------------
%This script m-file creates an endless animation of bouncing balls.
%--------------------------------------------------------------------------

%CodeStart-----------------------------------------------------------------
%Resetting MATLAB environment
    close all;
    clear all;
    clc;
%Declaring ball's initial condition
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modified ball's initial position
% % %     initpos=50;     %Ball's initial vertical position
    initpos=5;     %Ball's initial vertical position
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
    initvel=0;      %Ball's initial vertical velocity
%Declaring environmental variable
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modified ball's radius
% % %     r_ball=5;       %Ball's radius
    r_ball=0.25;       %Ball's radius
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
    gravity=10;     %Gravity's acceleration
    c_bounce=1;     %Bouncing's coefficient of elasticity
%Declaring animation timestep
    dt=0.0125;      %Animation timestep
%Drawing first frame
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Removed (commented) the first plot
% % %     ball=rectangle('Position',[-r_ball,initpos,r_ball,r_ball],...
% % %               'Curvature',[1,1],...
% % %               'FaceColor','b');
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
    line([-5*r_ball,5*r_ball],...
         [0,0]);
%Executing animation
    pos=initpos-r_ball;             %Ball's current vertical position
    vel=initvel;                    %Ball's current vertical velocity
% % % % % % % % % % % % % % % % % % % % 
    axis([-5*r_ball,5*r_ball,0,initpos+2*r_ball]);
        axis('equal');
        axis('off');
% % % % % % % % % % % % % % % % % % % % % % 
    while 1
        %Declaring time counter
        t_loopstart=tic();
        %Updating ball's condition
        pos=pos+(vel*dt);           %Ball's current vertical position
        vel=vel-(gravity*dt);       %Ball's current vertical velocity
        %Adjusting ball's velocity if the ball is hitting the floow
        if pos<0
            vel=-vel*c_bounce;      %Balls' current vertical velocity
        end
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Removed (commented) clearing figure, replaced by "delete" of ball at the
% end of the loop (delete of the "rectangle handle")
%Clearing figure
% % %         clf;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
        %Drawing frame
        ball=rectangle('Position',[-r_ball,pos,r_ball,r_ball],...
                  'Curvature',[1,1],...
                  'FaceColor','b');
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Removed unnecessary plot instructions

% % %         line([-5*r_ball,5*r_ball],...
% % %              [0,0]);
        %Preserving axes
% % %         axis([-5*r_ball,5*r_ball,0,initpos+2*r_ball]);
% % %         axis('equal');
% % %         axis('off');
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
        %Pausing animation
        el_time=toc(t_loopstart);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Removed (commented) display of Elapsed time (to speed up the script)
% % %         disp(['Elapse time : ',num2str(el_time),' seconds']);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
        pause(dt-el_time);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Inserted "delete(ball)" to remove the ball before next plot; this replace
% the "clf" instruction commneted above
        delete(ball)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
    end
%CodeEnd-------------------------------------------------------------------

希望这有帮助。