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}
Best Answer
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):2. Use
pgfplots
for plotting.This greatly simplifies these kinds of tasks. Below is a minimal example of your code which produces:
References:
Code: