[Tex/LaTex] Using calc in TikZ

tikz-pgf

How to use function calc in TikZ?

For example, suppose I have a function f(x)=x^2.

Now, I want to find f(1) and f(2), and
then I want \draw f(1) -- f(2);.

Best Answer

A possibility with pgf 2.1

\documentclass{article} 
\usepackage{tikz}
\makeatletter
\pgfmathdeclarefunction{square}{1}{%
\begingroup
 \pgfmathparse{#1*#1}
 \pgfmath@smuggleone\pgfmathresult%
\endgroup}    

\begin{document}
\begin{tikzpicture}
    \draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,8.1);
    \draw[->] (-0.2,0) -- (3.2,0) node[right] {$x$};
    \draw[->] (0,-0.2) -- (0,4.2) node[above] {$f(x)$};
    \draw[domain=0:3] plot (\x,{square(\x)-1}) node[below right] {$f(x) = x^2$};
\end{tikzpicture}
\end{document}

enter image description here

Related Question