[Tex/LaTex] How to plot f(x)=sin(x), k(x)=cos(x) and u(x)=x² with TikZ

boxplotpgfplotsplottikz-pgf

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?

Best Answer

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.