% =============================================================================================
% =====        Discritized Approximation of The Exact Solution of: d^2y/dx^2 = x+y        =====
% =====   The Exact Solution of The Ordinary Differential Equation is: (e^x)-(e^(-x))-x   =====
% ===== << The Euler Method for Higher Than First Order Ordinary Differential Equation >> =====
% =============================================================================================

clc
clear

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

FirstOrderDerivative(1)=1;	%The Initial Condition for y'(0)=1
SecondOrderDerivative(1)=0; %The Initial Condition for y(0)=0
Z(1)=FirstOrderDerivative(1);

for i=1:length(Time)-1
   
   X(i)=SecondOrderDerivative(i);
   TheFunction(i)=SecondOrderDerivative(i)+X(i);
   
   SecondOrderDerivative(i+1)=SecondOrderDerivative(i)+(dt*FirstOrderDerivative(i));
   Z(i+1)=Z(i)+(dt*TheFunction(i));
   
   FirstOrderDerivative(i+1)=Z(i+1);

end

ExactSolution=exp(Time)-exp(-Time)-Time; %The Exact Solution

plot(Time,ExactSolution,'b',Time,SecondOrderDerivative,'r');
legend('The Exact Solution','The Approximate Solution');
title('Higher Euler Approximation Method, dt=0.1');
xlabel('Time');
ylabel('Y');
grid on