%
% Code for the solution of the
% differential equation:
%
%   dy/dt = f(t,y)
%
%  The right hand side of the equation,
%  i.e. f(t,y) should be found in the
%  function rhs.m
%
clear                    % this clears any pre-existing variables
% global statement, if required, should go here

%  inputs

y(1)=input(' enter the starting value of y: ');
tmax = input(' enter the maximum value of t: ');
h = input(' enter the value of the stepsize h: ');
nstep = tmax/h;
t = 0:h:tmax;
%
%  rk2 loop
%
for n = 1:nstep
   k1=h*rhs(t(n),y(n));
   k2=h*rhs(t(n)+h/2,y(n)+k1/2);
   y(n+1)=y(n)+k2;
end

% output
yexact=100./(1+(100/y(1)-1).*exp(-t/2));

plot(t,y,'b',t,yexact,'r');
legend(' y: rk2', 'y: exact');
maxerror=max(abs(y-yexact))

