Your domain is too large. So setting it to domain=-3:3.8
yields decent results.
However, there are other suggestions:
1. Use \foreach
You should use \foreach
loops for repetitive tasks. For instance, code identical to yours that places the labels can more easily be written as (with \usetikzlibrary{calc}
in the preamble):
%The following places numerals along the x-axis
\foreach \x in {-5,...,5}{%
\coordinate (\x on x) at ($(\x,5)+(5,0)$);
\node [below left] at (\x on x) {\small $\x$};
}%
%The following places numerals along the y-axis
\foreach \y in {-40,-32,...,40}{%
\coordinate (\y on y) at ($(5,\y/8)+(0,5)$);
\node [below left] at (\y on y) {\small $\y$};
}%
2. Use pgfplots
for plotting.
This greatly simplifies these kinds of tasks. Below is a minimal example of your code which produces:
References:
Code:
\documentclass{article}
\usepackage{pgfplots}
\def\FunctionF(#1){(#1)^3- 3*(#1)}%
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis y line=center,
axis x line=middle,
axis on top=true,
xmin=-5.5,
xmax=5.5,
ymin=-45,
ymax=45,
height=12.0cm,
width=12.0cm,
grid,
xtick={-5,...,5},
ytick={-40,-32,...,40},
]
\addplot [domain=-5:5, samples=50, mark=none, ultra thick, blue] {\FunctionF(x)};
\node [left, blue] at (axis cs: 3.6,42) {$x^3-3x$};
\end{axis}
\end{tikzpicture}
\end{document}
A modification of the popcorn function. \psThomase@i
will be part of the next version of pst-func
, hopefully.
\documentclass[]{article}
\pagestyle{empty}
\usepackage{pst-func}
\makeatletter
\def\psThomae@i(#1,#2)#3{%
\addbefore@par{dotsize=1pt}
\begin@ClosedObj
\addto@pscode{
1 1 #3 {
dup
/ipSave ED % save loop value
/ip ED % dito
1 1 #3 {
dup
/iqSave ED % save loop value
/iq ED % dito
{
iq 0 le { exit } if
ip iq mod
/ip iq def
/iq ED
} loop
ip 1 eq {
/xVal ipSave iqSave div def
xVal #1 ge { xVal #2 lt {
\pst@number\pslinewidth SLW
\pst@usecolor\pslinecolor xVal 1 iqSave div \tx@ScreenCoor
2 copy pop 0 moveto lineto } if } if
} if
} for
} for
}%
\end@ClosedObj%
}
\makeatother
\begin{document}
\psset{unit=4cm}
\begin{pspicture}(-0.1,-0.2)(2.5,1.15)
\psaxes{->}(0,0)(2.25,1.1)
\psThomae[linewidth=1pt](0,2){200}
\end{pspicture}
\end{document}
Best Answer
Here's an example of your function using the
pgfplots
package:The axis settings (range, grid, appearance, ..) can be defined in the optional argument of the
axis
environment. Inside theaxis
environment you can plot multiple curves or data point series using\addplot
macros.Edit
Here's a version using
pgfplots
which looks more similar to your example: