\subsection{Interpolation}

Il s'agit ici de trouver un polynôme d'interpolation sur l'intervalle $[-1~;~1]$ de la fonction $f$ définie par :
\[
  f(x)=\frac{1}{1+8x^2}
\]

Le polynôme d'interpolation est celui obtenu par la méthode de \tkzimp{Lagrange} avec vingt et un points équirépartis ; il est de degré $20$.

Avec \tkzname{tkz-fct}, il fallait calculer les coefficients du polynôme à l'extérieur, puis l'écrire avec la méthode de \tkzimp{Horner}. Avec Lua, le polynôme est calculé directement à partir des points : une fonction Lua est rangée dans la table |FN| à l'aide de |tkzfn.fct|. La courbe de $f$ est en rouge, le polynôme d'interpolation en bleu, les points utilisés en jaune.

\subsubsection{Le 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}

Le résultat est sur la page suivante, où l'on peut constater le phénomène de \tkzimp{Runge}.

\subsubsection{La 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
