我是 TikZ 和本网站的新手,所以如果有人问过类似的问题,请原谅我。
我想绘制多项式函数 f 的图形,该函数由 f(x) = x 3 - 6x 2定义。我希望缩放 y 轴,以便 f 的图形适合。(我不得不手动更改 y 轴上的数字。我知道,我是个菜鸟。)如果我不绘制 f 的图形,我会得到一个很好的居中坐标系。添加函数会把一切都搞乱。有人能帮我吗?这是我当前的设置:
\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}
答案1
您的域名太大。因此将其设置为会domain=-3:3.8
产生不错的结果。
不过,还有其他建议:
1. 使用\foreach
您应该使用\foreach
循环来执行重复性任务。例如,与您放置标签的代码相同的代码可以更轻松地编写为(在\usetikzlibrary{calc}
序言中):
%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. 用于pgfplots
绘图。
这大大简化了这类任务。下面是代码的一个最小示例,它会产生以下内容:
参考:
代码:
\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}
答案2
使用 tkz-fct(和 gnuplot),这很容易......
\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}