# High degree polynomial plotting with Tikz/pgfplot

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]
\foreach \data in {-3,-2,-1,0,1, 2, 3}{
\addplot[only marks, black] coordinates {(\data, {tanh(\data)})};
}
\end{axis}
\end{tikzpicture}%


I am trying to reproduce that figure:

tanh fitted with a degree 14 lagrangian polynomial...

And Here is my updated code:

    \begin{tikzpicture}
\begin{axis}[
width = 10cm, height= 8cm,
xlabel = {$x$},
ylabel = {$f(x)$},
]
{(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}%


• Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Feb 27 '15 at 11:24
• Mathematical remark: You try to plot a polynom of degree 7 in a y - range vom -10^7 to 10^7, whereas tanh is a restricted function, i.e. only in the -1 and 1, what do you expect then? ;-). I.e. your domain is ways too large!!!! – user31729 Feb 27 '15 at 11:26
• The Taylor expansion of tanh is x-1/3 x^3 + 2/15 x^5.... – user31729 Feb 27 '15 at 11:31
• Welcome to TeX.SX! You don't have to sign with your name since [it automatically appears in the lower right corner](tex.stackexchange. – Jesse Feb 27 '15 at 11:38
• Two general comments: You don't need to use gnuplot for this particular plot, PGFPlots can handle this natively; and instead of the \foreach construct, you can simply use \addplot[only marks, samples at={-3,-2,...,3}] {tanh(x)}; to generate the dots. – Jake Feb 27 '15 at 11:49

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

\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
]
\foreach \data in {-3,-2,-1,0,1, 2, 3}{
\addplot[only marks, black] coordinates {(\data, {tanh(\data)})};
}
\end{axis}
\end{tikzpicture}
\end{document}

• An alternative that avoids having to set the domain for each plot individually is to set restrict y to domain=-2:2 for the axis. – Jake Feb 27 '15 at 11:29

This uses just some terms in the Taylor expansion of tanh(x).
(It's the blue line)

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

\begin{document}

\begin{tikzpicture}
\begin{axis}[
clip=true,
width = 10cm, height= 8cm,
xlabel = {$x$},
ylabel = {$f(x)$},
ymin=-2, ymax=2, xmin=-10, xmax=10]