MATLAB code to graph solutions to a BVP

with a boundary layer


%  bndry_layer.m
%
%  Plot true solution and asymptotic approximation to the ODE BVP
%     epsilon*y'' + (1+epsilon)*y' + y = 0,
%     y(0) = 0,
%     y(1) = 1.

  epsilon = input(' Layer width epsilon = ');
  
  %  Set up grid.
  
  dt = epsilon / 10;
  t = [0:dt:1]';
  
  %  True solution
  
  const = 1 / (exp(-1) - exp(-1/epsilon));
  y_true = const * (exp(-t) - exp(-t/epsilon));
  
  %  Asymptotic approximation.
  
  y_approx = exp(1-t) - exp(1-t/epsilon);
  
  %  Plot true solution and approximation.
  
  plot(t,y_true,'-', t,y_approx,'--')
  xlabel('t')
  ylabel('y(t)')
  hdr = ['Solutions to BVP for epsilon = ', num2str(epsilon)];
  title(hdr)