function val = taylorsin(x, tol, kmax)
% taylorsin Computes an approximation to sin(x) using Taylor series [PHYS210-Hw3]
%  
% Input arguments
%
%  x:     The value of x at which the approximation is to be computed.
%  tol:   The tolerance for the computation.  The function will
%         return the approximation when the magnitude of the current 
%         term being summed is <= this value.
%  kmax:  The maximum number of terms that will be summed.
%
% Ouput argument 
%
%  val:   The approximate value of sin(x).
%      
% Also illustrates the use of the return statement to exit a function 
% immediately (i.e. before the end of the function definition).

   % Initialize the output argument.
   val = 0;

   % Loop to sum the series.
   for k = 1 : kmax

      % Compute k-th term in series.
      term =  (-1)^(k - 1) * x^(2*k - 1) / factorial(2*k - 1);

      % Update the approximation.
      val = val + term;

      % If the magnitude of the current term is smaller than 
      % the tolerance, exit the procedure using the return statement.
      if abs(term) <= tol;
         return;
      end
   end

   % If execution reaches this point then convergence to the requested
   % tolerance has not been achieved using the specified maximum 
   % number of terms.  Set the output argument to NaN and return.
   val = NaN;

end
