2

I am trying to plot some functions, however I run into the problem that I get the same output for different functions

enter image description here

Here is the code:

\documentclass[a4paper]{article}

\usepackage{pgfplots}
\begin{document}
\begin{figure}

\centering

    \begin{tikzpicture}[scale=0.7]


\begin{axis}[title style={yshift=-1ex,},
 title={$a=0.2$},
xlabel style={align=center}, xlabel=$p_2$,
    ylabel = {$\Delta x_1^{a,b}/x_1^{a,b}$},     xmin=0, xmax=2,
    ymin=0,ymax=0.003,
    legend style={legend pos=north west,legend cell align={left}},   enlargelimits=false, width={10cm},height={8cm}, xtick distance=0.5,
    ytick distance=0.0005,
    restrict y to domain=0:0.003,samples=500]
\addplot[mark=none, color=black, dashed] plot gnuplot{((0.2)^(1/10)+(1-0.2)^(1/10)*x^(1-1/10))/(0.999^(1/10)*(0.999^(1-1/10)*0.2^(1/10)+(1-0.2)^(1/10)*x^(1-1/10))) -1)};
\addlegendentry{$\xi=1/10$}

\end{axis}
\end{tikzpicture}
 \begin{tikzpicture}[scale=0.7]

\begin{axis}[title style={yshift=-1ex,},
 title={$a=0.2$},
xlabel style={align=center}, xlabel=$p_2$,
    ylabel = {$\Delta x_1^{a,b}/x_1^{a,b}$},     xmin=0, xmax=2,
    ymin=0,ymax=0.003,
    legend style={legend pos=north west,legend cell align={left}},   enlargelimits=false, width={10cm},height={8cm}, xtick distance=0.5,
    ytick distance=0.0005,
    restrict y to domain=0:0.003,samples=500]

\addplot[mark=none, color=black, dashed] gnuplot
{((0.2)^(1/2)+(1-0.2)^(1/2)*x^(1-1/2))/(0.999^(1/2)*(0.999^(1-1/2)*0.2^(1/2)+(1-0.2)^(1/2)*x^(1-1/2))) -1)};
\addlegendentry{$\xi=1/2$}

\end{axis}
\end{tikzpicture}
\begin{tikzpicture}[scale=0.7]

\begin{axis}[title style={yshift=-1ex,},
 title={$a=0.2$},
xlabel style={align=center}, xlabel=$p_2$,
    ylabel = {$\Delta x_1^{a,b}/x_1^{a,b}$},     xmin=0, xmax=2,
    ymin=0,ymax=0.003,
    legend style={legend pos=north west,legend cell align={left}},   enlargelimits=false, width={10cm},height={8cm}, xtick distance=0.5,
    ytick distance=0.0005,
    restrict y to domain=0:0.003,samples=500]

\addplot[mark=none, color=black, dashed] gnuplot
{((0.2)^(99/100)+(1-0.2)^(99/100)*x^(1-99/100))/(0.999^(99/100)*(0.999^(1-99/100)*0.2^(99/100)+(1-0.2)^(99/100)*x^(1-99/100))) -1)};
\addlegendentry{$\xi=99/100$}
\end{axis}
\end{tikzpicture}

\end{figure}
\end{document}

While compiling I also briefly get shown the message "line 2 unexpected or unrecognized token", however I have no idea how to fix this. I am very grateful for any help.

  • Welcome to TeX-SE! Please note that plot gnuplot is not entirely correct, it should be only gnuplot. Then AFAIK in gnuplot expressions ^ is to be replaced by **. Also please avoid passing scale=0.7 to the tikzpicture, pass it to the axis if you really want to use it. – user121799 Jun 10 at 20:37
  • 1
    @marmot, plot gnuplot does still work for backward compatibility reasons. Also ^ does work and will be translated correctly to ** before the expression is passed to gnuplot ... – Stefan Pinnow Jun 10 at 20:54
  • 1
    ... maybe it has something to do with the fact that there is one superfluous closing brace ()) which yields to this result? – Stefan Pinnow Jun 10 at 20:56
  • @Paul Ostmann, have you try to plot it directly in gnuplot? – Ñako Jun 10 at 21:00
  • I do not know if it's the case here, but in my gnuplot 1/2 is 0, like 1/10 or 99/100... Integer division. Try 1.0/2 etc etc... Can't check now, too late in the night ;-) – Rmano Jun 10 at 21:03
2

It seems that gnuplot does not recognize fractions as floats. Therefore, please whether place a point at the end of the fraction:

    \documentclass[a4paper]{article}

    \usepackage{pgfplots}
    \begin{document}
        \begin{figure}

            \centering

            \begin{tikzpicture} %[scale=0.7]


            \begin{axis}[title style={yshift=-1ex,},
            title={$a=0.2$},
            xlabel style={align=center}, xlabel=$p_2$,
            ylabel = {$\Delta x_1^{a,b}/x_1^{a,b}$},     xmin=0, xmax=2,
            ymin=0,ymax=0.003,
            legend style={legend pos=north west,legend cell align={left}},   enlargelimits=false, width={10cm},height={8cm}, xtick distance=0.5,
            ytick distance=0.0005,
            restrict y to domain=0:0.003,samples=500]
            \addplot[mark=none, color=black, dashed] plot gnuplot{((0.2)^(1/10.)+(1-0.2)^(1/10.)*x^(1-1/10.))/(0.999^(1/10.)*(0.999^(1-1/10.)*0.2^(1/10.)+(1-0.2)^(1/10.)*x^(1-1/10.)))-1};
            \addlegendentry{$\xi=1/10$}

            \end{axis}
            \end{tikzpicture}
            \begin{tikzpicture} %[scale=0.7]

            \begin{axis}[title style={yshift=-1ex,},
            title={$a=0.2$},
            xlabel style={align=center}, xlabel=$p_2$,
            ylabel = {$\Delta x_1^{a,b}/x_1^{a,b}$},     xmin=0, xmax=2,
            ymin=0,ymax=0.003,
            legend style={legend pos=north west,legend cell align={left}},   enlargelimits=false, width={10cm},height={8cm}, xtick distance=0.5,
            ytick distance=0.0005,
            restrict y to domain=0:0.003,samples=500]

            \addplot[mark=none, color=black, dashed] gnuplot{((0.2)^(1/2.)+(1-0.2)^(1/2.)*x^(1-1/2.))/(0.999^(1/2.)*(0.999^(1-1/2.)*0.2^(1/2.)+(1-0.2)^(1/2.)*x^(1-1/2.)))-1};
            \addlegendentry{$\xi=1/2$}

            \end{axis}
            \end{tikzpicture}
            \begin{tikzpicture} %[scale=0.7]

            \begin{axis}[title style={yshift=-1ex,},
            title={$a=0.2$},
            xlabel style={align=center}, xlabel=$p_2$,
            ylabel = {$\Delta x_1^{a,b}/x_1^{a,b}$},     xmin=0, xmax=2,
            ymin=0,ymax=0.003,
            legend style={legend pos=north west,legend cell align={left}},   enlargelimits=false, width={10cm},height={8cm}, xtick distance=0.5,
            ytick distance=0.0005,
            restrict y to domain=0:0.003,samples=500]

            \addplot[mark=none, color=black, dashed] gnuplot{((0.2)^(99/100.)+(1-0.2)^(99/100.)*x^(1-99/100.))/(0.999^(99/100.)*(0.999^(1-99/100.)*0.2^(99/100.)+(1-0.2)^(99/100.)*x^(1-99/100.)))-1};
            \addlegendentry{$\xi=99/100$}
            \end{axis}
            \end{tikzpicture}

        \end{figure}
    \end{document}

or substitute the fractions with decimal values:

    \documentclass[a4paper]{article}

    \usepackage{pgfplots}
    \begin{document}
    \begin{figure}

    \centering

        \begin{tikzpicture} %[scale=0.7]


    \begin{axis}[title style={yshift=-1ex,},
     title={$a=0.2$},
    xlabel style={align=center}, xlabel=$p_2$,
        ylabel = {$\Delta x_1^{a,b}/x_1^{a,b}$},     xmin=0, xmax=2,
        ymin=0,ymax=0.003,
        legend style={legend pos=north west,legend cell align={left}},   enlargelimits=false, width={10cm},height={8cm}, xtick distance=0.5,
        ytick distance=0.0005,
        restrict y to domain=0:0.003,samples=500]
    \addplot[mark=none, color=black, dashed] plot gnuplot{((0.2)^(0.1)+(1-0.2)^(0.1)*x^(1-0.1))/(0.999^(0.1)*(0.999^(1-0.1)*0.2^(0.1)+(1-0.2)^(0.1)*x^(1-0.1)))-1};
    \addlegendentry{$\xi=1/10$}

    \end{axis}
    \end{tikzpicture}
     \begin{tikzpicture} %[scale=0.7]

    \begin{axis}[title style={yshift=-1ex,},
     title={$a=0.2$},
    xlabel style={align=center}, xlabel=$p_2$,
        ylabel = {$\Delta x_1^{a,b}/x_1^{a,b}$},     xmin=0, xmax=2,
        ymin=0,ymax=0.003,
        legend style={legend pos=north west,legend cell align={left}},   enlargelimits=false, width={10cm},height={8cm}, xtick distance=0.5,
        ytick distance=0.0005,
        restrict y to domain=0:0.003,samples=500]

    \addplot[mark=none, color=black, dashed] gnuplot{((0.2)^(0.5)+(1-0.2)^(0.5)*x^(1-0.5))/(0.999^(0.5)*(0.999^(1-0.5)*0.2^(0.5)+(1-0.2)^(0.5)*x^(1-0.5)))-1};
    \addlegendentry{$\xi=1/2$}

    \end{axis}
    \end{tikzpicture}
    \begin{tikzpicture} %[scale=0.7]

    \begin{axis}[title style={yshift=-1ex,},
     title={$a=0.2$},
    xlabel style={align=center}, xlabel=$p_2$,
        ylabel = {$\Delta x_1^{a,b}/x_1^{a,b}$},     xmin=0, xmax=2,
        ymin=0,ymax=0.003,
        legend style={legend pos=north west,legend cell align={left}},   enlargelimits=false, width={10cm},height={8cm}, xtick distance=0.5,
        ytick distance=0.0005,
        restrict y to domain=0:0.003,samples=500]

    \addplot[mark=none, color=black, dashed] gnuplot{((0.2)^(0.99)+(1-0.2)^(0.99)*x^(1-0.99))/(0.999^(0.99)*(0.999^(1-0.99)*0.2^(0.99)+(1-0.2)^(0.99)*x^(1-0.99)))-1};
    \addlegendentry{$\xi=99/100$}
    \end{axis}
    \end{tikzpicture}

    \end{figure}
    \end{document}

enter image description here

  • 1
    As I suspected, +1. The problem is not that gnuplot "doesn't recognize" fractions, is that it uses the C-language style of automatic type promotion. So, 1/2 is an operation between integers, and uses integer division, which gives 0. If one of the two is float, it uses float (this is why 1/2. is 0.5). You'll find this behavior in (most?) programming languages. – Rmano Jun 11 at 7:06
  • Thank you so much for the help, this indeed fixed the problem. I have one more question though: Do you by any chance know what the error message "line 2 unexpected or unrecognized token" means, because I'm kinda curious and a google search did yield any helpful results. – Paul Ostmann Jun 11 at 7:57
  • @ Rmano, yes you are/were right about integers division. Thank you for the hint! And the further explanation – Ñako Jun 11 at 11:02
  • @ Paul Ostmann, see comment of Stefan Pinnow` about superfluous closing brace ...-1)}. Delete it: ...-1} and then compiles without errors. I've corrected it in the asnwer, too. – Ñako Jun 11 at 11:09

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.