Graphics, Figures & TablesWhy TikZ kink in sqrt function?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Why TikZ kink in sqrt function?

Post by LaTexLearner »

Why does the following code produce a graph with a kink near the origin?

Also, should I be bothering to make these kinds of things with just TikZ or should I also learn the PGF package?

Code: Select all


\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\draw [<->] (0,5)--(0,-1);
\draw [<->] (-1,0)--(5,0);

\draw [red, thick, domain=0:5] plot (\x, {sqrt(\x)});

\end{tikzpicture}

\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Why TikZ kink in sqrt function?

Post by rais »

LaTexLearner wrote:Why does the following code produce a graph with a kink near the origin?
The plot function uses a finite number of plot points, called samples, AFAIK.
Said samples are initially set to 25. For more detail in the plot, simply increase its number.

Code: Select all

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\draw [<->] (0,5)--(0,-1);
\draw [<->] (-1,0)--(5,0);

\draw [red, thick, domain=0:5, samples=50] plot (\x, {sqrt(\x)});

\end{tikzpicture}

\end{document}
LaTexLearner wrote:Also, should I be bothering to make these kinds of things with just TikZ or should I also learn the PGF package?
You may want to have a look at the pgfplots package.

KR
Rainer
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Why TikZ kink in sqrt function?

Post by LaTexLearner »

rais wrote:
LaTexLearner wrote:Why does the following code produce a graph with a kink near the origin?
The plot function uses a finite number of plot points, called samples, AFAIK.
Said samples are initially set to 25. For more detail in the plot, simply increase its number.
Got it.

It does occur to me, though, that given the average amount of computing power typically available that the default number of samples should be orders of magnitude larger than 25, especially for simple functions.

Or is there a rationale behind a default of 25 that I don't know about?

LaTexLearner wrote:Also, should I be bothering to make these kinds of things with just TikZ or should I also learn the PGF package?
You may want to have a look at the pgfplots package.

KR
Rainer
Ok. Something else to do this weekend. :)
User avatar
Stefan Kottwitz
Site Admin
Posts: 10330
Joined: Mon Mar 10, 2008 9:44 pm

Why TikZ kink in sqrt function?

Post by Stefan Kottwitz »

LaTexLearner wrote:
rais wrote:You may want to have a look at the pgfplots package.
Ok. Something else to do this weekend. :)
Very good advice by Rainer! pgfplots is an excellent plotting package. You could have a look at the pgfplots example gallery to get some ideas.

Stefan
LaTeX.org admin
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Why TikZ kink in sqrt function?

Post by Johannes_B »

LaTexLearner wrote:It does occur to me, though, that given the average amount of computing power typically available that the default number of samples should be orders of magnitude larger than 25, especially for simple functions.

Are you sure? One very simple function i can think of is y=3. To draw this with a mixumum of accuraccy, i only need two points.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Why TikZ kink in sqrt function?

Post by LaTexLearner »

Johannes_B wrote:
LaTexLearner wrote:It does occur to me, though, that given the average amount of computing power typically available that the default number of samples should be orders of magnitude larger than 25, especially for simple functions.

Are you sure? One very simple function i can think of is y=3. To draw this with a mixumum of accuraccy, i only need two points.
Are you saying that there is some huge downside to having computers calculate, say, 250 points instead of 25?

If 25 points are used for both y=3 and y=sqrt(x) only one graph looks reasonable.

If 250 points are used for each of those points, both graphs look reasonable.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Why TikZ kink in sqrt function?

Post by Johannes_B »

TeX was designed in the end of the seventies to the end of the eighties, memory was a very very big thing in those times.

Even today, if you have several files and want to plot the results, it is quit possible that you fence the memory cap, still carried over. You can set it to a higher value though or use the modern LuaTeX that sets up more memory if needed during compile.

On the other hand, TeX wass designed for typesetting, not for complex calculations. With every extra sample, it will take longer to compile.

For those reasons, the tikz-library externalize was introduced. It opens up an auxiliary LaTeX run to produce a pdf, in later runs this pdf can be included very easily.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Why TikZ kink in sqrt function?

Post by LaTexLearner »

Johannes_B wrote: TeX was designed in the end of the seventies to the end of the eighties, memory was a very very big thing in those times.

...

With every extra sample, it will take longer to compile.
I see.

How much longer are we talking? It seems to me that this is the kind of calculation that can literally be completed in a millisecond. E.g. look at how fast and smooth the graphs are at Desmos.com.
Johannes_B wrote: ...
For those reasons, the tikz-library externalize was introduced. It opens up an auxiliary LaTeX run to produce a pdf, in later runs this pdf can be included very easily.
When I start needing more complex functions, I will check this out. Thanks again! :)
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Why TikZ kink in sqrt function?

Post by Johannes_B »

LaTexLearner wrote:How much longer are we talking? It seems to me that this is the kind of calculation that can literally be completed in a millisecond. E.g. look at how fast and smooth the graphs are at Desmos.com.
Hard to say, depends on the hardware. Right now, my laptop seems to have some kind of bug or virus or ... i don't know. A three-page document tooks 2 seconds to compile with pdflatex, one testrun with LuaLaTeX of a one-sife document took about 10 to twelve seconds. Clearly, that is a fault on my side and doesn't even have anything to do with pgfplots.

But speed depends on the hardware (and maybe software) you have available.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply