2

I am trying to generate the following pgfplot:

Minimum working example

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{pgfplots}

\author{Some dude's name}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
    xlabel={x}, 
    ylabel={y}, 
    grid, 
    ticks=none
]
\addplot[blue, no marks, smooth, domain=0:1, samples=50] {25-5x -y};

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

but it throws an error multiple times:

 ! Package PGF Math Error: Unknown operator `x' or `x^' (in '25-5x^{2} -y^{2}').
See the PGF Math package documentation for explanation.
Type H <return> for immediate help.
...
l.19 ...omain=0:1, samples=50] {25-5x^{2} -y^{2}};
This error message was generated by an \errmessage
command, so I can't give any explicit help.
Pretend that you're Hercule Poirot: Examine all clues,
and deduce the truth by order and method.

In my messages/Log section, there are about 70x of the above error.

The Function that I want to Plot: function

Function details:

enter image description here enter image description here

3
  • 3
    5*x, not 5x. You can't omit the multiplication operator in pgfmath. So you can write your equation as 25-5*x*x-y*y. (Do not mix pgfmath expressions and LaTeX math language, they're completely different beasts.)
    – Rmano
    Jul 27, 2021 at 8:54
  • +1 Thank you... Jul 27, 2021 at 8:57
  • @Rmano another error:! Package pgfplots Error: Sorry, you can't use 'y' in this context. PGFPlots ex pected to sample a line, not a mesh. Please use the [mesh] option combined with [samples y>0] and [domain y!=0:0] to indicate a twodimensional input domain. See the pgfplots package documentation for explanation. Type H <return> for immediate help. ... l.19 ...h, domain=0:1, samples=50] {25-5*x*x-y*y}; This error message was generated by an \errmessage command, so I can't give any explicit help. Jul 27, 2021 at 9:00

2 Answers 2

2

Firstly, you can't omit the multiplication operation * in pgfmath expressions (this is not a LaTeX math formula!).

Secondly, you should always use a compat level with pgfplots, otherwise the behavior will be buggy (you have the following warning:

 Package pgfplots Warning: running in backwards compatibility mode (unsuitable tick labels; missing features). Consider writing \pgfplotsset{compat=1.18} into your preamble.

Finally, you are drawing a 3d-plot, so you need to use addplot3 (or similar functions, see the manual).

This works:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{pgfplots}\pgfplotsset{compat=1.18}

\author{Some dude's name}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
    xlabel={x}, 
    ylabel={y}, 
    grid, 
    ticks=none
]
\addplot3[blue, no marks, smooth, domain=0:1, samples=50] {25-5*x*x -y*y};

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

enter image description here

Notice the nice aliasing pattern due to the mesh... probably you want surf instead of smooth here. See manual at pages 124 and following: https://texdoc.org/serve/pgfplots/0#section.4.6

3
  • wonderful I got the function. But I have some questions. First, your section 4.6 link does not work. Alternative link? Secondly, what manual do i need to read? Jul 27, 2021 at 10:07
  • Works perfectly here. Anyway, you can go to texdoc.net, and given that you are using pgfplots, you should look for pgfplots manual. Just type pgfplots in the text area and choose the first link below (the small eye icon). Anyway, you probably have all the documentation locally available --- see tex.stackexchange.com/questions/380019/…
    – Rmano
    Jul 27, 2021 at 11:04
  • will this work on miktex CLI? tex.stackexchange.com/questions/380019/… Jul 27, 2021 at 11:07
0

This is not perfect, but you can see it as a starting point:

pgfplots

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

\author{Some dude's name}
\begin{document}
\begin{tikzpicture}[declare function = {
            Z(\x,\y) = 25 - \x^2 - \y^2;
        }]
\begin{axis}
[
    xlabel={x}, 
    ylabel={y}, 
    grid, 
    ticks=none,
    view={100}{20}, 
    enlargelimits,
    domain=0:5,
    samples=50, 
    restrict z to domain=0:30
    ]
\addplot3[surf] {Z(x,y)};

\end{axis}
\end{tikzpicture}
\end{document}
6
  • can you explain the things after \begin{axis}? Jul 27, 2021 at 10:01
  • The view is to let you view your shape from the desired point of view (delete it to see the effect). The restrict z is to only view the deisred part of the shape on the z axis (if you delete it, you will have more underneath).
    – SebGlav
    Jul 27, 2021 at 10:05
  • Ok. other attributes? Jul 27, 2021 at 10:08
  • I think that everything is pretty straightforward and you could learn from the code by commenting some lines and see what happens. Once again, I'm not that into pgfplots so the design is not perfect and could probably be improved.
    – SebGlav
    Jul 27, 2021 at 10:25
  • yeah. i can do that!!! Jul 27, 2021 at 10:26

You must log in to answer this question.

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