% Boundary layer numerical demo: 2nd order singularly-perturbed ODEs.
% Hit mouse near ends to change the a,b end BCs; any key to exit
% Barnett 4/19/11.   ODE is :  eps.y'' + p(x).y' + q(x).y = 0

eps = 0.02;    % perturbation
x = 0:0.01:1;  % initial grid
a = 2; b = 1;     % Dirichlet BCs for y value at x=0 & x=1
p = @(t) 1;   % 1+t left BL, -1 right, 2*t-1 interior layer; 1-2*t 2 BLs
q = @(t) 1;

figure('position', [300 100 400 800]); k=0;
while k==0
  F = @(t,y) [y(2); -(p(t)*y(2) + q(t)*y(1))/eps];  % set up and solve BVP
  bc = @(uva,uvb) [uva(1)-a; uvb(1)-b];
  solinit = bvpinit(x, [1 1]);
  s = bvp4c(F, bc, solinit);

  hold off; plot(s.x, s.y(1,:), '-', 'linewidth', 2); hold on;  % show soln
  plot([0 1],[a b],'.k', 'markersize', 30);
  c = axis; axis([0 1 -1 4]); drawnow;

  k = waitforbuttonpress; z = get(gca,'CurrentPoint');     % interactivity
  xp = z(1,1); yp = z(1,2);   % coords where mouse pressed
  if xp<0.2, a = yp; elseif xp>0.8, b = yp; end % if near ends, change the BC
end
