%======================================
%===== The Bouncing Ball Function =====
%======================================

clear
clc

Amplitude = 90;
Frequency = 0.5; %Oscillations Per Second
Decay = 0.5; %Decay = 0 For No Decay
Phase = 0;
Duration = 10;
SamplingRate = 40;
AngularFrequency = 2*pi*Frequency;
Time = 0:1/SamplingRate:Duration;
PeriodCounter = 0;

for i=1:length(Time)
display(i);
         
    BouncingBallMovement(i) = abs(cos(AngularFrequency*Time(i) + Phase));
    BouncingBall = Amplitude*BouncingBallMovement./exp(Decay*Time(i));

    if (rem(i,2) == 0)
        PeriodCounter = PeriodCounter + 1;      
    end
     
    figure(1);
    axis([-4 20 -3 (Amplitude+10)]);
    title('The Bouncing Ball Function');
    xlabel('Time (Sec)');
    ylabel('Amplitude');
    grid on

    circle = rsmak('circle');
    BallShape = fncmb((circle),[2 0;0 2]);
    Radious = 1/sqrt(2);
    Ball = fncmb(fncmb(BallShape,[-Radious Radious;Radious Radious]),[Time(i);BouncingBall(i)]);
    hold on
    
    if (PeriodCounter == 1)        
        fnplt(Ball,'r');
        PeriodCounter = 0;
    else
        fnplt(Ball);
    end
    
    hold off
end