function adaptive_test1


% Setup for the particular ODE problem
ODE = @Harmonic;
x0 = [1,0];  % State is expected to be a row vector.
t0 = 0;
tfinal = 2*pi;
Nsteps = 10;

% Setup for adaptive solver,  using default tolerance and recursion settings:
stepper = @rk2;
order = 2;
solver = @(ODE,x0,t0,h)  adaptive(ODE,x0,t0,h,stepper,order);

% Integrate
[x,t] = ode_fixed(solver, @Harmonic, x0, t0, tfinal, Nsteps);


% Plots
tc = (0:1000)*2*pi/1000; % time axis for smooth plot of analytic solution
figure(1)
plot(x(:,1), x(:,2), 'r-x',...
     cos(tc), sin(tc), 'b-' )
title('adaptive\_test1.m: test of adaptive solver','fontsize',18)
legend('10 quality-controlled RK23 steps','Analytic soln')
xlabel('x','fontsize',18)
ylabel('y','fontsize',18)
axis([-1.2,2,-1.2,1.2])
axis equal  % isotropy is desirable.


% Now check how deep we must go to get a particular tolerance. This requires
% better machinery than 'ode_fixed', so I wrote adaptive_stepper.m
max_rec_depth = 14;
for n = 1:7
   tol(n) = 10^(-n);
   tolerance = [tol(n), tol(n)];
   
   % Integrate   
   [x,t, greatest_depth(n)] = adaptive_stepper(@Harmonic, x0, t0, tfinal,...
      Nsteps, stepper, order, tolerance, max_rec_depth);
end

figure(2)
semilogy(greatest_depth, tol, 'b-x')
title('adaptive\_test1.m: performance of adaptive\_stepper',...
   'fontsize',18)
ylabel('tolerance (both relative and absolute)',...
   'fontsize',18)
xlabel('R = greatest recursion depth = - log (step size)',...
   'fontsize',18)
hold on

% Now try the experiment again, using euler!
stepper = @euler;
order = 1;
tol=0; % re-initialize
greatest_depth=0; % re-initialize
for n = 1:4
   tol(n) = 10^(-n);
   tolerance = [tol(n), tol(n)];
   
   % Integrate   
   [x,t, greatest_depth(n)] = adaptive_stepper(@Harmonic, x0, t0, tfinal,...
      Nsteps, stepper, order, tolerance, max_rec_depth);
end

semilogy(greatest_depth, tol, 'm-x')
legend('RK23','RK12')

hold off



   