2

I am using the following code to generate a graph of the sqrt function.

\begin{tikzpicture}
\begin{axis}[my style]
\addplot[domain=-1:3] (x,{sqrt(\x)});
\addplot[mark=*] coordinates {(1,1)};
\addplot[mark=*] coordinates {(0,0)};
\end{axis}
\end{tikzpicture}

but I am getting a strange gap in my graph. I tried inputting the graph into the text here but I'm not sure how to get it to actually display the graph instead of the code. Any idea how to make sure the graph doesn't have that gap near the origin?

EDIT: Here is the preamble, and the new code:

\documentclass[11pt,epsfig]{article}
\usepackage{amsfonts, colonequals, amsthm, amsmath, amssymb, mathrsfs, amscd, pgfplots,tikz,multicol}
\usepgfplotslibrary{polar}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{calc,arrows}

\pgfplotsset{my style/.append style={axis x line=middle, axis y line=middle, xlabel={$x$}, ylabel={$y$}, axis equal }}

\oddsidemargin=0in
\evensidemargin=0in
\textwidth=6.3in
\topmargin=-.5in
\textheight=9in

\parindent=0in
\pagestyle{empty}

\input{notestpoints}

\begin{tikzpicture}
\begin{axis}[my style]
\addplot[domain=0:3,samples=128] (x,{sqrt(\x)});
\addplot[mark=*] coordinates {(1,1)};
\addplot[mark=*] coordinates {(0,0)};
\end{axis}
\end{tikzpicture}
6
  • 3
    What's the square root of a negative number? Please, make the example fully compilable, adding a suitable preamble.
    – egreg
    Oct 10 '17 at 21:49
  • 2
    As @egreg mentions, a better domain would certainly help. Consider also the discussion in tex.stackexchange.com/questions/310502/… Oct 10 '17 at 22:00
  • Another useful discussion: tex.stackexchange.com/questions/375337/… Oct 11 '17 at 8:46
  • Thanks for the replies. I had the domain set like that to get tikz to show the graph to the left of 0, which obviously is empty. Changing the domain to 0:3 gets rid of the gap, but creates the issue where the curve has a "kink" in it. Increasing the number of samples fixed this. Is there a way I can keep this while showing some of the coordinate plane to the left of x=0? I've edited the code to include my preamble.
    – JohnC
    Oct 11 '17 at 19:48
  • 1
    xmin=-1. Unrelated: probably better to use the geometry package to set margins etc., and the epsfig options doesn't do anything I think. Oct 11 '17 at 20:10
4

(Overly long summary of comments follows.)

Two relevant posts are:

Now, with 25 samples (the default number) between -1 and 3, you should actually have a sample at zero, possibly round off error/lack of numerical precision leads to a number right below zero instead, and so the plot point is discarded.

Changing the domain to 0:3 gives a better result, but as discussed in the second of the two posts linked to above, the sampling frequency causes a noticeable kink in the plot. Increasing the number of samples is one way of improving the output, but I would seriously consider Stefan Pinnow's answer on that post.

To have the x-axis extend below zero, there are at least two different options:

  • Set the axis limits explicitly, with xmin, xmax, ymin, ymax. One doesn't have to provide all of them, so for your case adding xmin=-1 to the axis options would work.

  • enlargelimits/enlarge x limits (or y or z instead of x). These keys are for extending one or all of the axes by a fixed or relative amount. See the manual for a description of all the options.

Finally, some unrelated comments:

  • the practice of adding packages (epsfig in this case) to the class options dates back to LaTeX 2.09 and the \documentstyle command, which is not in use today. For \documentclass, the epsfig option doesn't do anything, you get a warning LaTeX Warning: Unused global option(s): epsfig]. And anyway, the epsfig package itself is outdated, use the graphicx package and \includegraphics for EPS-files as well.

  • At least for the standard classes (article, book, report), the best way of settings margins is to use the geometry package. That's recommended over redefining lengths as you've done.

  • I would also perhaps recommend against reusing the same preamble, adding to it all the time. Instead add things as you need them. (I realize this is a matter of convenience though.)

Anyway, in the code below I removed most of the things not necessary, including most packages and libraries. If you zoom in near the origin, you can see the difference between Stefan Pinnow's method and a simple increase in the number of samples. More samples means it takes a longer time to generate the document, though it's not very noticeable with 128 samples.

output of code

\documentclass[11pt]{article}
\usepackage{pgfplots}
\usepackage{geometry}
%\usepackage{parskip} % instead of setting \parindent to zero

\geometry{
  textwidth=6.3in,
  textheight=9in
}

\pgfplotsset{
  my style/.style={
    axis lines=middle,
    xlabel={$x$},
    ylabel={$y$},
    axis equal,
    xmin=-1
  },
  /pgf/declare function={
    % https://tex.stackexchange.com/a/375348
    lb=0;ub=3;a=2;
    f(\x) = sqrt(\x);
    Y(\x) = exp(a*\x);
    X(\x) = (Y(\x) - Y(lb))/(Y(ub) - Y(lb)) * (ub - lb) + lb;
    }
}

\pagestyle{empty}

%\input{notestpoints}
\begin{document}
\begin{tikzpicture}
\begin{axis}[my style]
  \addplot[red,domain=0:3,samples=128] {f(x)}; % for plotting against x, you don't need (x,{f(x)}), {f(x)} is enough
  \addplot[domain=0:3,samples=200] ({X(x)},{f(X(x))});
  \addplot[only marks, mark=*,samples at={0,1}] {f(x)}; % instead of two separate \addplot's
\end{axis}
\end{tikzpicture}
\end{document}
1
  • Very good. +1 for you. hi.
    – Sebastiano
    Oct 12 '17 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.