% ====================================================================================
% =====   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 4th Order Runge - Kutta Method >>                 =====
% ====================================================================================

clc
clear

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

RungeKuttaApproximation(1)=1.0;	%The Initial Condition

for i=1:length(Time)-1
   k1=dt*exp(Time(i));				
   k2=dt*exp(Time(i)+(1/2*dt));
   k3=dt*exp(Time(i)+(1/2*dt));
   k4=dt*exp(Time(i)+dt);
   RungeKuttaApproximation(i+1)=RungeKuttaApproximation(i)+((1/6)*(k1+2*k2+2*k3+k4));
end

%The Exact Solution
ExactSolution=exp(Time);

figure(1);
plot(Time,ExactSolution,'b',Time,RungeKuttaApproximation,'r');
legend('Exact Solution','4th Order Runge - Kutta Approximation');
title('4th Order Runge - Kutta Approximation, dt = 0.1');
xlabel('Time');
ylabel('Y');
grid on
