%
%   SINH_SERIES.M   Evaluate sinh(x) using Taylor Series
%
%   Inputs:
%     x - vector of independent variable values
%     maxT - maximum number of terms in series (optional, default value = 10)
%     tol - error tolerance for truncating series (optional, default value = 0.0001)
%
%   Outputs:
%     f - value of sinh(x) evaluated at all x values
%
%   File prepared by J. R. White, UMass-Lowell (last update: Oct. 2017)
%

      function f = sinh_series(x,maxT,tol)
%
%  set defaults if no inputs for maxT or tol, and check on minimum value for tol
      if nargin == 1, maxT = 10; tol = 0.0001;  end  
      if nargin == 2,  tol = 0.0001;  end  
      if isempty(maxT) || maxT < 1, maxT = 10;  end
      if tol < 100*eps, tol = 100*eps;  end
%
%  initialize variables and perform computational loop
      T = x;  f = T;   rerr = 1.0;  n = 1;
      while rerr > tol && n < maxT
        r = x.*x/((2*n+1)*2*n);
        T = r.*T;       % next termin series
        f = f + T;      % update running sum
        i = find(f);    % finds indices of nonzero values of f
        rerr = max(abs(T(i)./f(i))); % calc max relative error
        n = n+1;
      end
%
%  display warning if hit max # of terms
      if n == maxT
        disp(' ')
        disp('  *** WARNING ***  Hit max # of terms in sinh_series.m')
        disp(' ')
      end
%
%   end of function
