# Difficulty with Tikz curves

I would like to draw this curve:

Using tikz, I did the following

\begin{tikzpicture}
\draw [<->,thick] (0,5) node (yaxis) [above, text width=3.5cm,align=center] {$B_{J}(x)$}
|- (7,0) node (xaxis) [right] {$x$};
\draw (0,0)--(0.3,0.9)--(0.6,1.2);
\end{tikzpicture}

But, the co-ordinates are breaking my head. Is there an easy way to do this?

-
"the co-ordinates are breaking my head" ... what? :) I have no idea what you mean and apparently Jesse doesn't either. – Svend Tveskæg Jul 20 '14 at 6:13
What is the function B_J(x) ? – percusse Jul 20 '14 at 6:50
@percusse please see the edit – subham soni Jul 20 '14 at 6:54
Well, what is J then? – percusse Jul 20 '14 at 6:59
Can you come in the chat? @percusse – subham soni Jul 20 '14 at 7:02

Assuming I got the equation bit right then you could try using LuaLaTeX:

\documentclass[border=5]{standalone}
\usepackage{tikz}
\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]
\draw [help lines] (-5,0) -- (10,0);
\draw [help lines, -stealth] (0,-1) -- (0,1.5);
\draw [densely dotted] (0,{ Brillouin(1, 100)} ) -- ++(10,0);
\draw [red]   plot [domain=-5:10, samples=100] (\x, { Brillouin(1, \x)});
\draw [green] plot [domain=-5:10, samples=100] (\x, { Brillouin(5, \x)});
\draw [blue]  plot [domain=-5:10, samples=100] (\x, { Brillouin(50, \x)});
\end{tikzpicture}
\end{document}

-
I added the example with this nice method to the TeXample TikZ gallery, thank you for this demonstration! – Stefan Kottwitz Jul 29 '14 at 20:45

Something like this. Use of \draw plot command in tikz.

Update Based on the OP's comment about not matching the original figure, this update provides a first order exponential plot with time constant capability. (One can change the y label 2 to 1.) Change parameter \a value for different plots with corresponding time constants.

Code

\documentclass[border=10pt]{standalone}%{article}
\usepackage{tikz}
\pagestyle{empty}
\def\a{3}              % time constant=1/a
\begin{document}

\begin{tikzpicture}
\draw [<->,thick] (0,2.5) node (yaxis) [above, text width=3.5cm,align=center] {$B_{J}(x)$} |- (5,0) node (xaxis) [right] {$x$};
\draw[dashed] (0,2)node[left]{2}--node[above]{value}(3,2);
\node[left] at (0,0){0};
\draw[red] plot[domain=0:4,samples=50] ({\x},{2-2*exp(-\a*\x)});   % <-- \a is added
\end{tikzpicture}

\end{document}

Code

\documentclass[border=10pt]{standalone}%{article}
\usepackage{tikz}
%\usepackage{pgfplots,pgfplotstable}
\pagestyle{empty}
\begin{document}

\begin{tikzpicture}
\draw [<->,thick] (0,3) node (yaxis) [above, text width=3.5cm,align=center] {$B_{J}(x)$} |- (7,0) node (xaxis) [right] {$x$};
\draw[dashed] (0,1)node[left]{1}--node[above]{above}(5,1);
\node[left] at (0,0){0};
\draw[red] plot[domain=0:5] ({\x},{1-exp(-\x)});
\end{tikzpicture}

\end{document}
-
Nope. It isn't matching with the original figure – subham soni Jul 20 '14 at 6:02
Sorry, I don't quite follow you. I used the x,y axis provided by you. The function shown in the original figure is like 1-exp(-x). – Jesse Jul 20 '14 at 6:13
You could use 2-2*exp(-x) or any value higher than 2, to get a larger magnitudes, if that is what you meant. Or you are referring to the time constant of an exponential function. – Jesse Jul 20 '14 at 6:26

Here's an option using pgfplots, which is based on tikz, but eases the plotting of such functions.

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass[tikz,border=2pt]{standalone}

\usepackage{pgfplots}

% arrows are stealth fighters
\tikzset{>=stealth}
\begin{document}
\begin{tikzpicture}[
/pgf/declare function={coth(\x)=1/tanh(\x);},
/pgf/declare function={brillouin(\x,\J)=%
(2*\J+1)*coth( (2*\J+1)*\x/(2*\J) )/(2*\J)-coth(\x/(2*\J))/(2*\J);
},
]
\begin{axis}[
axis x line=middle,
axis y line=middle,
axis line style=->,
ymax=2,
grid=none,
samples=100,
xtick={-1},
ytick={1},
xlabel={$x$},
ylabel={$B_J(x)$},
]
\end{axis}
\end{tikzpicture}
\end{document}
-

Just for typing exercise with PSTricks.

\documentclass[pstricks,border=15pt,12pt,dvipsnames]{standalone}
\usepackage{pst-math,pst-plot}

\def\f#1{(2*#1+1)/(2*#1*TANH((2*#1+1)/(2*#1)*x))-1/(2*#1*TANH(x/(2*#1)))}

\psset
{
xAxisLabel=$x$,
yAxisLabel=$y$,
algebraic,
}
\begin{document}
\begin{psgraph}[linecolor=gray,tickcolor=gray]{<->}(0,0)(-6.5,-1.5)(6.5,1.5){12cm}{8cm}
\foreach \n/\c in {2/Red,3/ForestGreen,5/NavyBlue,7/Maroon}{%
\psplot[linecolor=\c]{-6}{6}{\f{\n}}}
\end{psgraph}
\end{document}

-
Any customization is still possible, just drop your comment. – kiss my armpit Jul 20 '14 at 13:08