Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can we plot the following three functions

  • f(x) = sin(x)
  • k(x) = cos(x)
  • u(x) = x²

for x ∈ [0,1]

on a single plot with the help of TikZ?

share|improve this question
    
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Tobi Aug 20 '13 at 16:12

2 Answers 2

In pgfplots, you can implement the task with

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[domain=0:1,legend pos=outer north east]
    \addplot {sin(deg(x))}; 
    \addplot {cos(deg(x))}; 
    \addplot {x^2};
    \legend{$\sin(x)$,$\cos(x)$,$x^2$}
    \end{axis}
\end{tikzpicture}
\end{document}

Since the options are provided for the complete axis, the domain is shared among all these functions. The TikZ function deg(x) converts x from radians to degrees (similar to the postfix operator x r which does not appear to work in pgfplots).

The \legend describes the legend's values and the legend pos option is one of the prescribed ways to configure the legend.

enter image description here

You could also use \addplot[color=red] to configure color/marker and what-ever styles as discussed by Tobi.

share|improve this answer
2  
Thanks for your addition! I was too lazy to give a full solution, but only because Mozo was too lazy to read the manual first himself … –  Tobi Apr 20 '11 at 21:43

This is the example which can be fount in the pgfmanual.pdf on p. 225

\documentclass{minimal}
\usepackage{tikz,pgfplots}

\begin{document}
\begin{tikzpicture}[domain=0:4]
    \draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
    \draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
    \draw[->] (0,-1.2) -- (0,4.2) node[above] {$f(x)$};
    \draw[color=red]    plot (\x,\x)    node[right] {$f(x) =x$};
    \draw[color=blue]   plot (\x,{sin(\x r)})   node[right] {$f(x) = \sin x$};
    \draw[color=orange] plot (\x,{0.05*exp(\x)}) node[right]
        {$f(x) = \frac{1}{20} \mathrm e^x$};
\end{tikzpicture}
\end{document}

Think you can adapt it for your functions?!

There ist also a way with pgfplots. See the above mentioned manual for more information.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.