%Differential Equation: dy/dx=y-(1/2)*exp(x/2).*sin(5*x)+5*exp(x/2).*cos(5*x); y(0)=0;
f=@(x,y) y-(1/2)*exp(x/2).*sin(5*x)+5*exp(x/2).*cos(5*x);
f1=@(x,y) exp(x/2).*sin(5*x);   %Analytic Solution
xinit=0; yinit=0;           %y(xinit)=yinit {y(0)=0}

xmax=5;
x=xinit:0.05:xmax;
h=x(2)-x(1);
m=length(x);
y=zeros(1,m);
y(1)=yinit;

for n=1:3
    k1=h*f(x(n), y(n));
    k2=h*f(x(n)+h/2, y(n)+k1/2);
    k3=h*f(x(n)+h/2, y(n)+k2/2);
    k4=h*f(x(n)+h, y(n)+k3);
    y(n+1)=y(n)+(1/6)*(k1+2*k2+2*k3+k4);
end;

for n=4:m-1
    y(n+1)=y(n-3)+4*h/3*(2*f(x(n),y(n))-f(x(n-1),y(n-1))+2*f(x(n-2),y(n-2)));
    y(n+1)=y(n-1)+h/3*(2*f(x(n+1),y(n+1))+4*f(x(n),y(n))+f(x(n-1),y(n-1)));
end

yy=exp(x/2).*sin(5*x);          %Analytic Solution
hold on;
h=plot(x,y,'ro-','LineWidth',1.5);
plot(x,yy,'b-','LineWidth',2.5);
title('Milne Method','fontsize',14,'fontweight','bold');
grid on;
legend('Milne','Analytic','Location','NorthWest');
hold off;