Erf function in LaTeX
Based on this answer.
\documentclass{standalone}
\usepackage{tikz}
\makeatletter
\pgfmathdeclarefunction{erf}{1}{%
\begingroup
\pgfmathparse{#1 > 0 ? 1 : -1}%
\edef\sign{\pgfmathresult}%
\pgfmathparse{abs(#1)}%
\edef\x{\pgfmathresult}%
\pgfmathparse{1/(1+0.3275911*\x)}%
\edef\t{\pgfmathresult}%
\pgfmathparse{%
1 - (((((1.061405429*\t -1.453152027)*\t) + 1.421413741)*\t
-0.284496736)*\t + 0.254829592)*\t*exp(-(\x*\x))}%
\edef\y{\pgfmathresult}%
\pgfmathparse{(\sign)*\y}%
\pgfmath@smuggleone\pgfmathresult%
\endgroup
}
\makeatother
\begin{document}
\begin{tikzpicture}[yscale = 3]
\draw[very thick,->] (-5,0) -- node[at end,below] {$x$}(5,0);
\draw[very thick,->] (0,-1) -- node[below left] {$0$} node[at end,
left] {$erf(x)$} (0,1);
\draw[red,thick] plot[domain=-5:5,samples=200] (\x,{erf(\x)});
\end{tikzpicture}
\end{document}
Using the same approximation idea as cjorssen (I tried the Taylor series as Qrrbrbirlbel suggested but it's pretty hopeless to get a decent approximation this way) I rewrote the function without using low-level PGF. Because we have so many 2D plots here already, I'll just use my 3D plot that I had already.
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{
colormap={bluewhite}{ color(0cm)=(rgb:red,18;green,64;blue,171); color(1cm)=(white)}
}
\begin{document}
\begin{tikzpicture}[
declare function={erf(\x)=%
(1+(e^(-(\x*\x))*(-265.057+abs(\x)*(-135.065+abs(\x)%
*(-59.646+(-6.84727-0.777889*abs(\x))*abs(\x)))))%
/(3.05259+abs(\x))^5)*(\x>0?1:-1);},
declare function={erf2(\x,\y)=erf(\x)+erf(\y);}
]
\begin{axis}[
small,
colormap name=bluewhite,
width=\textwidth,
enlargelimits=false,
grid=major,
domain=-3:3,
y domain=-3:3,
samples=33,
unit vector ratio*=1 1 1,
view={20}{20},
colorbar,
colorbar style={
at={(1,-.15)},
anchor=south west,
height=0.25*\pgfkeysvalueof{/pgfplots/parent axis height},
}
]
\addplot3 [surf,shader=faceted] {erf2(x,y)};
\end{axis}
\end{tikzpicture}
\end{document}
The approximation has a maximum error of 1.5·10-7 (source).
Thanks to Jake for spotting and fixing the wrong syntax I first had in this code.
For precise values, I recommend externalizing the calculation, here gnuplot
is used.
Code (needs --shell-escape
enabled)
\documentclass{article}
\usepackage{amsmath,pgfmath,pgffor}
\makeatletter
\def\qrr@split@result#1 #2\@qrr@split@result{\edef\erfInput{#1}\edef\erfResult{#2}}
\newcommand*{\gnuplotErf}[2][\jobname.eval]{%
\immediate\write18{gnuplot -e "set print '#1'; print #2, erf(#2);"}%
\everyeof{\noexpand}
\edef\qrr@temp{\@@input #1 }%
\expandafter\qrr@split@result\qrr@temp\@qrr@split@result
}
\makeatother
\DeclareMathOperator{\erf}{erf}
\begin{document}
\foreach \x in {-50,...,50}{%
\pgfmathparse{\x/50}%
\gnuplotErf{\x/50.}%
$ x = \pgfmathresult = \erfInput, \erf(x) = \erfResult$\par
}
\end{document}