%
%  code10Fc.m -- 08 nov 02 -- djm
%
%  -improved  Euler's method example

clear
disp('improved Euler method')

%  set t-points & initial v 

N  = input('input number of t-points on 0<t=<4*pi = ')
dt = 4*pi/N;

t = 0:dt:4*pi;
u(1) = 1;  v(1) = 0;

%  euler method iteration (2-step form)

for j=1:N
  U = u(j) + dt*v(j);
  V = v(j) - dt*u(j);
  u(j+1) = u(j) + (dt/2)*( v(j) + V );
  v(j+1) = v(j) + (dt/2)*(-u(j) - U );
end

%  plot t versus u(j), v(j) & exact solution (green)

figure(1);  clf;  hold on

plot(t, cos(t),'g')
plot(t,-sin(t),'g')

plot(t,u,'b')
plot(t,v,'r')
title('\bf improved Euler''s method')
xlabel('\bf t-axis')
ylabel('\bf u(t) in blue & v(t) in red')

disp('error at t=4*pi')
error = u(N)-1
