7

I'm new to TikZ and to this site, so forgive me if a similar question has already been asked.

I'd like to graph the polynomial function f defined by f(x) = x3 - 6x2. I want the y-axis scaled so that the graph of f fits. (I had to manually change the numbers on the y-axis. Such a noob, I know.) If I don't plot the graph of f, I have a nice, centered coordinate system. Adding the function messes up everything. Can someone help me with this? Here is my current setup:

\begin{center}
\begin{tikzpicture}[>=stealth]
%The following places numerals along the x-axis
\coordinate (-5 on x)   at  (0,5);
\node [below left]  at  (-5 on x)   {\small $-5$};
\coordinate (-4 on x)   at  (1,5);
\node [below left]  at  (-4 on x)   {\small $-4$};
\coordinate (-3 on x)   at  (2,5);
\node [below left]  at  (-3 on x)   {\small $-3$};
\coordinate (-2 on x)   at  (3,5);
\node [below left]  at  (-2 on x)   {\small $-2$};
\coordinate (-1 on x)   at  (4,5);
\node [below left]  at  (-1 on x)    {\small $-1$};
\coordinate (0 on x)    at  (5,5);
\node [below left]  at  (0 on x)    {\small $0$};
\coordinate (1 on x)    at  (6,5);
\node [below left]  at  (1 on x)    {\small $1$};
\coordinate (2 on x)    at  (7,5);
\node [below left]  at  (2 on x)    {\small $2$};
\coordinate (3 on x)    at  (8,5);
\node [below left]  at  (3 on x)    {\small $3$};
\coordinate (4 on x)    at  (9,5);
\node [below left]  at  (4 on x)    {\small $4$};
\coordinate (5 on x)    at  (10,5);
\node [below left]  at  (5 on x)    {\small $5$};

%The following places numerals along the y-axis
\coordinate (-40 on y)  at  (5,0);
\node [below left]  at  (-40 on y)  {\small $-40$};
\coordinate (-32 on y)  at  (5,1);
\node [below left]  at  (-32 on y)  {\small $-32$};
\coordinate (-24 on y)  at  (5,2);
\node [below left]  at  (-24 on y)  {\small $-24$};
\coordinate (-16 on y)  at  (5,3);
\node [below left]  at  (-16 on y)  {\small $-16$};
\coordinate (-8 on y)   at  (5,4);
\node [below left]  at  (-8 on y)   {\small $-8$};
\coordinate (8 on y)    at  (5,6);
\node [below left]  at  (8 on y)    {\small $8$};
\coordinate (16 on y)   at  (5,7);
\node [below left]  at  (16 on y)   {\small $16$};
\coordinate (24 on y)   at  (5,8);
\node [below left]  at  (24 on y)   {\small $24$};
\coordinate (32 on y)   at  (5,9);
\node [below left]  at  (32 on y)   {\small $32$};
\coordinate (40 on y)   at  (5,10);
\node [below left]  at  (40 on y)   {\small $40$};
%The following draws the grid lines
\draw[help lines] (-0.25,-0.25) 
  grid (10.25,10.25);
%The following draws the x-axis
\draw[line width=1.0pt,->] (-0.5,5) -- (10.5,5) 
  node[right] {$x$}; 
%The following draws the y-axis
\draw[line width=1.0pt,->] (5,-0.5) -- (5,10.5) 
  node[above] {$y$};
\draw[color=blue,domain=-3:5.2] 
  plot (\x+5,{5+(\x*\x*\x/8)-(3*\x)/8}) 
  node[right] {$f(x) = x^3 - 3x$};
\end{tikzpicture}
\end{center}
  • Welcome to TeX.SE. Thanks for providing a code snippet to show that you made an attempt. But, it is better to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. – Peter Grill Apr 10 '12 at 0:02
10

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:

enter image description here

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}
8

With tkz-fct (and gnuplot), it's easy ...

\documentclass{article}
\usepackage{tkz-fct}
\begin{document}
\begin{tikzpicture}
  \tkzInit[xmin=-5,xmax=5,ymin=-40,ymax=40,ystep=8]   
  \tkzGrid
  \tkzAxeXY
  \tkzFct[color=blue,thick,domain = -5:5]{x**3-3*x};
\end{tikzpicture}
\end{document} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.