# [Tex/LaTex] how to plot the functions in TikZ

tikz-pgf

I would like to plot a linear function as y=40-0.2x and an hyperbola as y=5/x. How could I make it? I've tried, a code from this document in p. 160, but the curve wasn't in the same page with the axes.

EDIT: Having used the Tikz method mentionned below, I would like to make a graph for y=5/(x-1)+1.

The result as it is shown in Microsoft Mathematics is:

However, using LaTeX, I don't have the same result. My code is:

\documentclass[tikz,border=3mm]{standalone}
\usepackage[english,greek]{babel}
\usepackage{ucs}
\usepackage[utf8x]{inputenc}
\begin{document}
\begin{tikzpicture}[xscale=0.08,yscale=0.09,domain=0.140:60,samples=800]
\draw[->] (0,0) -- (65,0) node[below] {$x$};
\draw[->] (0,0) -- (0,35) node[left] {$y$};
\foreach \i in {10,20,...,50} {
\draw (\i,1) -- (\i,-1) node[below] {$\i$};
}
\foreach \i in {5,10,...,30} {
\draw (1,\i) -- (-1,\i) node[left] {$\i$};
}
\draw[green] plot (\x,{5/(\x-1)+1});
\end{tikzpicture}
\end{document}


Where is my fault?

# Remarks

Keep in mind for both methods, that 5/x has a singularity at 0. At this point the function value will be infinity, which is kind of hard to draw for PGF and will therefore throw an error:

• ! Package PGF Math Error: You've asked me to divide 5' by 0', but I cannot divide any number by 0' (in '{5/0}').

• ? Dimensions too large

# Method 1: Using TikZ

The advantage of using TikZ here is, that you can easily place nodes on the plot. The disadvantage is, that you have to scale x and y dimension, because else the drawing will be 220cm wide.

## Implementation

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[xscale=0.04,yscale=0.08,domain=0.125:220,samples=400]
\draw[->] (-10,0) -- (225,0) node[below] {$x$};
\draw[->] (0,-5) -- (0,45) node[left] {$y$};
\foreach \i in {50,100,...,200} {
\draw (\i,1) -- (\i,-1) node[below] {$\i$};
}
\foreach \i in {10,20,...,40} {
\draw (1,\i) -- (-1,\i) node[left] {$\i$};
}
\draw[blue] plot (\x,{40-0.2*\x});
\draw[red] plot (\x,{5/\x});
\end{tikzpicture}
\end{document}


# Method 2: Using PGFPlots

This is much more elegant and the code is much shorter.

## Implementation

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0.125:220,samples=400]
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[
domain=0.125:220,
xmin=-10, xmax=220,
ymin=-5, ymax=45,
samples=400,
axis y line=center,
axis x line=middle,
]
\end{axis}
\end{tikzpicture}
\end{document}


# Method 3: PSTricks (Just for fun)

Using the package pst-plot you get access to advanced plotting features. Also PSTricks is much faster than TikZ for plotting, because it makes use of the Postscript language.

## Implementation

Compile with xelatex or latex -> dvips -> ps2pdf.

\documentclass[pstricks,border=3mm]{standalone}
\usepackage{pst-plot}
\begin{document}
\begin{pspicture}[xAxisLabel=$x$,yAxisLabel=$y$](-0.5,0)(0.5,6.5)
\begin{psgraph}[arrows=->,Dx=50,Dy=10](0,0)(-10,-5)(220,45){8cm}{6cm}
\psplot[plotpoints=200,linecolor=blue]{0}{220}{40 0.2 x mul sub}
\psplot[plotpoints=200,linecolor=red]{0.125}{220}{5 x div}
\end{psgraph}
\end{pspicture}

\begin{pspicture}[xAxisLabel=$x$,yAxisLabel=$y$,xAxisLabelPos={c,-12},yAxisLabelPos={-35,c}](-1,-1)(0.5,6.5)
\begin{psgraph}[axesstyle=frame,xticksize=-5 45,yticksize=-10 220,Dx=50,Dy=10](0,0)(-10,-5)(220,45){8cm}{6cm}
\psplot[plotpoints=200,linecolor=blue]{0}{220}{40 0.2 x mul sub}
\psplot[plotpoints=200,linecolor=red]{0.125}{220}{5 x div}
\end{psgraph}
\end{pspicture}
\end{document}
`