绘制阶乘函数

绘制阶乘函数

你好,我想绘制 n! 的图表,因为我正在处理 O(n!) 时间复杂度。我怎样才能让它看起来流畅?最好能显示标记。我尝试绘制前 10 个阶乘。如果有更好的方法来显示每个元素的操作,那也会很有帮助。

\begin{tikzpicture}
\begin{axis}[
  title={O(n!) Scaling data input},
  xlabel={Elements},
  ylabel={Operations},
  xmin=0, xmax=25,
  ymin=0, ymax=3628800,
  xtick={0,1,2,3,4,5,6,7,8,8,10},
  ytick={0,1,2,6,24,120,720,5040,40320,632880,3628800},
  legend pos=north west,
  ymajorgrids=true,
  grid style=dashed,
  ]  
\addplot[
  color=blue,
  mark=square,
  ]
coordinates {
   (0,1)(1,1)(2,2)(3,6)(4,24)(5,120)(6,720)(7,5040)(8,40320)(9,632880)(10,3628800)
};
\legend{O(n!)}

\end{axis}
\end{tikzpicture}   

:(

答案1

也许您想使用semilogyaxis,例如:

半逻辑轴演示

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{semilogyaxis}[
            title={O(n!) Scaling data input},
            xlabel={Elements},
            ylabel={Operations},
            xmin=0, xmax=10,
            ymin=0, ymax=3628800,
            xtick={0,1,2,3,4,5,6,7,8,8,10},
            ytick={0,1,2,6,24,120,720,5040,40320,632880,3628800},
            legend pos=north west,
            ymajorgrids=true,
            grid style=dashed,
        ]
        \addplot[
            color=blue,
            only marks,
            mark=square,
        ]
        coordinates {
                (0,1)(1,1)(2,2)(3,6)(4,24)(5,120)(6,720)(7,5040)(8,40320)(9,632880)(10,3628800)
            };
        \legend{O(n!)}
    \end{semilogyaxis}
\end{tikzpicture}
\end{document}

答案2

正如另一个答案中提到的,您可能想要使用半对数轴。使用 LuaJITTeX 的 FFI(和 LuaTeX ≥ 1.0.3),您还可以绘制 Gamma 函数,因为您想要一个“平滑的阶乘函数”。

另外,我还纠正了你的 9 中的拼写错误!(应该是 362880,而不是 632880)。

\documentclass{article}

\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\directlua{
  ffi=require("ffi")
  ffi.cdef[[
  double tgamma(double x);
  ]]
}

\pgfmathdeclarefunction{Gamma}{1}{%
  \edef\pgfmathresult{%
    \directlua{tex.print(ffi.C.tgamma(\pgfmathfloatvalueof{#1}))}%
  }%
}

\begin{document}

\begin{tikzpicture}
  \begin{semilogyaxis}[
    title={$O(n!)$ Scaling data input},
    xlabel={Elements},
    ylabel={Operations},
    domain=0:10,
    ytick={0,1,2,6,24,120,720,5040,40320,362880,3628800},
    log ticks with fixed point,
    legend pos=north west,
    ymajorgrids=true,
    grid style=dashed,
    ]  
    \addplot+[mark=square,only marks] coordinates {
      (0,1) (1,1) (2,2) (3,6) (4,24) (5,120) (6,720) (7,5040)
      (8,40320) (9,362880) (10,3628800)
    };
    \addlegendentry{$O(n!)$}

    \addplot+[no marks,samples=100] {Gamma(x+1)};
    \addlegendentry{$\Gamma(n+1)$}

  \end{semilogyaxis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

正如 cmhughes 已经指出的那样他的回答,关键是使用对数 y 轴。

这个答案只是为了展示你还可以用什么其他方式来创建你的图表计算而不是明确给出这些值。与以下答案相反亨利·孟克这里仅针对整数 x 值进行计算,但因此可以由 LaTeX 本身完成,而不需要使用 LuaJITTeX 的 FFI。

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % use this `compat` level to use the advanced positioning features of the axis labels
    \pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
    % use log y-axis
    \begin{semilogyaxis}[
        title={$O(n!)$ Scaling data input},
        xlabel={Elements},
        ylabel={Operations},
        % so you don't have to state the values manually
        % (the data of the first given plot (only) are used)
        xtick=data,
        ytick=data,
        % you can also use ...
        enlargelimits=false,
%        % ... instead of giving the limits explicitly
%        xmin=0, xmax=10,
%        ymin=0, ymax=3628800,
        legend pos=north west,
        legend cell align=left,
        ymajorgrids=true,
        grid style=dashed,
        log ticks with fixed point,
        only marks,
    ]
        % automatic calculation of $n!$
        % (I placed it before the "manual" variant so that the right yticks are shown)
        \addplot [
            red,
            mark=*,
            mark size=1.5pt,
            % state the domain where the values should be calculated
            domain=0:10,
            % state the number of samples that should be calculated
            samples=11,
        ] {x!};
        % typo at $9!$ (switched first two digits)
        \addplot [color=blue,mark=square] coordinates {
            (0,1)(1,1)(2,2)(3,6)(4,24)(5,120)(6,720)(7,5040)(8,40320)(9,632880)(10,3628800)
        };

        \legend{
            $O(n!)$ (calculated),
            $O(n!)$ (by hand),
        }
    \end{semilogyaxis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容