pgfplots interpolation

A PSTricks solution:

\documentclass{article}

\usepackage{pstricks-add}

\def\interpolation[#1](#2,#3)#4{
  \psline[linestyle = #1](0,#3)(#2,#3)(#2,0)
  \uput[270](#2,0){$x_{#4}$}
  \uput[180](0,#3){$y_{#4}$}
}

\begin{document}

\psset{xunit = 2, yunit = 1.5}
\begin{pspicture}(-0.24,-0.24)(4.68,5.76)
\def\xA{0.4}
\def\yA{1.2}
\def\xB{1.6}
\def\yB{1.6}
\def\xC{2.1}
\def\yC{2.5}
\def\xD{2.8}
\def\yD{3.7}
\def\xE{3.8}
\def\yE{4.7}
  \psaxes[
    labels = none,
    xticksize = -0.14 5,
    yticksize = -0.105 4,
    tickwidth = 0.5pt,
    tickcolor = gray!50
  ]{->}(0,0)(4.5,5.5)[$x$,0][$y$,90]
  \psset{
    dotsize = 3pt 2,
    dotstyle = Bo
  }
  \psline[
    linewidth = 1.2pt,
    linecolor = blue,
    showpoints = true,
  ](\xA,\yA)(\xB,\yB)(\xC,\yC)(\xD,\yD)(\xE,\yE)
  \psdot[
    fillcolor = red
  ](\xC,\yC)
  \interpolation[dotted](\xB,\yB){1}
  \interpolation[dashed](\xC,\yC){}
  \interpolation[dotted](\xD,\yD){2}
\end{pspicture}

\end{document}

output

Notice that all you have to do is choose the coordinates for the points, i.e., the values of \xA,...,\xE and \yA,...,\yE, and the drawing will adjust accordingly.

Update

Notice that had it not been for the labels on the axes, we could have defined the points using \pnodes instead of all the \defs. (There may be a smart way of defining the coordinates for the points but I don't know of it.)

Tags:

Pgfplots