7

Hello wonderful people,

TikZ doesn't seem to display the function it should be, I might be missing a dumb point, but here's the problem:

First, let's take a look at this marvellous function: Plot with GeoGebra polynomial, arousing curves, cuts the x-axis in (almost) integer points, this function just seems too good to be true.

Now, look at this smooth code :

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \draw [gray,dotted] (-6,-5) grid (5,5) ;
  \draw[->,>=latex] (-6,0) -- (5,0) node[below]{$x$} ;
  \draw[->,>=latex] (0,-5) -- (0,5) node[left]{$y$} ;

  \draw [domain=-5:4,samples=200] plot (\x,-0.0012*\x^5-0.0131*\x^4+0.0893*\x^3+0.3988*\x^2-0.5881*\x-0.8857) ;
\end{tikzpicture}

\end{document}

simple, efficient, should do the work and show us this gorgeous function we've been talking about for minutes now.

But, alas! Look at the disastrous result it produces: Plot with TikZ

Does anyone know why TikZ would want to crush my dreams of happiness like this? A "funny" thing is that it seems to be ok with the positive values of x(?)

Thanks a lot!

1 Answer 1

8

I am not quite sure why, but for some reason the even powers seem to be acting weird. When using the function as follows, the graph looks fine:

\begin{tikzpicture}
  \draw [gray,dotted] (-6,-5) grid (5,5) ;
  \draw[->,>=latex] (-6,0) -- (5,0) node[below]{$x$} ;
  \draw[->,>=latex] (0,-5) -- (0,5) node[left]{$y$} ;

  \draw [domain=-5:4,samples=200] plot (\x,-0.0012*\x^5-0.0131*\x*\x*\x*\x+0.0893*\x^3+0.3988*\x*\x-0.5881*\x-0.8857) ;
\end{tikzpicture}

EDIT: fixed it using the following post: How can I work around this TikZ bug: (\x)^2 and \x^2 produce different results in TikZ plot?. Using the following code fixes it completely.

\begin{tikzpicture}
  \draw [gray,dotted] (-6,-5) grid (5,5) ;
  \draw[->,>=latex] (-6,0) -- (5,0) node[below]{$x$} ;
  \draw[->,>=latex] (0,-5) -- (0,5) node[left]{$y$} ;

  \draw [domain=-5:4,samples=200] plot (\x,{-0.0012*\x^5-0.0131*(\x)^4+0.0893*\x^3+0.3988*(\x)^2-0.5881*\x-0.8857}) ;
\end{tikzpicture}
4
  • Great, thanks a lot! I was not aware of this problem at all. Have a nice day and a nice life!
    – fbatch
    Commented Jul 23, 2020 at 19:54
  • 1
    In my opinion, it's not a bug. You have to get to grips with the fact that TeX is a macro expansion language, so if \x is defined as -1, \x^2 will get expanded (and not calculated --- expanded) to -1^2 which, using the normal precedence, is -1. It's funny at first, but you'll have exactly the same problem in the C language preprocessor. And in m4...
    – Rmano
    Commented Jul 23, 2020 at 20:23
  • 1
    I agree, I posted the answer so OP could go on with his life, found the post as to why this is the reason thereafter. There it is also explained why this is not a bug :)
    – G. de Man
    Commented Jul 23, 2020 at 20:24
  • 1
    @G.deMan nice (+1!) , really the comment was for the OP. Is a kind of philosophical concept... but I have for example a lot of students that have problems with this and C... like #define A 1+2 and #define B A*3 ;-)
    – Rmano
    Commented Jul 23, 2020 at 20:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .