# Plotting Hermite polynomials

I prefer to plot the first five Hermite polynomials through \draw command (not through the \axis command). There is no problem with the first three ones, but sketching the last two polynomials is not possible due to the “dimension problem”. How is rescaling possible to show all the graphs in the plane?

\begin{figure}[H]
\begin{center}
\begin{tikzpicture}[scale=1.25]
\draw[thick,->] (-3,0) -- (3,0);
\draw[thick,->] (0,-3) -- (0,3);
\draw [smooth,ultra thick,domain=-1:1,black] plot (\x,{2*\x});
\draw [smooth,ultra thick,domain=-1:1,gray] plot (\x,{4*\x*\x-2});
\draw [smooth,ultra thick,domain=-1:1,black] plot (\x,{8*\x*\x*\x-
12*\x});
\draw [smooth,ultra thick,domain=-1:1,blue] plot (\x,{16*\x*\x*\x*\x-
48*\x*\x*+12});
\draw [smooth,ultra thick,domain=-1:1,gray] plot (\x,{32*\x*\x*\x*\x*\x-
160*\x*\x*\x+120});
\end{tikzpicture}
\end{center}
\caption{The graph of some Hermite polynomials over the reals}
\end{figure}

• You might want to use pgfplots for serious plotting without the smooth option.. – percusse May 12 '18 at 10:25

You can divide your function by a factor to prevent ! Dimension too large. and then apply yscale by this factor.

But as your polynomials have values too big you have to clip also.

    \documentclass[tikz,border=7pt]{standalone}
\begin{document}
\begin{tikzpicture}[scale=1.25]
\clip (-3,-3) rectangle (3,3);% <- clip to prevent drawing H_5(0) = 120
\draw[thick,->] (-3,0) -- (3,0);
\draw[thick,->] (0,-3) -- (0,3);
\draw [smooth,ultra thick,domain=-1:1,black] plot (\x,{2*\x});
\draw [smooth,ultra thick,domain=-1:1,gray] plot (\x,{4*\x*\x - 2});
\draw [smooth,ultra thick,domain=-1:1,black] plot (\x,{8*\x*\x*\x - 12*\x});
\draw [smooth,ultra thick,domain=-1:1,blue,yscale=100]% <- up/down scaling by 100
plot (\x,{16/100*\x*\x*\x*\x - 48/100*\x*\x + 12/100});
\draw [smooth,ultra thick,domain=-1:1,red,yscale=100]% <- up/down scaling by 100
plot (\x,{32/100*\x*\x*\x*\x*\x - 160/100*\x*\x*\x + 120/100*\x});
\end{tikzpicture}
\end{document}


Notes:

• The order is important : 160/100*\x*\x*\x is not the same as 160*\x*\x*\x/100 if you want to eliminate ! Dimension too large.
• There was an error in your H_4 : ...48*\x*\x*+12 should be ...48*\x*\x+12.
• There was an error in your H_5 : ...+120 should be ...+120*\x.
• Suggestion: [y=0.5mm] instead of [scale=1.25], remove the \clip, and draw the y-axis from -40 to 40. – Torbjørn T. May 12 '18 at 8:59
• @TorbjørnT. I don't know if Amir wants to keep the aspect ratio or not. – Kpym May 12 '18 at 9:14
• I think here Horner's scheme is needed. – percusse May 12 '18 at 10:26

You can try with pgfplots

\documentclass[]{article}

\usepackage{graphicx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}

\begin{document}

\begin{figure}
\begin{center}

\begin{tikzpicture}[scale = 1.25]
\begin{axis}[
axis lines = middle,
grid,
xmin = -1.2, xmax = 1.2,
domain = -1 : 1,
xlabel = $x$,
ylabel = $H_n(x)$,
samples = 100,
thick,
legend style = {at = {(1.1, 0.5)}, anchor = west, draw = none, mark = none},
declare function = {
H0(\x) = 1;
H1(\x) = 2 * \x;
H2(\x) = 4 * \x^2 - 2;
H3(\x) = 8 * \x^3 - 12 * \x;
H4(\x) = 16 * \x^4 - 48 * \x^2 + 12;
H5(\x) = 32 * \x^5 - 160 * \x^3 + 120 * \x;
}, ]
\addplot[red] {H0(x)}; \addlegendentry{$n=0$};
\addplot[green] {H1(x)}; \addlegendentry{$n=1$};
\addplot[blue] {H2(x)}; \addlegendentry{$n=2$};
\addplot[cyan] {H3(x)}; \addlegendentry{$n=3$};
\addplot[magenta] {H4(x)}; \addlegendentry{$n=4$};
\addplot[olive] {H5(x)}; \addlegendentry{$n=5$};

\end{axis}
\end{tikzpicture}
\end{center}
\caption{The graph of some Hermit polynomials for reals}
\end{figure}

\end{document}


• The OP explicitly ask not through the \axis command. – Kpym May 12 '18 at 7:00

For fun, a pstricks solution. The pst-func package has a \psPolynomial command, which takes as its first argument the list of coefficients of a polynomial (in increasing order):

\documentclass[11pt, svgnames, border = 6pt]{standalone}
\usepackage{fourier}
\usepackage{auto-pst-pdf}
\def\f{20*(1-cos(x))}

\begin{document}

\psset{xunit=4cm, arrowinset=0.12, ticksize=2.5pt -2.5pt, labels =all, algebraic}%ticks =none,
\begin{pspicture}*(-1.5,-7.8)(1.95,7.8)
\psaxes[arrows=->, linecolor=LightSteelBlue, tickcolor=LightSteelBlue] (0,0)(-1.5,-7.8)(1.9, 7.9)[$x$,-115][$y$,-140]
\psgrid[subgriddiv=1, gridcolor=Gainsboro, gridwidth=0.4pt, gridlabels=0pt](0,0)(-1,-7)(1,7)
\uput[dl](0,0){$O$}
\psset{linewidth=1.2pt, plotpoints=200, plotstyle=curve, polarplot, algebraic, labelsep=0.5em}
\psline[linecolor =DarkSeaGreen](-1,-1 )(1, 1 ) %%H1
\psPolynomial[coeff = -1 0 1, linecolor =IndianRed]{-1}{1} %%H2(x) = x²-1
\psPolynomial[coeff = 0 -3 0 1, linecolor =Goldenrod]{-1 }{1 } % H3(x) = x³-3x)
\psPolynomial[coeff = 3 0 -6 0 1, linecolor =Plum]{-1}{1} %%H4
\psPolynomial[coeff = 0 15 0 -10 0 1, linecolor =Gold ]{-1}{1} % H5
\uput[r](1.25,6){$\begin{array}{l}\textcolor{DarkSeaGreen}{\rule{1cm}{1.2pt}}\quad H_1(x) \\ \textcolor{IndianRed}{\rule{1cm}{1.2pt}}\quad H_2(x)\\ \textcolor{Goldenrod}{\rule{1cm}{1.2pt}}\quad H_3(x)\\ \textcolor{Plum}{\rule{1cm}{1.2pt}}\quad H_4(x)\\ \textcolor{Gold}{\rule{1cm}{1.2pt}}\quad H_5x) \end{array}$}
\end{pspicture}

\end{document}


That is a standard thingy. If an expression is reasonably small due to cancellations, you need to tell this to TikZ.

\draw [smooth,ultra thick,domain=-1:1,blue] plot (\x,{16*\x*\x*(\x*\x-3)+12});
\draw [smooth,ultra thick,domain=-1:1,gray] plot (\x,{32*\x*\x*\x*(\x*\x-5)+120*\x});


works (thanks to @Kpym for the missing \x after 120!) and does not cause an error. As you can see, I just wrote the expressions in such a way that TikZ sees the cancellations. Needless to say that the expressions are still too large to be plotted on an A4 paper.

• Nice. In the second function ...+120 should be ...+120*\x so you can factorize it to (\x*\x*(\x*\x-5)+15/4)*\x*32. – Kpym May 12 '18 at 13:09
• For this particular case where \x is in -1:1 moving the coefficients at the end is enough. For example \x*\x*\x*\x*\x*32 - \x*\x*\x*160 + \x*120 works. – Kpym May 12 '18 at 13:18