Ein Fehler, den ich in meinem TikZ/pgfplots-Bild gefunden habe

Ein Fehler, den ich in meinem TikZ/pgfplots-Bild gefunden habe

Hier ist mein Code:

\begin{tikzpicture} 
    [
        scale=1.05,
        declare function=
        {
            funcc(\x)=
            %(\x<-0.6) * (\x)+
            (\x<-0.6) * (0.7285067873303168+2.058823529411765*\x+2.1606334841628962*\x^2 + 0.7918552036199096*\x^3)+
            and(\x>=-0.6,\x<-0.4) * (1.1411764705882352+ 4.294117647058823*\x + 6.029411764705881*\x^2 + 
            2.941176470588235*\x^3)+
            and(\x>=-0.4,\x < -0.2) * (0.5*(2 + 6*\x + 5*\x^2))+
            and(\x>=-0.2,\x <0) * (1 +1*\x - 12.5*\x^2 - 25*\x^3)+
            and(\x>=0,\x <0.2) * (1 - 1*\x - 12.5*\x^2 +25*\x^3)+
            add(\x>=0.2,\x < 0.4) * (0.5*(2 - 6*\x + 5*\x^2))+
            and(\x>=0.4 ,\x<0.6) * (1.14118 - 4.29412*\x + 6.02941*\x^2 - 2.94118*\x^3) +
            %(\x>=0.6) * (\x^3);
            (\x>=0.6) * (0.728507 - 2.05882*\x + 2.16063*\x^2 - 0.791855*\x^3);
        },
    ]
\begin{axis}
    [
    title = {},%\quad The distribution of the points and the line of regression},
    axis lines = middle,
    legend pos=outer north east,
    xlabel={$x$},
    ylabel={$y$},
    xmin=-2, xmax=2,
    ymin=-2, ymax=2,
    ]
    \addplot%S 10
    [
    domain = -1:1,
    legend pos=outer north east,
    color=yellow,
    samples = 1000
    ]
    {
        (\x<-0.6) * (0.7285067873303168+2.058823529411765*\x+2.1606334841628962*\x^2 + 0.7918552036199096*\x^3)+
            and(\x>=-0.6,\x<-0.4) * (1.1411764705882352+ 4.294117647058823*\x + 6.029411764705881*\x^2 + 
            2.941176470588235*\x^3)+
            and(\x>=-0.4,\x < -0.2) * (0.5*(2 + 6*\x + 5*\x^2))+
            and(\x>=-0.2,\x <0) * (1 +1*\x - 12.5*\x^2 - 25*\x^3)+
            and(\x>=0,\x <0.2) * (1 - 1*\x - 12.5*\x^2 +25*\x^3)+
            add(\x>=0.2,\x < 0.4) * (0.5*(2 - 6*\x + 5*\x^2))+
            and(\x>=0.4 ,\x<0.6) * (1.14118 - 4.29412*\x + 6.02941*\x^2 - 2.94118*\x^3) +
            %(\x>=0.6) * (\x^3);
            (\x>=0.6) * (0.728507 - 2.05882*\x + 2.16063*\x^2 - 0.791855*\x^3)
    };
    
    \addplot %N 10
    [
    domain = -1:1,
    legend pos=outer north east,
    color=green,
    samples = 1000
    ]
    {
        -220.942*x^10+494.91*x^8-381.434*x^6+123.36*x^4-16.8552*x^2+1.
    };
    \addplot %N_{5}
    [
    domain = -1:1,
    legend pos=outer north east,
    color=blue,
    samples = 1000
    ]
    {
        1.20192*x^4-1.73077*x^2+0.567308
    };
    %\addlegendentry{$N_{5}(x)$}
    \addplot %f(x)
    [
    domain = -1:1,
    legend pos=outer north east,
    color=red,
    samples = 1000
    ]
    {
        1 /(1+25*x^2)
    };
    %\addlegendentry{$f(x) = \frac{1}{1+25x^{2}}$}
    \legend{$S_{10}(x)$,$N_{10}(x)$,$N_{5}(x)$,$f(x) = \frac{1}{1+25x^{2}}$};
    \end{axis}
\end{tikzpicture}   

Das Ergebnis ist unten:

Bildbeschreibung hier eingeben

Die gelbe Linie sollte fast der roten Linie entsprechen, aber der Unterschied ist zu groß.

Das korrekte Ergebnis der gelben Linie, das ich von Mathematica erhalten habe, ist unten:

Bildbeschreibung hier eingeben

Ich kann meinen Fehler trotzdem nicht finden, daher denke ich, dass das Problem bei pdgplots selbst liegt. Übrigens habe ich ungefähr eine Nacht gebraucht, um meinen Code zu reparieren, aber es scheint alles in Ordnung zu sein.

Antwort1

Die Änderung von „alles“ addin andergibt:

\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture} 
    [
        scale=1.05,
        declare function=
        {
            funcc(\x)=
            %(\x<-0.6) * (\x)+
            (\x<-0.6) * (0.7285067873303168+2.058823529411765*\x+2.1606334841628962*\x^2 + 0.7918552036199096*\x^3)+
            and(\x>=-0.6,\x<-0.4) * (1.1411764705882352+ 4.294117647058823*\x + 6.029411764705881*\x^2 + 
            2.941176470588235*\x^3)+
            and(\x>=-0.4,\x < -0.2) * (0.5*(2 + 6*\x + 5*\x^2))+
            and(\x>=-0.2,\x <0) * (1 +1*\x - 12.5*\x^2 - 25*\x^3)+
            and(\x>=0,\x <0.2) * (1 - 1*\x - 12.5*\x^2 +25*\x^3)+
            and(\x>=0.2,\x < 0.4) * (0.5*(2 - 6*\x + 5*\x^2))+
            and(\x>=0.4 ,\x<0.6) * (1.14118 - 4.29412*\x + 6.02941*\x^2 - 2.94118*\x^3) +
            %(\x>=0.6) * (\x^3);
            (\x>=0.6) * (0.728507 - 2.05882*\x + 2.16063*\x^2 - 0.791855*\x^3);
        },
    ]
\begin{axis}
    [
    title = {},%\quad The distribution of the points and the line of regression},
    axis lines = middle,
    legend pos=outer north east,
    xlabel={$x$},
    ylabel={$y$},
    xmin=-2, xmax=2,
    ymin=-2, ymax=2,
    ]
    \addplot%S 10
    [
    domain = -1:1,
    legend pos=outer north east,
    color=yellow,
    samples = 100
    ]
    {
        (\x<-0.6) * (0.7285067873303168+2.058823529411765*\x+2.1606334841628962*\x^2 + 0.7918552036199096*\x^3)+
            and(\x>=-0.6,\x<-0.4) * (1.1411764705882352+ 4.294117647058823*\x + 6.029411764705881*\x^2 + 
            2.941176470588235*\x^3)+
            and(\x>=-0.4,\x < -0.2) * (0.5*(2 + 6*\x + 5*\x^2))+
            and(\x>=-0.2,\x <0) * (1 +1*\x - 12.5*\x^2 - 25*\x^3)+
            and(\x>=0,\x <0.2) * (1 - 1*\x - 12.5*\x^2 +25*\x^3)+
            and(\x>=0.2,\x < 0.4) * (0.5*(2 - 6*\x + 5*\x^2))+
            and(\x>=0.4 ,\x<0.6) * (1.14118 - 4.29412*\x + 6.02941*\x^2 - 2.94118*\x^3) +
            %(\x>=0.6) * (\x^3);
            (\x>=0.6) * (0.728507 - 2.05882*\x + 2.16063*\x^2 - 0.791855*\x^3)
    };
    
    \addplot %N 10
    [
    domain = -1:1,
    legend pos=outer north east,
    color=green,
    samples = 100
    ]
    {
        -220.942*x^10+494.91*x^8-381.434*x^6+123.36*x^4-16.8552*x^2+1.
    };
    \addplot %N_{5}
    [
    domain = -1:1,
    legend pos=outer north east,
    color=blue,
    samples = 100
    ]
    {
        1.20192*x^4-1.73077*x^2+0.567308
    };
    %\addlegendentry{$N_{5}(x)$}
    \addplot %f(x)
    [
    domain = -1:1,
    legend pos=outer north east,
    color=red,
    samples = 100
    ]
    {
        1 /(1+25*x^2)
    };
    %\addlegendentry{$f(x) = \frac{1}{1+25x^{2}}$}
    \legend{$S_{10}(x)$,$N_{10}(x)$,$N_{5}(x)$,$f(x) = \frac{1}{1+25x^{2}}$};
    \end{axis}
\end{tikzpicture}   
\end{document}

Mehrere Grundstücke

Wobei das gelbe Diagramm in der Nähe des roten Diagramms liegt. Natürlich weiß ich nicht, was Sie versuchen, aber es scheint wahrscheinlicher, dass Sie diesen Fehler gemacht haben, als dass dies auf einen Fehler zurückzuführen ist.

verwandte Informationen