答案1
- 代码片段中提到的函数与问题中图片中显示的函数不同。我在下面考虑了第一个,但更改它并不困难。
- 在下面的 MWE 中,我使用包
pgfdplots
版本 v1.14。 x
选取最小值,使得函数方程能够拟合成图。
\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ticklabel style={fill=white, font=\footnotesize},
xmin=-5, xmax=3, xlabel={$x$}, xtick={-5,...,3},
ymin=-5, ymax=3, ylabel={$y$}, ytick={-5,...,3},
grid,
axis lines=middle
]
\addplot[blue, very thick, samples=50, domain=-2:1.8] {(x)^3 -(x)^2 + 1};
\node[below right, fill=white, text=blue] at (-4,3) {$f(x) = x^3 - x^2 + 1$};
\end{axis}
\end{tikzpicture}
\end{document}
如果您想要第二个方程,则适当更改线条\addplot ...
和\node ...
。您还可以将轴放在顶部并放大图形宽度和函数域,如下所示:
\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=88mm,
ticklabel style={fill=white, inner xsep=1pt, font=\footnotesize},
xmin=-5, xmax=4, xlabel={$x$}, xtick={-5,...,4},
ymin=-5, ymax=3, ylabel={$y$}, ytick={-5,...,3},
set layers = axis on top,
axis lines = middle,
grid=both,
]
\addplot[blue, very thick, samples=50, domain=-2:4] {-5*(x^4)/16 + 3*(x^3)/4 + 1};
\node[below right, inner xsep=0pt, fill=white, text=blue] at (-5,3)
{$f(x) = -\frac{5}{16}x^4 + \frac{3}{4}x^3 + 1$};
\end{axis}
\end{tikzpicture}
\end{document}
并且图表将变成: