# Graphing multiple functions on the same graph

I'm new to LaTeX and am attempting to type up some old differential equations notes. The only part I seem to be having trouble with is graphing multiple functions onto a single graph. For example the family of solutions of: y=2e^x, y=e^x, y=e^-x, y=2e^-x. I was talking to a physics professor and he suggested that I use TikZ or pfgplots. Would there be one program you would recommend over another?

• I recommend TikZ Dec 31, 2013 at 6:53
• I recommend PSTricks as the first resort. Dec 31, 2013 at 7:11
• I allow myself to welcome Christopher to TeX.SX (speaking on behalf of the whole community). @Christopher: You can have a look at our starter guide to familiarize yourself further with our format. I also want to remark at that position that the package PSTricks is incompatible with the compiler pdflatex - just in case that you want to use that one. For graphs (like the one you are speaking about), I can recommend the package pgfplots (works on base of TikZ). Dec 31, 2013 at 9:10

With pgfplots. \begin{axis}[options]...\addplot \end{axis} is required.

Code

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=-4:4, samples=100,grid=major,
restrict y to domain=0:4,xlabel=$x$,ylabel=$y(x)$, legend pos=north west]

\legend{$2e^x$, $e^x$, $e^{-x}$, $2e^{-x}$}
\end{axis}
\end{tikzpicture}
\end{document}


With PSTricks. Compile it with latex-dvips-ps2pdf sequence.

\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-plot}
\psset
{
algebraic,
plotpoints=1000,
yunit=.5,
}

\begin{document}
\begin{pspicture}(-5,-1)(6,24)
\psaxes[linecolor=gray,ticksize=4pt 0,Dy=2]{->}(0,0)(-5,-1)(5.5,23.5)[$x$,0][$y$,90]
\psset{yMaxValue=22,linewidth=2pt}
\foreach \y/\c/\i/\f in
{
2*Euler^x/red/2/2e^x,
Euler^x/green/4/e^x,
Euler^(-x)/blue/6/e^{-x},
2*Euler^(-x)/magenta/8/2e^{-x}
}
{
\psset{linecolor=\c}
\psplot{-5}{5}{\y}
\psline(3,\i)(4,\i)
\uput[0](4,\i){\textcolor{\c}{\large$y=\f$}}
}% important percent sign
\end{pspicture}
\end{document}


## Important Notes

Even though PSTricks cannot work with pdflatex directly, it really does not bother us because the agile users (who adopt the best practice paradigm, "separation of concern") know that diagrams should be separated from the text contents. It means that each diagram should be put in a separate, standalone, yet compilable input file. For PSTricks, this diagram's input file must be compiled by latex-dvips-ps2pdf sequence. The product of it is a diagram in PDF format which is consumable to pdflatex used to compile the main input file (i.e., the input file containing the text document). Does it make sense?

• But if your pictures as standalone will not adapt its font or similar whenever something changes etc., do you still have a good answer for me? Thank you! Dec 31, 2013 at 12:09
• @strpeter: We can create our own package common.sty just to load the common settings (fonts, etc). This package then can be loaded from within each input file, either for diagrams or text document. Does it make sense for you? :-) Dec 31, 2013 at 12:11

With TikZ:

\documentclass[border=1cm]{standalone}

\usepackage{tikz}

\begin{document}

\tikz[yscale=0.5]{
\draw [help lines,xstep=0.5cm] (0,0) grid (3,15);
\draw [red,thick,->] (0,0) plot [domain=0:2] (\x,2*e^\x);
\draw [blue,thick,->] (0,0) plot [domain=0:2.7] (\x,e^\x);
\draw [->] (0,0) -- (3.5,0) ;
\draw [->] (0,0) -- (0,15.5) ;
\node [fill,red] at (4,7.5) {};
\node at (4.85,7.5) {$y=2e^x$};
\node [fill,blue] at (4,6.5) {};
\node at (4.75,6.5) {$y=e^x$};
\foreach \x in {0,...,15}{\node [left] at (0,\x) {\tiny \x};}
\foreach \y in {0,0.5,...,3}{\node [below] at (\y,0) {\tiny \y};}
}

\end{document}