% ====================================================================================
% =====   Discritized Approximation of The Exact Solution of: dy/dx = exp(x)     =====
% ===== The Exact Solution of The Ordinary Differential Equation is: y(x)=exp(x) =====
% =====               << The 2nd Order Adams - Moulton Method >>                 =====
% ====================================================================================

clc
clear

dt=0.1;			%The Time Step
Time=0:dt:4;	%The Time Interval

AdamsMoultonApproximation(1)=1;	%The Initial Condition
AdamsMoultonApproximation(2)=1;	%The Initial Condition

for i=2:length(Time)-1
   Function1=exp(Time(i));
   Function2=exp(Time(i-1));
%  AdamsMoultonApproximation(i+1)=AdamsMoultonApproximation(i)+((dt/2)*(3*Function1-Function2));
   AdamsMoultonApproximation(i+1)=AdamsMoultonApproximation(i)+((dt/2)*(Function2+Function1)); %((dt/2)*(AdamsMoultonApproximation(i+1)+Function1));
end

%The Exact Solution
ExactSolution=exp(Time);

figure(1);
plot(Time,ExactSolution,'b',Time,AdamsMoultonApproximation,'r');
legend('Exact Solution','2nd Order Adams - Moulton Approximation');
title('2nd Order Adams - Moulton Approximation, dt = 0.1');
xlabel('Time');
ylabel('Y');
grid on