0

I tried to plot this graph using tikz and pgfplots. The code compiled just fine, but I got a peculiar result that I can't explain. My code.

\documentclass{article}

\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[xlabel=$x$,ylabel=$y$,
xmin=-6,xmax=6,ymin=-6,ymax=6, axis lines=center, axis equal]

\addplot[domain=-6:6,color=blue,samples=400]({sin(\x^2+\y^2)},{cos(\x*\y r)});

\end{axis}
\end{tikzpicture}

\end{document}

This is the riddle I was presented with.

enter image description here

The shape my code produced (a line, strangely enough) perplexes me. I truly have no idea what I did wrong. Is there any internal process at fault? Can you please modify my code in such a way that the desired plot will actually show up? I'd be grateful if you (kind users who already mastered tikz and pgfplots) would help me. Thank you in advance.

7
  • probably better to give the direct link to math.stackexchange.com/questions/713873/very-interesting-graph rather than rely on google search Mar 30, 2017 at 15:14
  • I'm assuming you mean math.stackexchange.com/questions/713873/very-interesting-graph. You can't plot implicit functions directly in pgfplots like that. Try for example tex.stackexchange.com/questions/18359/… Mar 30, 2017 at 15:16
  • I actually considered this, but at the end it isn't vital to the question. I will consider your suggestion again, though. Please help if you can. I'd like to observe the beauty of pgfplots.
    – God bless
    Mar 30, 2017 at 15:16
  • Huh? What did you consider that you don't think is vital to the question? It is certainly vital that pgfplots cannot, as far as I know, plot implicit functions they way you're trying. Mar 30, 2017 at 17:28
  • I considered putting the link directly to the Stack Exchange mathematics post and not simply to Google images not vital to my post, and NOT your information that states pgfplots can't do the desired work. I think you misunderstood me.
    – God bless
    Mar 30, 2017 at 17:32

1 Answer 1

2

I have seen this graph when I saw an introductory article on the small application GrafEq. The ideal behind is to compute the LHS and RHS using interval arithmetic. We cannot (at least it is pretty difficult to) do interval arithmetic in TeX. But we have the following for free

\documentclass[tikz]{standalone}

\pgfdeclarefunctionalshading{interesting-1}{\pgfpoint{-25bp}{-25bp}}{\pgfpoint{25bp}{25bp}}{}{
    3 mul exch 3 mul exch % x y
    2 copy                % x y x y
    dup mul exch dup mul  % x y y² x²
    add sin               % x y sin(x²+y²)
    3 1 roll              % sin(x²+y²) x y
    mul cos               % sin(x²+y²) cos(xy)
    le                    % < or not <
    {0 0 0}{1 1 1}ifelse  % black or white
}
\pgfdeclarefunctionalshading{interesting-2}{\pgfpoint{-25bp}{-25bp}}{\pgfpoint{25bp}{25bp}}{}{
    3 mul exch 3 mul exch % x y
    2 copy                % x y x y
    dup mul exch dup mul  % x y y² x²
    add sin               % x y sin(x²+y²)
    3 1 roll              % sin(x²+y²) x y
    mul cos               % sin(x²+y²) cos(xy)
    sub dup mul           % (L-R)²
    .01 le                % <.01 or not <.01
    {0 0 0}{1 1 1}ifelse  % black or white
}

\begin{document}
    \tikz\path[shading=interesting-1](-10,-10)rectangle(10,10);
    \tikz\path[shading=interesting-2](-10,-10)rectangle(10,10);
\end{document}

OR GNUPLOT

\documentclass[tikz]{standalone}
    \usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[view={0}{90}]
            \addplot3[domain=-40:40,samples=100,contour gnuplot={levels={0},labels=false}]
                {sin(x^2+y^2)-cos(x*y)};
        \end{axis}
    \end{tikzpicture}
\end{document}

Choose what you love and love what you choose.

10
  • Um, thank you for your wonderful answer, I'm very grateful for it. Though I must say the result I got was not what you posted in your answer. Rather than getting a smooth plot, I have a bunch of pixels that kind of depict the general structure of the plot, but are utterly a mess. Can you help me with that? And also, I'd like to ask you to present an answer that can be attached to a mathematical paper (axis-like style). Thank you in advance.
    – God bless
    Mar 30, 2017 at 18:21
  • @GregorPerčič I am afraid there is no short answer to your question. Functional shading is a very hard-core feature of PDF. If your PDF viewer cannot render this, you can only try with another viewer. Similar situation will happen if you submit this to a publisher. So a painless way is to attach a high-resolution raster image.
    – Symbol 1
    Mar 30, 2017 at 18:40
  • I checked my produced PDF and it produces the expected result! Thank you so much (you get a tick for your effort :)). Though I would like to know how to plot such a graph with gnuplot and pgfplot.
    – God bless
    Mar 30, 2017 at 18:46
  • In the pgfplots manual page 151 it uses gnuplot to plot the contour of a given function (exp(0-x^2-y^2)). I think you should try that. (I do not have gnuplot installed; sorry.)
    – Symbol 1
    Mar 30, 2017 at 18:53
  • Could you provide a link? There are multiple pgfplots manuals.
    – God bless
    Mar 30, 2017 at 19:04

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.