Home > tidal_ellipse > tanh_v5_2.m

tanh_v5_2

PURPOSE ^

TANH Hyperbolic tangent.

SYNOPSIS ^

function y = tanh(xin)

DESCRIPTION ^

TANH   Hyperbolic tangent.
   TANH(X) is the hyperbolic tangent of the elements of X.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function y = tanh(xin)
0002 %TANH   Hyperbolic tangent.
0003 %   TANH(X) is the hyperbolic tangent of the elements of X.
0004 
0005 %   C. Moler, 6-17-92
0006 %   Copyright (c) 1984-98 by The MathWorks, Inc.
0007 %   $Revision: 5.4 $  $Date: 1997/11/21 23:28:39 $
0008 
0009 %   21 Oct 92 (jmh) -- fixed for matrix inputs
0010 %        2 Sep 93 (jmh) -- fixed for null matrix input
0011 
0012 % Complex argument
0013 if ~isreal(xin) %  2 Sep 93 (jmh)
0014     tr = tanh(real(xin));
0015     ti = i*tan(imag(xin));
0016     y = (tr + ti)./(1 + tr.*ti);
0017 
0018 % Real argument
0019 else
0020 
0021    % Reference: W. J. Cody and W. Waite, "Software Manual
0022    % for the Elementary Functions", 1980, chapter 13.
0023    
0024    xbig = 18.715;                       % 0.5*log(4/eps)
0025    xmid = 0.5493061443340549;           % log(3)/2
0026    xsmall = 1.49e-8;                    % sqrt(eps)
0027    x = abs(xin);
0028    y = zeros(size(x));
0029 
0030    % tanh(x) == 1 to working precision
0031    k = find(xbig <= x);
0032    if ~isempty(k)           % 21 Oct 92 (jmh)
0033       y(k) = ones(size(find(k)));       % added FIND -- 20 Jul 92 (jmh)
0034    end
0035 
0036    % 1/2 < tanh(x) < 1
0037    k = find((xmid < x) & (x < xbig));
0038    if ~isempty(k)           % 21 Oct 92 (jmh)
0039       y(k) = 1 - 2./(exp(2*x(k))+1);
0040    end
0041 
0042    % x < tanh(x) <= 1/2
0043    k = find((xsmall < x) & (x <= xmid));
0044    if ~isempty(k)           % 21 Oct 92 (jmh)
0045       p = [-0.16134119023996228e4 -0.99225929672236083e2 -0.96437492777225470];
0046       q = [0.48402357071988689e4 0.22337720718962313e4  0.11274474380534949e3];
0047       xx = x(k).^2;
0048       y(k) = x(k) + x(k).*(xx.*(((p(3)*xx + p(2)).*xx + p(1))) ./ ...
0049                           (((xx + q(3)).*xx + q(2)).*xx + q(1)));
0050    end
0051 
0052    % tanh(x) == x to working precision
0053    k = find((x <= xsmall) | isnan(x));
0054    if ~isempty(k)       % 21 Oct 92 (jmh)
0055       y(k) = x(k);
0056    end
0057 
0058    y = sign(xin).*y;
0059 end

Generated on Wed 30-Nov-2005 15:40:57 by m2html © 2003