[Tex/LaTex] Plot to illustrate secant lines

diagramstikz-pgf

I am trying for teaching purpose to explain how basically linear regression might work and how it could be extended. Since I am not the TeXpert I fear that my idea might be tricky and more difficult than it probably can be. I have seen many nice plots with tikz and pgfplots.

I searched around and found finally the following image.

enter image description here

This image looks quite appropriate, cause I can explain how the regression might look like. But the problem is that I cannot manipulate, e.g. draw grid, change the naming of x_0 resp \epsilon, change the red line to a dotted one, add other points than P and Q, etc. If the function itself is x^2 or some mixture, I don't mind for explanation. My main objective is to illustrate the basic ideas. And if I would have a sample, then I could extend it little bit further to mean, variance, etc.

Is anybody willing to give me a helping hand? I don't mind if somebody could point me to existing samples. Would be fine as well.

Best Answer

With TikZ it is really easy. I used Plain TeX, so you will need to \input tikz.tex instead of \usepackage{tikz} and instead of \begin{document}...\end{document} you issue \bye at the end of your document.

Typeset the following code with pdftex

\input tikz.tex
\nopagenumbers% for cropping
\usetikzlibrary{arrows,intersections}
\tikzpicture[
        thick,
        >=stealth',
        dot/.style = {
            draw,
            fill=white,
            circle,
            inner sep=0pt,
            minimum size=4pt
        }
    ]
    \coordinate (O) at (0,0);
    \draw[->] (-0.3,0) -- (8,0) coordinate[label={below:$x$}] (xmax);
    \draw[->] (0,-0.3) -- (0,5) coordinate[label={right:$f(x)$}] (ymax);
    \path[name path=x] (0.3,0.5) -- (6.7,4.7);
    \path[name path=y] plot[smooth] coordinates {(-0.3,2) (2,1.5) (4,2.8) (6,5)};
    \scope[name intersections={of=x and y,name=i}]
        \fill[gray!20] (i-1) -- (i-2 |- i-1) -- (i-2) -- cycle;
        \draw (0.3,0.5) -- (6.7,4.7) node[pos=0.8,below right] {Sekante};
        \draw[red] plot[smooth] coordinates {(-0.3,2) (2,1.5) (4,2.8) (6,5)};
        \draw (i-1) node[dot,label={above:$P$}] (i-1) {} -- node[left] {$f(x_0)$} (i-1 |- O) node[dot,label={below:$x_0$}] {};
        \path (i-2) node[dot,label={above:$Q$}] (i-2) {} -- (i-2 |- i-1) node[dot] (i-12) {};
        \draw (i-12) -- (i-12 |- O) node[dot,label={below:$x_0 + \varepsilon$}] {};
        \draw[blue,<->] (i-2) -- node[right] {$f(x_0 + \varepsilon) - f(x_0)$} (i-12);
        \draw[blue,<->] (i-1) -- node[below] {$\varepsilon$} (i-12);
        \path (i-1 |- O) -- node[below] {$\varepsilon$} (i-2 |- O);
        \draw[gray] (i-2) -- (i-2 -| xmax);
        \draw[gray,<->] ([xshift=-0.5cm]i-2 -| xmax) -- node[fill=white] {$f(x_0 + \varepsilon)$}  ([xshift=-0.5cm]xmax);
    \endscope
\endtikzpicture
\bye

This will produce the following output (cropped)

enter image description here

As intersections are computed by the intersections library this solution is adaptive. In this code, not actually the point Q is moved, but one of the points on the red line, which causes Q to move (if you look closely you can see, that the red line gets an ugly bump while Q moves right).

My workflow for creating animations is the following:

  • Modify the source file, such that for each variation a seperate page in the output is created (in most cases using the PGF \foreach loop, like here)
  • Crop the resulting PDF using Heiko Oberdiek's pdfcrop.
  • Import the cropped PDF in GIMP.
  • In GIMP: Reverse the layer order and export as .gif with the option As Animation checked and a delay of 200 milliseconds (otherwise it is too fast for me).

The following contains the code used to create the animation. I marked the extra and modified lines needed in contrast to the above code.

\input tikz.tex
\nopagenumbers% for cropping
\usetikzlibrary{arrows,intersections}
\foreach \Q in {4,4.1,4.2,...,5,4.9,4.8,...,4.1} {%<-- added
\tikzpicture[
        thick,
        >=stealth',
        dot/.style = {
            draw,
            fill=white,
            circle,
            inner sep=0pt,
            minimum size=4pt
        }
    ]
    \coordinate (O) at (0,0);
    \draw[->] (-0.3,0) -- (8,0) coordinate[label={below:$x$}] (xmax);
    \draw[->] (0,-0.3) -- (0,5) coordinate[label={right:$f(x)$}] (ymax);
    \path[name path=x] (0.3,0.5) -- (6.7,4.7);
    \path[name path=y] plot[smooth] coordinates {(-0.3,2) (2,1.5) (\Q,2.8) (6,5)};%<-- modified
    \scope[name intersections={of=x and y,name=i}]
        \fill[gray!20] (i-1) -- (i-2 |- i-1) -- (i-2) -- cycle;
        \draw (0.3,0.5) -- (6.7,4.7) node[pos=0.8,below right] {Sekante};
        \draw[red] plot[smooth] coordinates {(-0.3,2) (2,1.5) (\Q,2.8) (6,5)};%<-- modified
        \draw (i-1) node[dot,label={above:$P$}] (i-1) {} -- node[left] {$f(x_0)$} (i-1 |- O) node[dot,label={below:$x_0$}] {};
        \path (i-2) node[dot,label={above:$Q$}] (i-2) {} -- (i-2 |- i-1) node[dot] (i-12) {};
        \draw (i-12) -- (i-12 |- O) node[dot,label={below:$x_0 + \varepsilon$}] {};
        \draw[blue,<->] (i-2) -- node[right] {$f(x_0 + \varepsilon) - f(x_0)$} (i-12);
        \draw[blue,<->] (i-1) -- node[below] {$\varepsilon$} (i-12);
        \path (i-1 |- O) -- node[below] {$\varepsilon$} (i-2 |- O);
        \draw[gray] (i-2) -- (i-2 -| xmax);
        \draw[gray,<->] ([xshift=-0.5cm]i-2 -| xmax) -- node[fill=white] {$f(x_0 + \varepsilon)$}  ([xshift=-0.5cm]xmax);
    \endscope
\endtikzpicture
\eject%<-- added
}%<-- added
\bye

enter image description here

Related Question