5

I want to draw like this picture enter image description here

I tried.

First way (I got the correct result)

\documentclass[12pt,border=1mm]{standalone} 
\usepackage{fouriernc} 
\usepackage{pgfplots} 
\pgfplotsset{compat=1.16} 
\begin{document} 
\begin{tikzpicture}[>=stealth,declare function={ 
xmin=-1;xmax=10;ymin=-3;ymax=2;}] 
\draw[gray!30] (xmin,ymin) grid (xmax,ymax); % grid 
\draw[->, thick] (xmin,0)--(xmax,0) node [below left]{$x$}; 
\draw[->,thick] (0,ymin)--(0,ymax) node [below left]{$y$}; 
\node[below right] at (0, 0) {$O$}; 
\foreach \p/\g in {1/-90,2/-45,3/-90,4/90,5/-90,6/-45,7/-90,8/-90,9/-90 }\draw(\p,0)node[shift={(\g:.3)},scale=1]{$\p$}--+(0,.05)--+(0,-.05); 
\foreach \p/\g in {-2/180,1/180}\draw(0,\p)node[shift={(\g:.3)},scale=1]{$\p$}--+(0,.05)--+(0,-.05); 
\draw[very thick] (2,0)
arc [start angle=0, end angle=180, radius=1];
\draw[very thick] (6,0)
arc [start angle=0, end angle=-180, radius=2];
\draw[thick] (6,0) -- (9,1);
\foreach \Y in {(2,0),(4,-2),(6,0),(9,1)} \fill \Y circle(2pt); 
\draw[dashed] (9,0) |- (0,1);
\draw[dashed] (4,0) |- (0,-2);
\end{tikzpicture} 
\end{document}

Second way (I didn't get the correct result)

\documentclass[12pt,border=1mm]{standalone} 
\usepackage{fouriernc} 
\usepackage{pgfplots} 
\pgfplotsset{compat=1.16}
\begin{document} 
\begin{tikzpicture}[>=stealth,declare function={ 
xmin=-1;xmax=10;ymin=-3;ymax=2;
f(\x)=sqrt(2*\x- \x*\x);
g(\x)=-sqrt(-12 + 8*\x - \x*\x);
}] 
%\draw[gray!30] (xmin,ymin) grid (xmax,ymax); % grid 
\draw[->, thick] (xmin,0)--(xmax,0) node [below left]{$x$}; 
\draw[->,thick] (0,ymin)--(0,ymax) node [below left]{$y$}; 
\node[below right] at (0, 0) {$O$}; 
\foreach \p/\g in {1/-90,2/-45,3/-90,4/90,5/-90,6/-45,7/-90,8/-90,9/-90 }\draw(\p,0)node[shift={(\g:.3)},scale=1]{$\p$}--+(0,.05)--+(0,-.05); 
\foreach \p/\g in {-2/180,1/180}\draw(0,\p)node[shift={(\g:.3)},scale=1]{$\p$}--+(0,.05)--+(0,-.05); 
\draw[thick] (6,0) -- (9,1);
%\foreach \Y in {(2,0),(4,-2),(6,0),(9,1)} \fill \Y circle(2pt); 
\draw[dashed] (9,0) |- (0,1);
\draw[dashed] (4,0) |- (0,-2);
\draw[very thick,blue,smooth,samples=250,domain=0:2] plot(\x,{f(\x)});
\draw[very thick,blue,smooth,samples=250,domain=2:6] plot(\x,{g(\x)});  
\end{tikzpicture} 
\end{document}

How can I get the correct result with second way? enter image description here

3
  • 2
    Why at all do you want to get the result with the second way?
    – user156344
    May 22, 2019 at 5:11
  • @JouleV We can easy to see the equations of two curves. Therefore, I tried with the second way. May 22, 2019 at 5:22
  • Do you think the readers will be able to view the equations of the curves? If they don't, it is not necessary, because of course the owner of the file clearly has to know the equations of these curves. And, obviously, the readers have no means to know the equation except by reading the source themselves. Also, I don't think there will be any person who reads the source without sufficient information. As for the curve, this can be said to be a drawback of TikZ plot for functions which are defined on only a limited domain.
    – user156344
    May 22, 2019 at 8:31

2 Answers 2

2

According to this awesome answer you need to put plot function instead of only add plot.

I have tried and I could not find a way to use plot function within f(\x) or g(\x), so I deleted them and replaced by their definitions that were in declare function to get your first way (btw I do not know why you forget to add blue color, the black points and grid background):

\documentclass[12pt,border=1mm]{standalone} 
\usepackage{fouriernc} 
\usepackage{pgfplots} 
\pgfplotsset{compat=1.15}
\begin{document} 
\begin{tikzpicture}[>=stealth,declare function={ 
xmin=-1;xmax=10;ymin=-3;ymax=2;
%f(\x)=sqrt(2*\x- \x*\x); % deleted
%g(\x)=-sqrt(-12 + 8*\x - \x*\x); % deleted
}] 
%\draw[gray!30] (xmin,ymin) grid (xmax,ymax); % grid 
\draw[->, thick] (xmin,0)--(xmax,0) node [below left]{$x$}; 
\draw[->,thick] (0,ymin)--(0,ymax) node [below left]{$y$}; 
\node[below right] at (0, 0) {$O$}; 
\foreach \p/\g in {1/-90,2/-45,3/-90,4/90,5/-90,6/-45,7/-90,8/-90,9/-90 }\draw(\p,0)node[shift={(\g:.3)},scale=1]{$\p$}--+(0,.05)--+(0,-.05); 
\foreach \p/\g in {-2/180,1/180}\draw(0,\p)node[shift={(\g:.3)},scale=1]{$\p$}--+(0,.05)--+(0,-.05); 
\draw[thick] (6,0) -- (9,1);
%\foreach \Y in {(2,0),(4,-2),(6,0),(9,1)} \fill \Y circle(2pt); 
\draw[dashed] (9,0) |- (0,1);
\draw[dashed] (4,0) |- (0,-2);
\draw[very thick,blue,smooth,samples=250,domain=0:2] plot function{sqrt(2*x- x*x)};
\draw[very thick,blue,smooth,samples=250,domain=2:6] plot function{-sqrt(-12 + 8*x - x*x)};  
\end{tikzpicture} 
\end{document}

Output

1
  • I don't understand why, I don't get the curves. May 22, 2019 at 10:18
1

I do not know why this happens (seems that this has to do with the conversion between cm and pt and some numerical errors) and manoooh's answer is the cleaner way to go. This is just in case you do not have gnuplot installed. Then you can just "continue" the functions in such a way that they remain well-defined and plot over a slightly longer interval.

\documentclass[12pt,border=1mm]{standalone} 
\usepackage{fouriernc} 
\usepackage{pgfplots} 
\pgfplotsset{compat=1.16}
\begin{document} 
\begin{tikzpicture}[>=stealth,declare function={ 
xmin=-1;xmax=10;ymin=-3;ymax=2;
f(\x)=ifthenelse(2*\x- \x*\x>0,sqrt(abs(2*\x- \x*\x)),0);
g(\x)=-ifthenelse(-12 + 8*\x - \x*\x>0,sqrt(abs(-12 + 8*\x - \x*\x)),0);
}] 
%\draw[gray!30] (xmin,ymin) grid (xmax,ymax); % grid 
\draw[->, thick] (xmin,0)--(xmax,0) node [below left]{$x$}; 
\draw[->,thick] (0,ymin)--(0,ymax) node [below left]{$y$}; 
\node[below right] at (0, 0) {$O$}; 
\foreach \p/\g in {1/-90,2/-45,3/-90,4/90,5/-90,6/-45,7/-90,8/-90,9/-90 }\draw(\p,0)node[shift={(\g:.3)},scale=1]{$\p$}--+(0,.05)--+(0,-.05); 
\foreach \p/\g in {-2/180,1/180}\draw(0,\p)node[shift={(\g:.3)},scale=1]{$\p$}--+(0,.05)--+(0,-.05); 
\draw[thick] (6,0) -- (9,1);
%\foreach \Y in {(2,0),(4,-2),(6,0),(9,1)} \fill \Y circle(2pt); 
\draw[dashed] (9,0) |- (0,1);
\draw[dashed] (4,0) |- (0,-2);
\draw[very thick,blue] plot[smooth,samples=250,domain=0:2.003](\x,{f(\x)});
\draw[very thick,blue] plot[smooth,samples=250,domain=2:6.003](\x,{g(\x)});  
\end{tikzpicture} 
\end{document}

enter image description here

You must log in to answer this question.

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