0

Could you spot the error? I'm trying to plot a function with two parameters, the initial condition of a differential equation. The same pattern works for simple functions.

\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usetikzlibrary{calc,patterns}
\usetikzlibrary{arrows.meta}
\tikzset{arrow/.style={-{Stealth[length=2mm, width=2mm, sep=0.5pt]}}}

\begin{document}
\begin{center}
\begin{tikzpicture}[scale=2.5,
declare function={ fctP(\x,\a,\b) = (sqrt(\b)+(\x-\a)/2)**2;
                   fctN(\x,\a,\b) = - (sqrt(-\b)+(\a-\x)/2)**2; }]
  \draw [arrow] (-2.2,0) -- (2.2,0) node [below]  {$x$};
  \draw [arrow] (0,-1.2) -- (0,1.2) node [right] {$y$};

  \draw[samples=200,domain=1:3,smooth,variable=\x,thick]  plot ({\x},{fctP(\x,3,1)}) node[above right] {$?$};  
\end{tikzpicture}
\end{center}

\end{document}

1 Answer 1

2

The main problem is that you are using ** instead of ^ for pgfmath exponentiation. I also adjusted the axes ranges and removed a few things that you don't use. I'm leaving the definition of fctN() on purpose for the example of how to define several pgfmath functions at once, but it isn't used in the code.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\tikzset{arrow/.style={-{Stealth[length=2mm, width=2mm, sep=0.5pt]}}}

\begin{document}

\begin{tikzpicture}[scale=2.5,
                    declare function={
                      fctP(\x,\a,\b) = (sqrt(\b)+(\x-\a)/2)^2;
                      fctN(\x,\a,\b) = - (sqrt(-\b)+(\a-\x)/2)^2; }]
  \def\myxMax{3.0}
  \draw [arrow] (-0.2,0) -- (\myxMax,0) node [below]  {$x$};
  \draw [arrow] (0,-0.2) -- (0,1.2) node [right] {$y$};

  \draw[samples=200, domain=1:\myxMax, smooth, variable=\x, thick]
    plot ({\x}, {fctP(\x,3,1)}) node[above right] {$?$};
\end{tikzpicture}

\end{document}

enter image description here

Note: the \usetkzobj{all} I removed is also causing a problem on my computer (Command \tkzMarkRightAngle already defined). I don't know if it is a tkz-euclide bug or a problem with my installation. Anyway, since tkz-euclide isn't used in your example, removing this line (as well as \usepackage{tkz-euclide}) solves the problem. I sent a mail to the author of tkz-euclide with this minimal code:

\documentclass{article}
\usepackage{tikz}
\usepackage{tkz-euclide}
%\usetkzobj{all} you can remove this line with the new version

\begin{document}

\end{document}

No need to bother him more. :)

Completed plot from the OP (@PeptideChain)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\tikzset{
  bullet/.style={circle, fill, minimum size=4pt, inner sep=0pt, outer sep=0pt},
  arrow/.style={-{Stealth[length=2mm, width=2mm, sep=0.5pt]}}
}

\begin{document}

\begin{tikzpicture}[scale=1.5,
                    declare function={
                      fctP(\x,\a,\b) = (sqrt(\b)+(\x-\a)/2)^2;
                      fctN(\x,\a,\b) = - (sqrt(-\b)+(\a-\x)/2)^2; }]
  \draw [arrow] (-2.4,0) -- (3.4,0) node [below] {$x$};
  \draw [arrow] (0,-1.4) -- (0,1.4) node [right] {$y$};

  \draw[samples=200, domain=1:3.2, smooth, variable=\x, thick]
    plot ({\x}, {fctP(\x,3,1)}) node[above right] {$y(x)$};
  \draw[samples=200, domain=0:1, smooth, variable=\x, thick]
    plot ({\x},{0});
  \draw[samples=200, domain=-2.2:0, smooth, variable=\x, thick]
    plot ({\x}, {fctN(\x,-2,-1)});

  \draw (1,0)  -- node [below] {$1$}  (1,-0.03);
  \draw (2,0)  -- node [below] {$2$}  (2,-0.03);
  \draw (3,0)  -- node [below] {$3$}  (3,-0.03);
  \draw (-1,0) -- node [above] {$-1$} (-1,-0.03);
  \draw (-2,0) -- node [above] {$-2$} (-2,-0.03);
  \draw (0,1)  -- node [left]  {$1$}  (-0.03,1);
  \draw (0,-1) -- node [right] {$-1$} (-0.03,-1);

  \node[bullet, minimum size=3pt] at (3,1) {};
  \node[bullet, minimum size=3pt] at (-2,-1) {};
  \draw [dotted] (0,1)  -- (3,1);
  \draw [dotted] (3,0)  -- (3,1);
  \draw [dotted] (-2,0) -- (-2,-1);
  \draw [dotted] (0,-1) -- (-2,-1);
\end{tikzpicture}

\end{document}

completed plot

8
  • Thank you very much. The problem is that the error message of Tikz says just nothing. Jan 30, 2020 at 12:26
  • Yes, debugging is not always easy, especially with LaTeX. That is why MWEs are so useful. :)
    – frougon
    Jan 30, 2020 at 12:27
  • May I edit your post and replace your code with my complete code adjusted with your correction? Jan 30, 2020 at 12:27
  • How so? What is missing in my code?
    – frougon
    Jan 30, 2020 at 12:27
  • Nothing is wrong in your code, my model was not finished, so it look prettier (second part of the curve, tiks, label) Jan 30, 2020 at 12:29

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.