function [] = rk2()
%RK2 Runge-Kutta-Verfahren 2. Ordnung (Heun) fuer y'(t) = y(t), y(0) = 1

close all
help rk2

dt = input(' dt [0.2]: ');
if isempty(dt)
   dt = 0.2;
end

tmax = 2.0;

figure(1)
axis([0 tmax 0 exp(tmax)])
hold on

% Anfangsbedingung ---------------------------------
t = 0.0;
y = 1.0;
% Zeit-Schritte ------------------------------------
while t <= tmax
      plot(t,y,'+r','Markersize',10,'Linewidth',1.6)
      k1 = f(t,y);
      k2 = f(t+dt,y+dt*k1);
      y = y + (0.5*dt)*(k1 + k2);
      t = t + dt;
end
% --------------------------------------------------

t = linspace(0, tmax);
plot(t,exp(t),'-b')
hold off
title(['Verfahren von Heun: y''(t)=y(t), y(0)=1, \Deltat = ' ...
        num2str(dt)], 'FontSize',18)
xlabel('t', 'FontSize', 18); ylabel('y(t)', 'FontSize', 18)
set(gca, 'FontSize', 18)
box on
print -depsc rk2.eps

%----------------------
function dydt = f(~, y)
dydt = y;
%----------------------
