[Tex/LaTex] Plot sin(x)/x using TikZ

pgfplotstikz-pgf

Something is going wrong here…

\documentclass[10pt]{article}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{figure}[!h]
\centering
    \resizebox{.5\linewidth}{!}{
\begin{tikzpicture}[scale=1]
\begin{axis}[grid=both,xmin=-4,xmax=4,ymin=-2,ymax=2,xlabel=$x$,ylabel=$y$,axis lines=center,>=stealth]
\addplot[domain=-4:4, blue, ultra thick] plot[smooth] {sin(deg(x))/x}; 
\end{axis}
\end{tikzpicture}
    }
    \caption{Plot of $\frac{\sin x}{x}$.}
\end{figure}

\end{document}

Does anyone have any ideas of how to 'fix' this?

Best Answer

The default number of samples is too small (25), increasing it helps to smooth the function. Also an odd number triggers the problematic value sin(0)/0, thus the number should be even for the symmetric domain.

\documentclass[10pt]{article}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{figure}[!h]
\centering
\begin{tikzpicture}[scale=1]
\begin{axis}[
  grid=both,
  xmin=-4,
  xmax=4,
  ymin=-2,
  ymax=2,
  xlabel=$x$,
  ylabel=$y$,
  axis lines=center,
  >=stealth
]
  \addplot[
    domain=-4:4,
    blue,
    ultra thick,
    samples=100,
  ] plot[smooth] {sin(deg(x))/x};
\end{axis}
\end{tikzpicture}
\caption{Plot of $\frac{\sin x}{x}$.}
\end{figure}

\end{document}

Result

Related Question