pgfplot 中的绘图函数

pgfplot 中的绘图函数

你好,我想绘制一个像这样的函数 在此处输入图片描述

方程 = x^3 -3x^2 + 1

我试过这个

\begin{center}
\begin{tikzpicture}
\begin{axis}[
    axis lines = left,
    xlabel = \(x\),
    ylabel = {\(f(x)\)},
]

\addplot [
    domain=-10:10, 
    samples=100, 
    color=red,
]
{x^3 - 3 * x^2 + 1};
\addlegendentry{\(x^3 - 3x^2 +1\)}


\end{axis}
\end{tikzpicture}
\end{center}

但它看上去与参考图片完全不同。

它看起来像这样:

在此处输入图片描述

我在这里做错了什么?

答案1

试试这个代码:

\documentclass[a4paper]{article}
\usepackage{tkz-euclide}
\begin{document}
    \centering
    \large 
    \textbf{Grafici matematici con Tikz}\\
    \vspace*{.5cm}  
    \begin{tikzpicture}[line width=3pt,scale=1.5]
    %\fill[gray!10] (-2.2,-6.2) rectangle (7.5,4.5); % prepara il colore di fondo
    \draw [white] (-2,-6) grid (7,4.5);     
    \draw [->,style=thick] (-2.2,0) -- (7.2,0) node[pos=1,right] {$x$}; 
    \foreach \x in {-2,...,7} \draw[thick] (\x,-0.05) to (\x,0.05) node[black,below] at (\x,-0.05) {$\x$}; 
    \draw [->,style=thick] (0,-6.20) -- (0,4.2) node[pos=1,above] {$y$}; 
    \foreach \x in {-6,...,4} \draw[thick] (-0.05,\x) to (0.05,\x) node[black,left] at (-0.05,\x) {$\x$};
    \draw [magenta, domain=-1.3:3.3, samples=50] plot (\x, {\x*\x*\x - 3 * \x*\x + 1}); 
    \draw [magenta] (3.6,3.5) to (4.4,3.5) node[right] at (4.5,3.5) {$y=x^3-3x^2+1$};
    \draw [cyan, domain=-2:3.3, samples=50] plot (\x, {\x*\x - \x - 3}); 
    \draw [cyan] (3.6,2.5) to (4.4,2.5) node[right] at (4.5,2.5) {$y=x^2-x^2-3$};
    \end{tikzpicture}

\end{document}

您有此输出(如果取消注释以 % 开头的行,您还会得到一个带有网格的漂亮背景):

在此处输入图片描述

在此处输入图片描述

答案2

添加参数xmin, ymin, xmax, ymax

\begin{center}
\begin{tikzpicture}
\begin{axis}[
    axis lines = left,
    xlabel = \(x\),
    ylabel = {\(f(x)\)},
    xmin=-15,
    xmax=15,
    ymin=-15,
    ymax=15]

\addplot [
    domain=-10:10, 
    samples=100, 
    color=red,
]
{x^3 - 3 * x^2 + 1};
\addlegendentry{\(x^3 - 3x^2 +1\)}


\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

在此处输入图片描述

相关内容