4

I have a graph with some extra graphics on it: Graph of function

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
    declare function={ f(\x) = 4/(1+pow(\x/5, 2)); }]
% partitions
\foreach \x in {1, ..., 8} {
    \filldraw [fill=gray!20!white, draw=gray] (\x, 0) -- (\x, {f(\x)}) -- (\x + 1, {f(\x)}) -- (\x + 1, 0) -- cycle;
    \filldraw [fill=gray!40!white, draw=gray] (\x, 0) -- (\x, {f(\x + 1)}) -- (\x + 1, {f(\x + 1)}) -- (\x + 1, 0) -- cycle;
}
\draw [help lines] (9, 0) -- (9, {f(9)});
% axes
\draw [->] (-0.3, 0) -- (10, 0) node[right] {$n$};
\draw [->] (0, -0.3) -- (0, 4.5) node[above] {$y$};
\foreach \x in {1, ..., 9} {
    \draw [font=\footnotesize] (\x, 0) -- (\x, -0.1) node[below] {\x};
}
% curve
\draw [thick, domain=0:10] plot(\x, {f(\x)});
\end{tikzpicture}
\end{document}

However, I would like to replace the function f(\x) = 4/(1+pow(\x/5, 2)) with f(\x) = 4*(1+pow(tan(180*\x), 2)/10)/(1+pow(\x/5, 2)), which is the same except with a vertical asymptote toward positive infinity at each half-integer (0.5, 1.5, 2.5, etc.). The problem is that TikZ does not appear to be good at handling this. (It crashes with a "Dimension too large" error.) I would like

  1. to keep the view frame the same as it appears currently,
  2. to crop any part of the graph that would not appear on the view frame as it is currently, and
  3. to have the graph lines near asymptotes all be cut off at the same vertical height.

I tried cutting off the plotting near asymptotes with this code:

\foreach \x in {1, ..., 10} {
    \draw [thick, domain=\x-0.4:\x+0.4] plot(\x, {f(\x)});
}

but this failed all three of my conditions listed above.

I also tried using pgfplots, but it was difficult to reproduce what I have here. Since everything else is in pure TikZ, I would like to solve this one remaining problem in pure TikZ. (I don't want to learn a whole new package unless it is absolutely necessary.)

So what is the best way to draw a graph with asymptotes that satisfies my three conditions?

1
  • 1
    You can check this answer to see how we can avoid singularities. In this way you can avoid f to go to infinity.
    – Kpym
    Commented Jun 30, 2015 at 7:32

1 Answer 1

2

You can use what you had tried already, just go a little bit closer to the asymptote, i.e. \x-0.46:\x+0.46, and use \clip to cut off the parts of the curves that go too high.

enter image description here

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
   declare function={ f(\x) = 4*(1+pow(tan(180*\x), 2)/10)/(1+pow(\x/5, 2)); }]
% partitions
\foreach \x in {1, ..., 8} {
    \filldraw [fill=gray!20!white, draw=gray] (\x, 0) -- (\x, {f(\x)}) -- (\x + 1, {f(\x)}) -- (\x + 1, 0) -- cycle;
    \filldraw [fill=gray!40!white, draw=gray] (\x, 0) -- (\x, {f(\x + 1)}) -- (\x + 1, {f(\x + 1)}) -- (\x + 1, 0) -- cycle;
}
\draw [help lines] (9, 0) -- (9, {f(9)});
% axes
\draw [->] (-0.3, 0) -- (10, 0) node[right] {$n$};
\draw [->] (0, -0.3) -- (0, 4.5) node[above] {$y$};
\foreach \x in {1, ..., 9} {
    \draw [font=\footnotesize] (\x, 0) -- (\x, -0.1) node[below] {\x};
}
% curves
\begin{scope}
\clip (0,0) rectangle (10,4.5);
\foreach \x in {1, ..., 10} {
    \draw [thick, domain=\x-0.46:\x+0.46] plot(\x, {f(\x)});
}
\end{scope}
\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .