function RungeKutta4(n)
% Solves the ivp y'=y - 1/(1+x)^2 - 1/(1+x), y(0)=2 
% using the fourth order R-K method
%
fprintf('\n')
%
for iit=1:6
%
nn=2^(iit-1)*n;
uu(iit)=1/nn;
%
h=1/nn;%
y=2;
%
for i=0:nn
t=i*h;
t2=(i+0.5)*h;
t1=(i+1)*h;
ex=exp(t)+(1/(1+t));
er(i+1)=abs(ex-y);
k1=h*f1(t,y);
k2=h*f1(t2,y+0.5*k1);
k3=h*f1(t2,y+0.5*k2);
k4=h*f1(t1,y+k3);
y=y+(k1+2*k2+2*k3+k4)/6;
end
u1(iit)=max(er);
fprintf('h =%12.6f        maxerr = %15.6e\n',h,u1(iit))
end
fprintf('\n')
for iit=1:5
      p=log(u1(iit)/u1(iit+1))/log(2.);
      fprintf('h =%12.6f        rate %15.6e\n',uu(iit),p)
end
loglog(uu,u1,'b+')
xlabel('h');ylabel('Error');
%
function f=f1(x,y)
f=y-(1/(1+x)^2) -(1/(1+x));

