# How to draw this graph in pgfplots or tikz?

I have a picture

With Mathematica I found formula of the curve is 6.12465 - 0.206646 x - 4.42686 x^2 - 2.58476 x^3 + 3.53276 x^4 + 3.78595 x^5 - 2.30475 x^6 - 1.91715 x^7 + 1.0322 x^8 + 0.430597 x^9 - 0.286308 x^10 - 0.0251744 x^11 + 0.040756 x^12 - 0.00530342 x^13 - 0.00183667 x^14 + 0.000649826 x^15 - 0.0000757694 x^16 + 3.16808*10^-6 x^17

I reduce the code and draw in pgfplots. I tried

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepackage{fouriernc}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
declare function={Y(\x)=6-0.21*\x -4.43*\x^2 -2.6*\x^3 +3.5*\x^4 +3.8*\x^5 -2.31*\x^6 -1.2*\x^7 +1.03*\x^8 +0.43*\x^9 -0.29*\x^10 -0.03*\x^11 +0.04*\x^12 -0.005*\x^13 -0.001*\x^14 +0.001*\x^15  + 3.169*10^{-6}*\x^17;},
axis lines = center,
xlabel=$x$,ylabel=$y$,
domain=-2.5:4.5,
ymin=-4,
ymax=6.2,
xmin=-3,
xmax=4.2,
samples=100,xtick distance=1,
ytick distance=2,unit vector ratio*=1 1 1,
width=11cm,
grid=major,
grid style={gray!30}
]
\addplot [black, mark=*,only marks,samples at={0,-1,1,-2,2}] {Y(x)};
\node at (axis cs:-0.25, -0.25) {$O$} ;
\end{axis}
\end{tikzpicture}
\end{document}


But I don't get the result. How can I draw it in pgfplots or tikz?

• What happens? Do you get an error or a wrong result? – cfr Sep 11 '17 at 0:33
• @cfr I got got an ill-formatted floating point number 2Y6.0e0]'. The unreadable part was near '2Y6.0e0]'. (in '6-0.21*2Y2.2171765e0]-4.43*2Y2.2171765e0]^2-2.6*2Y2.2171 765e0]^3+3.5*2Y2.2171765e0]^4+3.8*2Y2.2171765e0]^5-2.31*2Y2.2171765e0]^6-1.2*2Y 2.2171765e0]^7+1.03*2Y2.2171765e0]^8+0.43*2Y2.2171765e0]^9-0.29*2Y2.2171765e0]^ 10-0.03*2Y2.2171765e0]^11+0.04*2Y2.2171765e0]^12-0.005*2Y2.2171765e0]^13-0.001* 2Y2.2171765e0]^14+0.001*2Y2.2171765e0]^15+3.169*10^{-6}*2Y2.2171765e0]^17'). See the PGF Math package documentation for explanation. – minhthien_2016 Sep 11 '17 at 0:35
• Related: But have some more problems (see my answer) tex.stackexchange.com/questions/35191/… – koleygr Sep 11 '17 at 1:18
• @koleygr Yes, with no setting at all, it runs in maximal-backwards-compatibility mode. But 1.9 is still missing fixes and features in the current version. – cfr Sep 11 '17 at 2:15

You can draw something similar that shows what happens with the code:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\def\func(#1){6-0.21*#1 -4.43*#1^2 -2.6*#1^3 +3.5*#1^4 +3.8*#1^5 -2.31*#1^(6) -1.2*#1^(7) +1.03*#1^(8) +0.43*#1^(9) -0.29*#1^(10) -0.03*#1^(11) +0.04*#1^(12) -0.005*#1^(13) -0.001*#1^(14) +0.001*#1^(15) + 3.169*10^(-6)*#1^(17)}%

\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
xlabel=$x$,ylabel=$y$,
domain=-1.35:1.4,
ymin=-4,
ymax=10.2,
xmin=-3,
xmax=4.2,
samples=100,xtick distance=1,
ytick distance=2,unit vector ratio*=1 1 1,
width=11cm,
grid=major,
grid style={gray!30}
]
\addplot [black, mark=*,only marks,samples at={0,-1,1,-2,2}] {\func(x)};
\node at (axis cs:-0.25, -0.25) {$O$} ;
\end{axis}
\end{tikzpicture}
\end{document}


The output is

If you change the domain that I have already changed from your code... you will see that you will be out of range and pgf will complain too for values that can not afford.

The output is not the original mathematica output because you have changed the numbers on the factors on products with huge exponents (like 15 and 17) and this causes huge differences.

To overcome of pgf complaints you have to redefine function (manually by a reducing factor) and so to print a (manually) scaled y axis with the real values.

Edit:

The code that can give your result is:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\def\func(#1){6.12465 - 0.206646*#1 - 4.42686*#1^2 - 2.58476*#1^3 + 3.53276*#1^4 + 3.78595*#1^5 - 2.30475*#1^6 - 1.91715*#1^7 + 1.0322*#1^8 + 0.430597*#1^9 - 0.286308*#1^10 - 0.0251744*#1^11 + 0.040756*#1^12 - 0.00530342*#1^13 - 0.00183667*#1^14 + 0.000649826*#1^15 - 0.0000757694*#1^16 + 3.16808*(10^(-6))*#1^17}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
xlabel=$x$,ylabel=$y$,
domain=-2.2:3.5,
ymin=-4,
ymax=10.2,
xmin=-3,
xmax=4.2,
samples=100,xtick distance=1,
ytick distance=2,unit vector ratio*=1 1 1,
width=11cm,
grid=major,
grid style={gray!30}
]
\addplot [black, mark=*,only marks,samples at={0,-1,1,-2,2}] {\func(x)};
\node at (axis cs:-0.25, -0.25) {$O$} ;
\end{axis}
\end{tikzpicture}
\end{document}
`

And it's output is:

But if you increase samples you will see that pgf can not handle the accuracy over x=3.

• Please change ymax to 7 because I don't want to waste space for uploading one more image – koleygr Sep 11 '17 at 2:00
• I saw difficult with samples. – minhthien_2016 Sep 11 '17 at 9:10
• What do you mean? pgf can not really afford such kind of numbers as far as I can understand if you mean the values over x=3... Please explain the problem or edit your question (if my answer is not good enough) or open a new question if the problem is a new problem. – koleygr Sep 11 '17 at 9:30