%
%   PLOT_DEMO1.M   Demo of how to plot simple functions in Matlab
%                  (Demo for Differential Equations Homework)
%
%   This is a file that illustrates some aspects of programming within the 
%   Matlab environment.  The goal here is simply to evaluate and plot the 
%   solutions to some IVPs.  In particular, we will plot three simple analytical
%   functions using a number of presentation techniques and plot styles.  The three 
%   functions of interest are:
%        y1(x) = exp(-2x)   and    y2(x) = x*exp(-2x)     1 < x < 4
%        y3(x) = ln(x+1)                                  0 < x < 10
%
%   File prepared by J. R. White, UMass-Lowell (Jan. 2005)
%

%
%   getting started
      clear all;   close all;   nfig = 0;
%
%   define the two different domains for x 
      xa = linspace(1,4,41);   xb = linspace(0,10,51);  
%
%   now evaluate the three functions (use Matlab's vector processing capability)
      y1 = exp(-2*xa);   y2 = xa.*exp(-2*xa);   y3 = log(xb+1);
%
%   plot y1 and y2 in separate figures
      nfig = nfig+1;   figure(nfig)
      plot(xa,y1,'r-','LineWidth',2), grid
      title('Plot\_Demo1:  Simple plot of y_1(x) = exp(-2x)')
      xlabel('x value'),ylabel('y_1(x)')
%
      nfig = nfig+1;   figure(nfig)
      plot(xa,y2,'g-.','LineWidth',2), grid
      title('Plot\_Demo1:  Simple plot of y_2(x) = x*exp(-2*x)')
      xlabel('x value'),ylabel('y_2(x)')
%
%   now plot y1 and y2 as separate subplots on same page
      nfig = nfig+1;   figure(nfig)
      subplot(2,1,1),plot(xa,y1,'r-','LineWidth',2), grid
      title('Plot\_Demo1:  Simple plot of y_1(x) and y_2(x)')
      gtext('y_1(x) = exp(-2x)')
      ylabel('y_1(x)')
%
      subplot(2,1,2),plot(xa,y2,'g-.','LineWidth',2), grid
      gtext('y_2(x) = x*exp(-2*x)')
      ylabel('y_2(x)'),xlabel('x value')
%
%   now plot y1 and y2 on the same axis in one subplot and y3 in another subplot
      nfig = nfig+1;   figure(nfig)
      subplot(2,1,1),plot(xa,y1,'r-',xa,y2,'g-.','LineWidth',2), grid
      title('Plot\_Demo1:  Simple plot of y_1(x), y_2(x), and y_3(x)')
      legend('y_1 = exp(-2x)','y_2 = x*exp(-2*x)')
      xlabel('x value'),ylabel('y_1(x) & y_2(x)')
%
      subplot(2,1,2),plot(xb,y3,'b--','LineWidth',2), grid
      xlabel('x value'),ylabel('y_3(x)'),legend('y_3 = ln(x+1)')
%
%   Note: For this example, the last plot is probably the most informative.  It has
%   all three functions.  Two subplots were used since there were two different 
%   x ranges.  There is a lot of flexibility here depending on what your goals 
%   are for the plot.  Always try to make your plots as informative as possible...
%
%   end of program
