Error function erf(x)
computation and figure anatomy (axes,legends and labels) have been rendered in three approaches.
- Fully
gnuplot
pgfplots
invokes gnuplot
- Fully
Matlab
Already there are good answers for example by Qrrbrbirlbel and cjorssen, both exploit pgfmath at macro level.
1. Fully gnuplot
Error function erf(x)
computation in gnuplot with axes, legends and labels rendered in gnuplot epslatex
terminal. The gnuplot terminal output is embedded automatically with gnuplottex package. terminal=pdf
does not render Math labels hence epslatex
terminal was used.
\documentclass[preview=true,12pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{gnuplottex}
\begin{document}
\begin{gnuplot}[terminal=epslatex,terminaloptions=color]
set grid
set size square
set key left
set title 'Error function in gnuplot $ erf(x) = \frac{2}{\sqrt{\pi}} \int_{0}^{x}e^{-t^{2}}\, dt$'
set samples 50
set xlabel "$x$"
set ylabel "$erf(x)$"
plot [-3:3] [-1:1] erf(x) title 'gnuplot' linetype 1 linewidth 3
\end{gnuplot}
\end{document}
1) gnuplot output figure
2. pgfplots
invokes gnuplot
Error function erf(x)
computation in gnuplot invoked by pgfplots and axes, legends, labels are rendered by pgfplots
\documentclass[preview=true,12pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xlabel=$x$,ylabel=$erf(x)$,title= {Error function in pgfplots $erf(x)=\frac{2}{\sqrt{\pi}}\int_{0}^{x}e^{-t^{2}}\, dt$},legend style={draw=none},legend pos=north west,grid=major,enlargelimits=false]
\addplot [domain=-3:3,samples=50,red,no markers] gnuplot[id=erf]{erf(x)};
% Note: \addplot function { gnuplot code } is alias for \addplot gnuplot { gnuplot code };
\legend{pgfplots-gnuplot}
\end{axis}
\end{tikzpicture}
\end{document}
2. pgfplots(gnuplot backend)output figure
3) Fully Matlab
Error function $erf(x)$ computation in Matlab with axes,legends,labels rendered using matlabfrag(psfrag tag based) and mlf2pdf functions.
Note: Fonts are frozen in PDF figure unlike the above approaches, but can be changed in mlf2pdf.m
before generating them.
**erf(x)
Matlab Script using mlf2pdf(matlabfrag as backend) to generate pdf **
clear all
clc
% Plotting section
set(0,'DefaultFigureColor','w','DefaultTextFontName','Times','DefaultTextFontSize',12,'DefaultTextFontWeight','bold','DefaultAxesFontName','Times','DefaultAxesFontSize',12,'DefaultAxesFontWeight','bold','DefaultLineLineWidth',2,'DefaultLineMarkerSize',8);
% x and y data
x=linspace(-3,3,50);
y=erf(x);
figure(1);plot(x,y,'r');
grid on
axis([-3 3 -1 1]);
xlabel('$x$','Interpreter','none');
ylabel('$erf(x)$','Interpreter','none');
legend('Matlab');legend('boxoff');
title('Error function in Matlab $erf(x)=\frac{2}{\sqrt{\pi}}\int_{0}^{x}e^{-t^{2}}\, dt$','Interpreter','none');
mlf2pdf(gcf,'error-func-fig');
3. Output Figure
gnuplot 4.4
,pgfplots 1.8
and pdflatex -shell-escape
engine were used.
pgfmath
function very easily with the Taylor expression of the erf function. The precision won't be that good anyway as TeX is not meant for math. (Although certain floating point packages/libraries may help here to a certain degree.)