# tikz does not plot correctly

@Jake: this is the whole document

    \documentclass[11pt, a4paper]{book}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgflibrary{arrows}
\usepackage{pgf}
\usepackage{Tikz-exp}

\parindent=0pt

\begin{document}

\begin{tikzpicture}
\tikzset{x={(.965cm,-.258cm)}, y={(.7cm,.7cm)}, z={(0cm,1cm)},
sezioni/.style={draw=#1!60, thin, fill=#1!30, fill opacity=0.3},
scale=1.75
}
\coordinate (O) at (0,0,0) node [left] {$O$};
\draw[gray,->] (-2,0,0) -- (2,0,0) node [right, black] {$x$};
\draw[gray, ->] (0,-2,0) -- (0,2,0) node [right, black] {$y$};
\draw[gray, ->] (O) -- (0,0,3) node [left, black] {$z$};

%=====% sezioni %=====%
\foreach \x in {-1,-.85,...,1}
\draw[sezioni=teal] (\x,{sqrt(1-(\x)^2)},0)--(\x,0,{sqrt(4*(1-(\x)^2))})--(\x,{-sqrt(1-(\x)^2)},0)--(\x,{sqrt(1-(\x)^2)},0);
%=====================%

\draw[blue] (O) circle (1);
\draw[red, domain=-1:1, samples=500] plot (\x,0,{sqrt(4*(1-(\x)^2))});
\end{tikzpicture}

\end{document}


The red map in x=1 should produce the value zero, but it is positive.

-
Hi Lorenzo, welcome to the site! This is a known bug that's been fixed in the development version of PGF. There's a bugfix at the linked question. – Jake Feb 23 '13 at 10:40
A fine solution is to install gnuplot : \draw plot[id=f,domain=-1:1] function{sqrt(4*(1-x**2))};  – Alain Matthes Feb 23 '13 at 10:48
@Lorenzo: Actually, I take that back, the fix at the linked question works for 2^\x, but not for \x^2. You'll need to say (\x)^2 to get the correct plot. – Jake Feb 23 '13 at 10:50
@Lorenzo: I'm not sure I understand what you mean. What does it produce instead? Could you edit your question to include a screenshot of your output, and turn the code snippet into a complete minimal compilable document (starting from \documentclass)? – Jake Feb 23 '13 at 11:21
@Lorenzo: It may be compilable with every preamble, but it's generally preferred if full example documents are posted so it's easier to try the code out, and to rule out the possibility of any "side-effects" of packages that may be loaded. Note that you don't need to load color if you're loading tikz. If you have less than 10 rep, you can insert the picture and then remove the ! from the code, someone else will then edit the question to insert it back in. – Jake Feb 23 '13 at 14:06

This is due to a numerical inaccuracy when determining where to sample the function. The following code makes sure that the upper bound of the domain is always sampled:

\documentclass{article}
\usepackage{tikz}

\makeatletter
\def\tikz@plot@samples@recalc#1:#2\relax{%
\pgfmathparse{#1}%
\let\tikz@temp@start=\pgfmathresult%
\pgfmathparse{#2}%
\let\tikz@temp@end=\pgfmathresult%
\pgfmathparse{\tikz@temp@start+(\tikz@temp@end-\tikz@temp@start)/(\tikz@plot@samples-1)}%
\edef\tikz@plot@samplesat{\tikz@temp@start,\pgfmathresult,...,\tikz@temp@end,\tikz@temp@end}%
}
\makeatother

\begin{document}
\begin{tikzpicture}
\draw (-2,0,0) -- (2,0,0);

\draw[red, domain=-1:1, samples=50] plot (\x,{sqrt(4*(1-(\x)^2))});
\end{tikzpicture}
\end{document}

-