function [M] = langevin(x)

% Langevin Function - Outputs L(x) where x is a vector or scalar.  
% 11/11/01 Mike Scarpulla.  Please direct questions and comments to scarps@uclink.berkeley.edu
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

M = ((exp(x)+exp(-x))./(exp(x)-exp(-x)) - 1./(x));

% both terms in the function -> infinity for x=0, so need to handle it explicitly
n = find(x==0);
M(n)=0;
%%%%%%%End of Essential Code%%%%%%%%%%%%%%%%%%


% Because L(x) is composed of a few exponential functions, the possibility exists that
% MATLAB will not be able to compute L(x) for large or small x (may be machine dependent)
% This section produces a warning when NaN or infinite values are encountered

n = ~isfinite(M);
n = find(n);

if ~isempty(n)
    disp('Warning: NaN or infinite values in output of "Langevin"')
end

