%=================================================
%===== The Decay/Non-Decay Pendulum Function =====
%=================================================

clear
clc

Amplitude = -80;
Frequency = 1.0; %Oscillations Per Second
Decay = 0.8; %Decay = 0 For A Non-Decaying Pendulum
Phase = 0;
Duration = 10;
SamplingRate = 40;
AngularFrequency = 2*pi*Frequency;
Time = 0:1/SamplingRate:Duration;
PeriodCounter = 0;

for i=1:length(Time)
display(i);
         
    Pendulum(i) = Amplitude * sin(AngularFrequency*Time(i) + Phase)./exp(Decay*Time(i));

    if (rem(i,2) == 0)
        PeriodCounter = PeriodCounter + 1;      
    end
     
    figure(1);
    axis([(Amplitude-10) (-Amplitude+10) -10 -Amplitude]);
    title('The "Decay/Non-Decay" Pendulum Function');
    xlabel('Amplitude');
    ylabel('Time (Sec)');
    grid on

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