%================================
%===== The Stretch Function =====
%================================

clear
clc

Amplitude = 50;
Frequency = 1; %Oscillations Per Second
Decay = 2; %Decay = 0 For No Decay
Phase = 0;
Duration = 5;
SamplingRate = 100;
AngularFrequency = 2*pi*Frequency;
Time = 0:1/SamplingRate:Duration;
PeriodCounter = 0;
QuarterPeriodCounter = 1;

for i=1:length(Time)
display(i);

    StretchLevel(i) = Amplitude*sin(AngularFrequency*Time(i))./exp(Decay*Time(i));
  
    if (rem(i,round(SamplingRate/(Frequency*4))) == 0)
        QuarterPeriodCounter = QuarterPeriodCounter + 1;
        if (rem(QuarterPeriodCounter,2) == 0)
            PeriodCounter = 1;
        else
            PeriodCounter = 0;
        end
    end
    
    figure(1);
    axis([-Amplitude Amplitude -Amplitude Amplitude]);
    title('The Stretch Function');
    xlabel('Time (Sec)');
    ylabel('Amplitude');
    grid on

    circle = rsmak('circle');
    BallShape = fncmb((circle),[20-StretchLevel(i) 0;0 20-StretchLevel(i)]);
    Radious = 1/sqrt(2);
    Ball = fncmb(fncmb(BallShape,[-Radious Radious;Radious Radious]),[0;0]);
    hold on
    
    if (PeriodCounter == 1)        
        fnplt(Ball,'r');
    else
        fnplt(Ball);
    end
   
    for ii=1:10000000
    end

    hold off
end