%=================================
%===== The Curve Integration =====
%=================================

clear
clc

Duration = 2;
SamplingRate = 100;
Time = 0:1/SamplingRate:Duration;
Time = Time(1:length(Time)-1);

Curve = zeros(1,length(Time));
for i=2:length(Time)
    if (i <= (length(Time)/4))
        Curve(i) = Curve(i-1) + 1;
    end
    if (i > (length(Time)/4)) && (i < ((length(Time)/4)*3))
        Curve(i) = (length(Time)/4);
    end
    if (i >= ((length(Time)/4)*3))
        Curve(i) = Curve(i-1) - 1;
    end
end

CurveArea = zeros(1,length(Time));
for ii=2:length(Time)
    TimeInterval(ii) = Time(ii)-Time(ii-1);
    MagnitudeInterval(ii) = (Curve(ii) + Curve(ii-1))/2;
    CurveArea(ii) = CurveArea(ii-1)+(TimeInterval(ii)*MagnitudeInterval(ii));
end

figure(1);
plot(Time(1:length(Time)),Curve,Time(1:length(Time)),CurveArea);
CurveLegend = legend('The Curve','The Integral of The Curve');
title('The Curve & The Integral of The Curve');
xlabel('Time (Sec)');
ylabel('Magnitude');
grid on