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
    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 '21 at 08:54
  • @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. – NotStanding with GoGotaHome Jul 27 '21 at 09:00

2 Answers2

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-5xx -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

Rmano
  • 40,848
  • 3
  • 64
  • 125
  • 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? – NotStanding with GoGotaHome Jul 27 '21 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 https://tex.stackexchange.com/questions/380019/how-to-find-the-documentation-for-a-package – Rmano Jul 27 '21 at 11:04
  • will this work on miktex CLI? https://tex.stackexchange.com/questions/380019/how-to-find-the-documentation-for-a-package#:~:text=For%20example%2C%20to,texdoc%20pgf – NotStanding with GoGotaHome Jul 27 '21 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}

SebGlav
  • 19,186