% 
%   fun1_main  Demonstrates use of function file fun1.m to plot
%                and study the function f = exp(at)*sin(wt)
%
%   Used as part of Lesson #3 to illustrate the use of function files 
%   within Matlab coding.
%
%   Prepared by J.R White, UMass-Lowell (last update: Sept. 2017)
%

      clear all, close all, nfig = 0;
%
%   set time vector for plots
      Nt = 501;  t = linspace(0,10,Nt)';
%
%   Case 1:  create simple plot for fixed a and w
      a = -0.2;  T = 2;  w = 2*pi/T;
      f1 = fun1(t,a,w);
%
      nfig = nfig+1; figure(nfig)
      plot(t,f1,'LineWidth',2),grid
      title(['Case 1: f(t) = exp(at)*sin(wt) for a = ',num2str(a), ...
            ' sec^{-1} and \omega = ',num2str(w),' rad/sec'])
      xlabel('time (sec)'),ylabel('f(t)')
%
%   Case 2:  evaluate sensitivity to decay factor a 
      a = [-0.2 -0.4 -0.6];  Na = length(a);  T = 2;  w = 2*pi/T;
      f2 = zeros(Nt,Na);
      for i = 1:Na
        f2(:,i) = fun1(t,a(i),w);
      end
%
      nfig = nfig+1; figure(nfig)    % this uses Matlab default colors
      plot(t,f2,'LineWidth',2),grid
      title(['Case 2: f(t) = exp(at)*sin(wt) for \omega = ',num2str(w),' rad/sec'])
      xlabel('time (sec)'),ylabel('f(t)')
      legend('a = -0.2 1/s','a = -0.4 1/s','a = -0.6 1/s')  % not general
%
      ss1 = ['r- ';'b: ';'m-.'];     % this sets user-desired plot styles
      txt1 = cell(Na,1);             % initial cell array for text within legend
      nfig = nfig+1;   figure(nfig)
      for k = 1:Na
        plot(t,f2(:,k),ss1(k,:),'LineWidth',2);grid on, hold on;
        tt = sprintf('a = %4.2f 1/s ',a(k));   txt1(k) = cellstr(tt);
      end
      title(['Case 2: f(t) = exp(at)*sin(wt) for \omega = ',num2str(w),' rad/sec'])
      xlabel('time (sec)'),ylabel('f(t)')
      legend(txt1,'location','NorthEast')
%
%   Case 3:  evaluate sensitivity to frequency
      a = -0.2;    T = [2 1];  NT = length(T);  w = 2*pi./T;
      f3 = zeros(Nt,NT);
      for i = 1:NT
        f3(:,i) = fun1(t,a,w(i));
      end
%
      ss2 = ['r- ';'b: ';'m-.'];     % this sets user-desired plot styles
      txt2 = cell(NT,1);             % initial cell array for text within legend
      nfig = nfig+1;   figure(nfig)
      for k = 1:NT
        plot(t,f3(:,k),ss2(k,:),'LineWidth',2);grid on, hold on;
        tt = sprintf('w = %6.3f rad/s ',w(k));   txt2(k) = cellstr(tt);
      end
      title(['Case 3: f(t) = exp(at)*sin(wt) for a = ',num2str(a),' sec^{-1}'])
      xlabel('time (sec)'),ylabel('f(t)')
      legend(txt2,'location','NorthEast')
%
%   end of program
