home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / ELFUN.DI$ / TANH.M < prev   
Encoding:
Text File  |  1993-03-07  |  1.6 KB  |  58 lines

  1. function y = tanh(xin)
  2. %TANH    Hyperbolic tangent.
  3. %    TANH(X) is the hyperbolic tangent of the elements of X.
  4.  
  5. %    C. Moler, 6-17-92
  6. %    Copyright (c) 1984-93 by the MathWorks, Inc.
  7.  
  8. %    21 Oct 92 (jmh) -- fixed for matrix inputs
  9.  
  10. % Complex argument
  11. if any(any(imag(xin) ~= 0))        % 21 Oct 92 (jmh)
  12.     tr = tanh(real(xin));
  13.     ti = i*tan(imag(xin));
  14.     y = (tr + ti)./(1 + tr.*ti);
  15.  
  16. % Real argument
  17. else
  18.  
  19.    % Reference: W. J. Cody and W. Waite, "Software Manual
  20.    % for the Elementary Functions", 1980, chapter 13.
  21.    
  22.    xbig = 18.715;                       % 0.5*log(4/eps)
  23.    xmid = 0.5493061443340549;           % log(3)/2
  24.    xsmall = 1.49e-8;                    % sqrt(eps)
  25.    x = abs(xin);
  26.    y = zeros(size(x));
  27.  
  28.    % tanh(x) == 1 to working precision
  29.    k = xbig <= x;
  30.    if any(any(k))            % 21 Oct 92 (jmh)
  31.       y(k) = ones(size(find(k)));       % added FIND -- 20 Jul 92 (jmh)
  32.    end
  33.  
  34.    % 1/2 < tanh(x) < 1
  35.    k = (xmid < x) & (x < xbig);
  36.    if any(any(k))            % 21 Oct 92 (jmh)
  37.       y(k) = 1 - 2./(exp(2*x(k))+1);
  38.    end
  39.  
  40.    % x < tanh(x) <= 1/2
  41.    k = (xsmall < x) & (x <= xmid);
  42.    if any(any(k))            % 21 Oct 92 (jmh)
  43.       p = [-0.16134119023996228e4 -0.99225929672236083e2 -0.96437492777225470];
  44.       q = [0.48402357071988689e4 0.22337720718962313e4  0.11274474380534949e3];
  45.       xx = x(k).^2;
  46.       y(k) = x(k) + x(k).*(xx.*(((p(3)*xx + p(2)).*xx + p(1))) ./ ...
  47.                           (((xx + q(3)).*xx + q(2)).*xx + q(1)));
  48.    end
  49.  
  50.    % tanh(x) == x to working precision
  51.    k = (x <= xsmall) | isnan(x);
  52.    if any(any(k))        % 21 Oct 92 (jmh)
  53.       y(k) = x(k);
  54.    end
  55.  
  56.    y = sign(xin).*y;
  57. end
  58.