% Brillouin Function
% Author: Mark Wibrow
\documentclass[tikz,border=10pt]{standalone}
%%%<
\usepackage{verbatim}
%%%>
\begin{comment}
:Title: Plot of the Brillouin Function
:Tags: LuaTeX;Plots;Plotting;Mathematics
:Author: Mark Wibrow
:Slug: brillouin-function

The Brillouin function[1][2] is a special function, used for example
in statistical mechanics.

It's plotting using standard TikZ, pgfplots is not required.
The calculation is done using Lua, so LuaLaTeX has to be used for compiling.

This example was written by Mark Wibrow answering a question on TeX.SE,
with modifications by Stefan Kottwitz (formula, scaling, styles for
axis and plot).
\end{comment}
\usepackage{amsmath}
\usetikzlibrary{arrows.meta}
\directlua{
function coth (i) 
  return math.cosh(i) / math.sinh(i)
end

function brillouin (J, x) 
  if x == 0 then
    return 0
  else
   return (2*J+1)/(2*J)*coth((2*J+1)/(2*J)*x) - 
        1/(2*J)*coth(1/(2*J)*x)
  end
end
}
\pgfmathdeclarefunction{Brillouin}{2}{%
  \edef\pgfmathresult{%
     \directlua{tex.print("" .. brillouin(#1,#2))}%
   }%  
}
\begin{document}
\begin{tikzpicture}[
    x                = 2cm/10,
    scale            = 3,
    axis/.style      = {help lines, -{Stealth[length = 1.5ex]}},
    brillouin/.style = {domain = -5:10, samples = 100}
  ]
  \draw [axis] (-5,0) -- (10,0);
  \draw [axis] (0,-1) -- (0,1.5);
  \draw [densely dotted] (0,{ Brillouin(1, 100)} ) -- ++(10,0);
  \draw [red]   plot [brillouin] (\x, { Brillouin(1,  \x)});
  \draw [green] plot [brillouin] (\x, { Brillouin(5,  \x)});
  \draw [blue]  plot [brillouin] (\x, { Brillouin(50, \x)});
  \node [align = center, anchor = west] at (1,1.3) {%
    $\begin{alignedat}{2}
      B_J(x) &= \tfrac{2J + 1}{2J}
                &&\coth \left ( \tfrac{2J + 1}{2J} x \right ) \\
             &\quad - \tfrac{1}{2J}
                &&\coth \left ( \tfrac{1}{2J} x \right )
     \end{alignedat}$};
\end{tikzpicture}
\end{document}