\subsection{Interpolation}

We look for an interpolating polynomial on the interval $[-1, 1]$ of the function $f$ defined by:
\[
  f(x)=\frac{1}{1+8x^2}
\]

The interpolating polynomial is the \tkzimp{Lagrange} polynomial built on twenty-one equally spaced points; its degree is $20$.

With \tkzname{tkz-fct}, the coefficients of the polynomial had to be computed elsewhere and the polynomial written in \tkzimp{Horner} form. With Lua, the polynomial is computed directly from the points: a Lua function is stored in the table |FN| with |tkzfn.fct|. The graph of $f$ is in red, the interpolating polynomial in blue, the points used in yellow.

\subsubsection{The code}
\begin{tkzexample}[code only]
\tkzFnDef{f}{1/(1+8*x^2)}
\directlua{
  local xs, ys = {}, {}
  for i = 0, 20 do
    xs[i] = -1 + i/10
    ys[i] = FN.f(xs[i])
  end
  FN.P = tkzfn.fct(function(x)
    local s = 0
    for i = 0, 20 do
      local l = 1
      for j = 0, 20 do
        if j == i then else l = l*(x-xs[j])/(xs[i]-xs[j]) end
      end
      s = s + ys[i]*l
    end
    return s
  end)
}
\begin{tikzpicture}
  \tkzFnInit[xmin=-1,xmax=1,ymin=-1.8,ymax=1.2,xstep=0.1,ystep=0.2]
  \tkzFnGrid
  \tkzFnAxes
  \tkzFnDraw[samples=400,line width=4pt,color=red,opacity=.5](f)
  \tkzFnDraw[samples=400,line width=1pt,color=blue](P)
  \tikzset{tkzfn point/.style={draw=black,fill=yellow}}
  \tkzFnMarkPoints[size=3pt](f){-1,-0.9,...,1}
\end{tikzpicture}
\end{tkzexample}

The result is on the next page, where the \tkzimp{Runge} phenomenon can be seen.

\subsubsection{The figure}

\tkzFnDef{f}{1/(1+8*x^2)}
\directlua{
  local xs, ys = {}, {}
  for i = 0, 20 do
    xs[i] = -1 + i/10
    ys[i] = FN.f(xs[i])
  end
  FN.P = tkzfn.fct(function(x)
    local s = 0
    for i = 0, 20 do
      local l = 1
      for j = 0, 20 do
        if j == i then else l = l*(x-xs[j])/(xs[i]-xs[j]) end
      end
      s = s + ys[i]*l
    end
    return s
  end)
}
\begin{sidewaysfigure}[htbp]
\centering
\begin{tikzpicture}[scale=.75]
  \tkzFnInit[xmin=-1,xmax=1,ymin=-1.8,ymax=1.2,xstep=0.1,ystep=0.2]
  \tkzFnGrid
  \tkzFnAxes
  \tkzFnDraw[samples=400,line width=4pt,color=red,opacity=.5](f)
  \tkzFnDraw[samples=400,line width=1pt,color=blue](P)
  \tikzset{tkzfn point/.style={draw=black,fill=yellow}}
  \tkzFnMarkPoints[size=3pt](f){-1,-0.9,...,1}
\end{tikzpicture}
\caption{Interpolation: $\dfrac{1}{1+8x^2}$}
\end{sidewaysfigure}

\endinput
