2

I need to draw multiple curves illustrating run time but some of them aren't working besides I don't know how to name these curves in the figure .

\documentclass{standalone}
\usepackage{pgfplots}
\tikzset{>=stealth}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    xmin=0,xmax=10,
    ymin=0,ymax=10,
    axis lines=center,
    axis line style=->]
    \addplot[->] expression[domain=0:10,samples=100]{x^(2)} ; 
  \addplot[->] expression[domain=0:10,samples=100]{\sqrt(x)} ;  
     \addplot[->] expression[domain=0:10,samples=100]{x!} ; 

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

I still need to draw constant, logarithmic and exponential but for now how do I get the code to work and how do I get to have the names next to each curve? (Extra material explaining all of this would also be helpful)

cgnieder
  • 66,645
  • Quick solution: \sqrt(x) = x^(1/2). – ebosi Jun 21 '17 at 15:27
  • @ebo, the \ in \sqrt is the problem, which is not needed. So simply writing sqrt(x) is sufficient. But this only solved one problem ... – Stefan Pinnow Jun 21 '17 at 15:28
  • You could start by changing '\sqrt' to 'sqrt'. If you want to add labels, you could use the legend command, alternatively you could add a node, e.g.: addplot{sqrt(x)} node {y=...}; – cmhughes Jun 21 '17 at 15:29
  • 1
    ... The second problem is that 10! results in a number of the magnitude of 10^6 which is much bigger than the next smaller number of 10^2 = 100 and thus you will get an "dimension too large" error. And even if it would be plotted, you won't see the 100 compared to the 10^6. So I suggest to simply reduce the domain of the faculty to 6 or 7. – Stefan Pinnow Jun 21 '17 at 15:31

3 Answers3

4

You can see here what you need to know to start with pgfplots:

\documentclass{standalone}
\usepackage{pgfplots}
\tikzset{>=stealth}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    xmin=0,xmax=6.9,
    ymin=0,ymax=30,
    axis lines=center,
    axis line style=->]
    \addplot[dashed,red] expression[domain=0:5,samples=100]{(x)^2} ; \addlegendentry{$x^2$}
  \addplot[-,green] expression[domain=0:5,samples=100]{sqrt(x)} ;  \addlegendentry{$\sqrt{x}$}
     \addplot[dotted,blue] expression[domain=0:5,samples=100]{x!} ; \addlegendentry{$x!$}

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

As @Stefan Pinnow said the domain was too big for the factorial and I had to change it...

The result is here:

enter image description here

koleygr
  • 20,105
4

Unfortunately you are not very precise in your question what you really want to achieve, so in my answer I am assuming that you want to stick to the given axis limits.

As already stated in the comments below the question, without changing "something" and only correcting \sqrt(x) with sqrt(x) you would get a "dimension to large" error because of the x! function yielding a value of the magnitude 10^6, compared to the axis limit of 10.

Also it is not clear how you want to "name the curves". If you meant adding a legend, than have a look at the answer of koleygr. If you wanted to add names at the plots itself, that is what I am presenting here. I also use the "hard" case here, adding the names "at the end of the plot", i.e. at pos=1. If the labels should appear somewhere "in the middle of the plot", feel free to add corresponding pos statements to the label nodes.

For more details please have a look at the comments in the code. (If still something should be unclear, please don't hesitate to ask.)

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        /tikz/>=stealth,
        % use this `compat' level or higher to use Lua for calculations
        % (this is not required though)
        compat=1.12,
        % ---------------------------------------------------------------------
        % please skip this block for now
        % (copied from <https://tex.stackexchange.com/a/375348>)
        /pgf/declare function={
            % declare the main function(s)
            f(\x) = sqrt(\x);
            % state (or calculate) the lower and upper boundaries (the domain values)
            lb = 0;
            ub = 10;
            %
            % ---------------
            %%% nonlinear spacing: <https://stackoverflow.com/a/39140096>
            % "non-linearity factor"
            a = 0.4;
            % function to use for the nonlinear spacing
            Y(\x) = exp(a*\x);
            % rescale to former limits
            X(\x) = (Y(\x) - Y(lb))/(Y(ub) - Y(lb)) * (ub - lb) + lb;
        },
        % ---------------------------------------------------------------------
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        % use the above defined variables here as well, so you only have to
        % change them in one place, if you would like to change these values
        xmin=lb,xmax=ub,
        ymin=0,ymax=10,
        axis lines=center,
        axis line style=->,
        % move common options here
        % also use the above defined variables here
        domain=lb:ub,
        % you don't want to show markers
        no markers,
        % show the plots "smoothly"
        smooth,
        % because the label nodes will be outside the axis "rectangle" they
        % would be clipped, so we disable clipping
        clip=false,
    ]
%        % Using these two functions have some downsides ...
%        \addplot+ [domain=0:4] {x^2};
%        \addplot {sqrt(x)};

        % Instead of increasing the number of samples "to make it look good",
        % we use nonlinear sampling instead.
        % (For more details have a look at <https://tex.stackexchange.com/a/375348>)
        \addplot ({X(x)},{f(X(x))})
            % add a label (node)
            % (the default position is at `pos=1.0`)
            node [right] {$\sqrt{x}$}
        ;

        % Because $x^2$ is the inverse function of $\sqrt{x}$ we simply
        % exchange the $x$ and $y$ arguments of the previous `\addplot'.
        % By doing that we also avoid that the function looks bad and we don't
        % have to increase the number of `samples'
        \addplot ({f(X(x))},{X(x)})
            node [above]{$x^2$}
        ;

        % Here we also use the minimalistic approach.
        \addplot+ [
            % Because values only change at integer x values, we can use
            % `const plot'.
            const plot,
            % $4!$ already yields $24$ and so we don't need a higher max
            % `domain' value.
            domain=0:4,
            % Use a number of samples that assures that also samples are at
            % integer x values to give a right result. This makes $5$ at minimum.
            samples=5,
            % So the plot stops at $y = 10$ use the following key--value.
            % (Otherwise, because we use `clip=false' the line would be drawn
            %  up to 24.)
            restrict y to domain*=0:10,
        ] {x!}
            node [above] {$x!$}
        ;
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535
1

Maybe you'd be interested in a log-log plot ?

The output

Brute plot

enter image description here

Restricted y domain

With \addplot+[mark=none,const plot,restrict y to domain=0:42] {x!};

Are displayed, only those values between exp(0)=1 and exp(42)~10^17.

I think this is enough to bring the point home about the relative growths.

Actually : setting ymax is better.

enter image description here

The code

Brute plot

\documentclass[12pt,tikz,border=0pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
  \begin{loglogaxis}
    [
      %domain=2:30,
      ymin=1,
      axis lines = center,
      samples at ={1,2,...,100},
      grid=both,
      legend entries={$y=x!$,$y=x^2$,$y=\sqrt{x}$},
      every axis legend/.append style=
      {
        at={(current axis.above origin)},
        anchor=north west, xshift=1cm, yshift=-1mm,
        draw=none,
        cells={anchor=west},
        %font=\scriptsize,
      }
    ]
    \addplot+[mark=none,const plot] {x!};
    \addplot+[mark=none] {x^2};
    \addplot+[mark=none] {sqrt(x)};
  \end{loglogaxis}
\end{tikzpicture}
\end{document}

Restricted domain

\documentclass[12pt,tikz,border=0pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
  \begin{loglogaxis}
    [
      height=13cm,
      ymin=1,
      ymax=exp(40),
      hide obscured x ticks=false,
      hide obscured y ticks=false,
      axis lines = center,
      samples at ={1,2,...,100},
      grid=both,
      legend entries={$y=x!$,$y=\exp(x)$,$y=x^2$,$y=\sqrt{x}$,$y=\ln(x)$},
      every axis legend/.append style=
      {
        at={(current axis.above origin)},
        anchor=north west, xshift=3mm, yshift=-1mm,
        draw=none,
        cells={anchor=west},
        %font=\scriptsize,
      },
    ]
    \addplot+[mark=none,const plot,] {x!}; 
    \addplot+[mark=none] {exp(x)}; 
    \addplot+[mark=none] {x^2};
    \addplot+[mark=none] {sqrt(x)};
    \addplot+[mark=none] {ln(x)}; 
  \end{loglogaxis}
\end{tikzpicture}
\end{document}
marsupilam
  • 6,383
  • How do I insert this as part of exercise 1 , when I add \section{Introduction} after \begin{document} and I write something before \end{document} the layout is off – G.Khalaf Jun 22 '17 at 16:52
  • @G.Khalaf This failure is because my code is a standalone document (a LaTeX type for pictures). If you change the first line to \documentclass{article}. Otherwise, you can compile the code above to produce a pdf file, then use \includegraphics{<theGeneratedPDF>} inside your main LaTeX file containing your exercice 1 – marsupilam Jun 22 '17 at 18:41