% Script to plot Airy Taylor series y0(x,P) and y1(x,P) truncated
% after term P.

% User-chosen parameters
  xl = -7;  % left limit of plot
  xr = 3;   % right limit of plot
  dx = 0.1; % x increment for TS calculations.
  Pchoices = [1 3 7 10 30]; % P's to plot

  x = xl + (0:0.01:1)*(xr-xl); % Plotting points
  nP = length(Pchoices);

  subplot(2,2,1)
  color = 'rmgbk';  
  for j = 1:nP
    P = Pchoices(j);
    [y0,y1] = airy_ts(x,P);
    plot(x,y0,[color(j) '-']) % cycle through plot colors
    hold on
    legend_entry(j)= {['P=' int2str(P)]}; % j'th cell of a cell array
  end

  xlim([xl xr])
  ylim([-5 5])
  xlabel('x')
  ylabel('y_0^P(x)')
  legend(legend_entry{1:nP},'Location','SouthEast')
  title('Truncated Taylor Series approximants to y_0(x)')
  hold off
  
  subplot(2,2,2)
  
  % Plot TS approximation to Ai(x) for the final truncation index P,
  % by using Ai(x) = a0*y0^P(x) + a1*y1^P(x)
  % and calibrating by enforcing Ai(0) to be correct and Ai(10) to be tiny.
  
  Ai_0 = airy(0)
  [y0_0,y1_0] = airy_ts(0,P);
  [y0_10,y1_10] = airy_ts(10,P);
  a1_a0_ratio= -y0_10/y1_10;
  a0 = Ai_0/(y0_0+a1_a0_ratio*y1_0);
  a1 = a0*a1_a0_ratio;
  
  x= -10:0.1:10;
  [y0,y1] = airy_ts(x,P);
  plot(x,airy(x),'b-',x,a0*y0+a1*y1,'g--')
  xlabel('x')
  legend('Exact Ai(x)','P=30 TS')
  
  
  
  
