如何绘制阶乘函数?

如何绘制阶乘函数?

我对 tikz 和 pgf 还很陌生。我正在绘制 Big O 时间复杂度,在查看了足够多的示例后,我能够创建我想要的图表,但阶乘除外。简单地绘制 x!会创建这个奇怪的阶梯图。我希望​​它是一条平滑的曲线。我发现这个问题其答案是使用semilogyaxis。但是简单地切换到它并没有帮助,阶乘图看起来是一样的。我还尝试按照问题中的示例答案绘制一个全新的图,它确实重叠了,但阶乘图看起来不正确,我认为这与对数 y 轴有关,我不确定如何调整坐标。我只需要类似的东西,f(x) = x!它应该会生成如下所示的图形:

所需阶乘图

以下是我目前所拥有的 MWE:

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[utf8]{inputenc}

\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    grid = major,
    clip = true,
    ticks = none,
    width=0.8\textwidth,
    height=0.6\textwidth,
    every axis plot/.append style={very thick},
    axis line style = ultra thick,
    clip mode=individual,
    restrict y to domain=0:10,
    restrict x to domain=0:10,
    axis x line = left,
    axis y line = left,
    domain = 0.00:10,
    xmin = 0,
    xmax = 11,
    ymin = 0,
    ymax = 11,
    xlabel = n,
    ylabel = no. of operations,
    xlabel style = {at={(axis description cs:0.5,-0.1)},anchor=south},
    ylabel style = {at={(axis description cs:-0.08,0.5)},anchor=north},
    label style = {font=\LARGE\bf},
  ]
\addplot [
    samples=100, 
    color=red,
]
{x^2}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n^2)$};
\addplot [
    samples=100, 
    color=blue,
]
{x}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n)$};
\addplot [
    samples=100, 
    color=orange,
]
{log2 x}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(\log{}n)$};
\addplot [
    samples=100, 
    color=black,
]
{x*(log2 x)}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n\log{}n)$};
\addplot [
    samples=100, 
    color=magenta,
]
{1}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(1)$};
\addplot [
    samples=100, 
    color=cyan,
]
{x^3}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n^3)$};

%Creates stair-step like plot
\addplot [
    samples=100, 
    color=green,
]{x!}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n!)$};
\end{axis}
\end{tikzpicture}
\end{document}

绘制如下图所示:

MWE 的输出

答案1

我重新创建了@haver 解决方案https://tex.stackexchange.com/a/520121/8650带有 OP 代码的 Gamma 函数 - 帮助人们搜索连续阶乘。真正的阶乘仅针对整数定义 - 请参阅https://en.wikipedia.org/wiki/Factorialx! = Γ(x + 1)

该解决方案gnuplot需要--shell-escape

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[utf8]{inputenc}

\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    grid = major,
    clip = true,
    ticks = none,
    width=0.8\textwidth,
    height=0.6\textwidth,
    every axis plot/.append style={very thick},
    axis line style = ultra thick,
    clip mode=individual,
    restrict y to domain=0:10,
    restrict x to domain=0:10,
    axis x line = left,
    axis y line = left,
    domain = 0.00:10,
    xmin = 0,
    xmax = 11,
    ymin = 0,
    ymax = 11,
    xlabel = n,
    ylabel = no. of operations,
    xlabel style = {at={(axis description cs:0.5,-0.1)},anchor=south},
    ylabel style = {at={(axis description cs:-0.08,0.5)},anchor=north},
    label style = {font=\LARGE\bf},
  ]
\addplot [
    samples=100, 
    color=red,
]
{x^2}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n^2)$};
\addplot [
    samples=100, 
    color=blue,
]
{x}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n)$};
\addplot [
    samples=100, 
    color=orange,
]
{log2 x}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(\log{}n)$};
\addplot [
    samples=100, 
    color=black,
]
{x*(log2 x)}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n\log{}n)$};
\addplot [
    samples=100, 
    color=magenta,
]
{1}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(1)$};
\addplot [
    samples=100, 
    color=cyan,
]
{x^3}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n^3)$};

%Creates stair-step like plot
\addplot [
    samples=100, 
    color=green,
]{x!}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n!)$};

\addplot [
  samples=100,
  color=green, 
] gnuplot{gamma(x+1)} node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n!)$};

\end{axis}
\end{tikzpicture}
\end{document}

包含 Gamme(x+1) 的图

相关内容