5

Hello I want to plot a graph for of n! since I'm working with O(n!) time complexity. How can I make this look smooth? Preferably I want to show the marks. I tried doing plotting the first 10 factorials. If there is better way to display operations per element that would be helpful too.

\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}   

:(

Stefan Pinnow
  • 29,535
J.Kirk.
  • 153

3 Answers3

6

Perhaps you'd like to use semilogyaxis, for example:

semilogyaxis demonstration

\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}
cmhughes
  • 100,947
5

As mentioned in the other answer you might want to use a semilogarithmic axis. Using the FFI of LuaJITTeX (and LuaTeX ≥ 1.0.3), you can also plot the Gamma function alongside, as you wanted to have a "smooth factorial function".

Also I corrected your typo in 9! (it's 362880 instead of 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}

enter image description here

Henri Menke
  • 109,596
3

As cmhughes already stated in his answer, the key is to use a logarithmic y axis.

This answer is just to show how else you could have created your graph by calculating the values instead of giving them explicitly. In contrast to the answer of Henri Menke here the values are only calculated for the integer x values, but therefor it can be done by LaTeX itself, so without the need of using FFI of LuaJITTeX.

% 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}

image showing the result of above code

Stefan Pinnow
  • 29,535