[Tex/LaTex] High degree polynomial plotting with Tikz/pgfplot

pgfplotstikz-pgf

I am new to tikz/pgfplots…so sorry if my question has been asked…

I am trying to plot the tanh function using pgplots, then add a set of N points and plot the (n-1) degree polynomial passing through the points…

Here is what I have done so far:

The tanh should vary between -1 and 1 but when I try to plot a high degree polynomial, the y axis is rescaled… If I try to reduce the domain value I get an error message from pdflatex…

\begin{tikzpicture}
   \begin{axis}[
       clip=true,
       width = 10cm, height= 8cm,
       xlabel = {$x$},
       ylabel = {$f(x)$},
       ymin=-2, ymax=2, xmin=-10, xmax=10]
       \addplot[domain=-10:10,smooth] gnuplot[id=tanh]{tanh(x)};
       \foreach \data in {-3,-2,-1,0,1, 2, 3}{
          \addplot[only marks, black] coordinates {(\data, {tanh(\data)})};
        }
       \addplot[domain=-10:10, samples=25, smooth] gnuplot[id=poly]{(x^7+x^6+x^5+x^4+x^3+x^2+x)};
  \end{axis}
\end{tikzpicture}%

Any idea will be helpful!

I am trying to reproduce that figure:

tanh fitted with a degree 14 lagrangian polynomial…

enter image description here

And Here is my updated code:

    \begin{tikzpicture}
  \begin{axis}[
      width = 10cm, height= 8cm,
      xlabel = {$x$},
      ylabel = {$f(x)$},
    ]
    \addplot[smooth] gnuplot[id=tanh]{tanh(x)};
    \addplot[only marks, samples at={-10,-8.57,...,10}]{tanh(x)};
    \addplot[samples=25, smooth, red, thick] gnuplot[id=poly]
            {(0.000000000138483*x^13-0.000000039937808*x^11+0.000004411606097*x^9-0.000237588565393*x^7+
            0.006610739962583*x^5-0.094756895881911*x^3+0.791753916669373*x)};
  \end{axis}
\end{tikzpicture}%

enter image description here

Best Answer

I don't know what you want to show, but you can choose particular domains for each function which produce comparable results.

enter image description here

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
   \begin{axis}[
       clip=true,
       width = 10cm, height= 8cm,
       xlabel = {$x$},
       ylabel = {$f(x)$},
%       ymin=-2, ymax=2
    ]
       \addplot[smooth] gnuplot[id=tanh]{tanh(x)};
       \foreach \data in {-3,-2,-1,0,1, 2, 3}{
          \addplot[only marks, black] coordinates {(\data, {tanh(\data)})};
        }
       \addplot[domain=-1.1:0.5,samples=25, smooth] gnuplot[id=poly]{(x^7+x^6+x^5+x^4+x^3+x^2+x)};
  \end{axis}
\end{tikzpicture}
\end{document}