% 
%   fun1_anon  Demonstrates use of anonymous functions to plot
%               and study the function f = exp(at)*sin(wt)
%
%   Used as part of Lesson #3 to illustrate the use of anonymous functions  
%   within Matlab coding.  This file does the same thing as fun1_main.m and fun1.m
%   except now the function file is kept directly within the main script file.  This
%   approach is useful when working with relatively simple functions...
%
%   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;
      ft = @(t) exp(a*t).*sin(w*t);   % anonymous function
%
      nfig = nfig+1; figure(nfig)
      plot(t,ft(t),'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
        ft = @(t) exp(a(i)*t).*sin(w*t);    % anonymous function
        f2(:,i) = ft(t);
      end
%
      ss1 = ['r- ';'b: ';'m-.'];   txt1 = cell(Na,1);
      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
        ft = @(t) exp(a*t).*sin(w(i)*t);    % anonymous function
        f3(:,i) = ft(t);
      end
%
      ss2 = ['r- ';'b: ';'m-.'];   txt2 = cell(NT,1);
      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
