3

How do I plot several functions in Latex with tikz?

I want it to look something like this:

enter image description here

With the this code:

\documentclass{article}

\usepackage[latin1]{inputenc}
\usepackage{tikz}

% GNUPLOT required
\begin{document}
\pagestyle{empty}


\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[id=x] function{x} 
    node[right] {$f(x) =x$};
\draw[color=blue] plot[id=sin] function{sin(x)} 
    node[right] {$f(x) = \sin x$};
\draw[color=orange] plot[id=exp] function{0.05*exp(x)} 
    node[right] {$f(x) = \frac{1}{20} \mathrm e^x$};
\end{tikzpicture}


\end{document}

This does actually work.

But as soon as I insert a little bit more complicated functions like:

enter image description here

And:

enter image description here

It simply fails, and gives me an error:

\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[id=x] function{-1/3x+1/5} 
    node[right] {$f(x) =x$};
\draw[color=blue] plot[id=sin] function{1/2x+1/2-1/2\sqrt{5x^2+2x+1}} 
    node[right] {$f(x) = \sin x$};
\end{tikzpicture}

How do I solve this?

6

As Efraín Soto A. pointed out, "sqrt" shouldn't have a leading backslash. I just want to point out that you can do this with straight tikz/pgf --- GNUPLOT isn't necessary:

\documentclass{article}

\usepackage[latin1]{inputenc}
\usepackage{tikz}

\begin{document}
\pagestyle{empty}

\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}

\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 / 3 + 1/5}) node[right] {$f(x)$};
  \draw[color=blue] plot
    (\x, { 1/2 * \x + 1/2 - 1/2 * sqrt(5 * \x^2 + 2*\x + 1)})
    node[right] {$g(x)$};
\end{tikzpicture}

\end{document}

The plot.

  • What do I then do, if I want to plot from x=-2..2 and y=-2..2? But this plot below seems a bit odd. The red and the blue functions are supposed to touch at exactly (2,-1). – RandomDude Sep 9 '14 at 19:46
  • I mistyped the function from your example. Instead of -\x / 3 + 1/5 it should be -3/5 * \x + 1/5. In any case, you can use domain = -2:2 in the parameters to \draw to restrict the values of your variables. – Derek Sep 9 '14 at 20:03
3

A general template with PSTricks.

\documentclass[pstricks,border=0pt,12pt,dvipsnames]{standalone}
\usepackage{amsmath}
\usepackage{pstricks-add}
\usepackage{pst-plot}
% \usepackage{pst-math} provides ATAN
\usepackage[nomessages]{fp}

\FPeval\XMin{0-2*pi}
\FPeval\XMax{2*pi}
\FPeval\YMin{0-pi}
\FPeval\YMax{2*pi}

\FPeval\XOL{0-1/2} % of DeltaX
\FPeval\XOR{1/2} % of DeltaX
\FPeval\YOB{0-1/5} % of DeltaY
\FPeval\YOT{1/5} % of DeltaY

\FPset\xTrigLabelBase{4}
\FPset\yTrigLabelBase{6}
\FPset\Dx{1}
\FPset\Dy{3}

\FPeval\dx{pi/xTrigLabelBase*Dx}
\FPeval\dy{pi/yTrigLabelBase*Dy}

\FPeval\AxisL{XMin+dx*XOL}
\FPeval\AxisR{XMax+dx*XOR}
\FPeval\AxisB{YMin+dy*YOB}
\FPeval\AxisT{YMax+dy*YOT}


\newlength\Width\Width=12cm
\newlength\Height\Height=8cm

\newlength\llx\llx=-5pt
\newlength\urx\urx=15pt
\newlength\lly\lly=-5pt
\newlength\ury\ury=15pt


\psset
{
    llx=\llx,
    lly=\lly,
    urx=\urx,
    ury=\ury,
    xtrigLabels=true,
    ytrigLabels=true,
    xtrigLabelBase=\xTrigLabelBase,
    ytrigLabelBase=\yTrigLabelBase,
    labelFontSize=\scriptstyle,
    xAxisLabel=$x$,
    yAxisLabel=$y$,
    algebraic,
    plotpoints=500,
}

\newpsstyle{mygrid}
{
        dx=\dx,
        dy=\dy,
        %Dx=\Dx,
        %Dy=\Dy,
        labels=none,
        subticks=5,
        tickwidth=.4pt,
        subtickwidth=.2pt,
        tickcolor=Red!30,
        subtickcolor=ForestGreen!30,
        xticksize=\YMin\space \YMax,
        yticksize=\XMin\space \XMax,
        subticksize=1,
}

\def\f{Pi*sin(x)}
\def\fp{Derive(1,\f)}

\def\g{2*asin(x/(2*Pi))}

\begin{document}
\pslegend[lt]{%
    \color{NavyBlue}\rule{12pt}{1pt} & \color{NavyBlue} $y=\pi\sin x$ \\
    \color{Red}\rule{12pt}{1pt} & \color{Red} $y=\pi\cos x$ \\
    \color{ForestGreen}\rule{12pt}{1pt} & \color{ForestGreen} $y=2\sin^{-1}\left(\tfrac{x}{2\pi}\right)$
}
\begin{psgraph}
    [
        dx=\dx,
        dy=\dy,
        Dx=\Dx,
        Dy=\Dy,
        linecolor=gray,
        tickcolor=gray,
        ticksize=-3pt 3pt,
        axespos=top,
    ]{<->}(0,0)(\AxisL,\AxisB)(\AxisR,\AxisT){\dimexpr\Width-\urx+\llx}{!}%{\dimexpr\Height-\ury+\lly}
    \psaxes[style=mygrid](0,0)(\XMin,\YMin)(\XMax,\YMax)
    \psplot[linecolor=NavyBlue]{\XMin}{\XMax}{\f}
    \psplot[linecolor=Red]{\XMin}{\XMax}{\fp}
    \psplot[linecolor=ForestGreen]{\XMin}{\XMax}{\g}
    % -------------------
    % tangent and normal
    \psplotTangent[linecolor=Magenta]{Pi 3 div 2 mul}{1.5}{\f}
    \psplotTangent[linecolor=Cyan,Derive={-1/\fp}]{Pi 3 div 2 mul}{1.5}{\f}
    %
    % sum
    \psplot[linecolor=Rhodamine]{\XMin}{\XMax}{Sum(i,0,1,7,(-1)^(i)*x^(2*i)/fact(2*i))}
\end{psgraph}
\end{document}

enter image description here

Explanation about the settings

enter image description here

  • 2
    So very many keystrokes. PSTricks seems really verbose compared to the TikZ solutions posted. – Jake Sep 9 '14 at 18:19
  • @cyanide-basedfood So very many keystrokes. I think he's not really comparing :) – Manuel Sep 9 '14 at 18:23
  • The axes setting seems to be verbose as I have not bundled it into my own package. – kiss my armpit Sep 9 '14 at 18:25
  • If you carefully read my code above, you will see there are many important things buried there. For example, I presented tangent line, sum of several functions (sinusoidal series), derivative, trigonometric axis, fractional axis, and many more. – kiss my armpit Sep 9 '14 at 18:27
  • 2
    This is a good help for understanding TikZ a little better, thank you :) – RandomDude Sep 9 '14 at 19:34
2

It seems like the error is in the notation. You should not include the "\" just before the function square root:

\draw[color=blue] plot[domain=-5:5] function{x/2 + 1/2 - 1/2 * sqrt{5x^2+2x+1}};

On the other hand, be sure the function is defined in the domain you ask to be plotted.

  • It isn't sqrt{…} but sqrt(…). – Manuel Sep 9 '14 at 17:27
2

If use of gunplot is preferred, this is how. Pdflatex -shell-escape is required and install of gnuplot in the system.

enter image description here

Code

\documentclass{article}
\usepackage{tikz}

%\pagestyle{empty}

\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[id=x] function{x} 
    node[right] {$f(x) =x$};
\draw[color=blue] plot[id=sin] function{sin(x)} 
    node[right] {$f(x) = \sin x$};
\draw[color=orange] plot[id=exp] function{0.05*exp(x)} 
    node[right] {$f(x) = \frac{1}{20} \mathrm e^x$};
\end{tikzpicture}



\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[id=x] function{-x/3 + 0.2}  node[right] {$f(x)$};
\draw[color=blue] plot[id=a] function{x/2+0.5-sqrt(5*x**2+2*x+1)/2} 
    node[right] {$g(x)$};
\end{tikzpicture}
\end{document}
  • How do i plot the functions from x=-2..2 and y=-2..2? And this plot below seems a bit odd. The f(x) is supposed to be a tangent line at (2,-1), so they are supposed to touch each other at exactly that point, but they don't seems to. – RandomDude Sep 9 '14 at 19:55
  • OK, put domain=-2:2 right after id=x with a comma to separate them. Further, the function should have been -3*x/5+0.2. – Jesse Sep 9 '14 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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