5

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

  • 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
4

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}
  • 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
2

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]
       \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=-1:1, samples=501, smooth] gnuplot[id=poly]{(x^7+x^6+x^5+x^4+x^3+x^2+x)}; % Nope!
       \addplot[blue,thick,domain=-1:1, samples=101, smooth]{x - (1.0/3) * x^3+2.0/15*x^5-17/315*x^7-62.0/2835*x^9};
  \end{axis}
\end{tikzpicture}%
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.