绘制不同次数的多项式

绘制不同次数的多项式

我如何获取屏幕截图上的内容?

在此处输入图片描述

梅威瑟:

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\usepackage{lmodern}
\begin{document}
    \begin{tikzpicture}[font=,
        declare function={f(\x)=0.5*pow(abs(\x-2),2)-0.06*pow(\x-2,3);}]
        \foreach \Z in {1,...,42}
        {\pgfmathsetmacro{\X}{\Z/10}
            \pgfmathsetmacro{\Y}{f(\X)+0.9*rnd}
            \ifnum\Z=1
            \xdef\LstOne{(\X,\Y)}
            \xdef\LstTwo{"(\X,\Y)"}
            \else
            \xdef\LstOne{\LstOne (\X,\Y)}
            \xdef\LstTwo{\LstTwo,"(\X,\Y)"}
            \fi}
        %  \begin{scope}[local bounding box=over0]
        %  \foreach \Z in {1,...,42}
        %  {\pgfmathsetmacro{\Coor}{{\LstTwo}[\Z-1]}
        %  \fill \Coor circle[radius=1pt];
        %  }
        %  \draw plot[smooth] coordinates \LstOne;
        %  \end{scope}
        \begin{scope}[local bounding box=over,xshift=-5cm]
            \foreach \Z in {1,...,40}
            {\pgfmathsetmacro{\Last}{{\LstTwo}[\Z-1]}
                \pgfmathsetmacro{\Current}{{\LstTwo}[\Z]}
                \pgfmathsetmacro{\Next}{{\LstTwo}[\Z+1]}
                %\typeout{\Last,\Current,\Next}
                \edef\temp{\noexpand\path ($0.6*\Current+0.2*\Last+0.2*\Next$)   coordinate 
                    (p\Z);}
                \temp
                \ifnum\Z=1
                \xdef\LstThree{(p\Z)}
                \else
                \xdef\LstThree{\LstThree (p\Z)}
                \fi
            }
            \foreach \Z in {1,...,42}
            {\pgfmathsetmacro{\Coor}{{\LstTwo}[\Z-1]}
                \fill \Coor circle[radius=1pt];
            }
            \draw[thick,blue] plot[smooth] coordinates \LstThree;
        \end{scope}
        %
        \begin{scope}[local bounding box=good,xshift=-10cm]
            \foreach \Z in {1,...,42}
            {\pgfmathsetmacro{\Coor}{{\LstTwo}[\Z-1]}
                \fill \Coor circle[radius=1pt];
            }
            \draw[thick,blue] plot[smooth,domain=0.1:4.2,variable=\x] (\x,{f(\x)+0.45});
        \end{scope}
        %
        \begin{scope}[local bounding box=under,xshift=-15cm]
            \foreach \Z in {1,...,42}
            {\pgfmathsetmacro{\Coor}{{\LstTwo}[\Z-1]}
                \fill \Coor circle[radius=1pt];
            }
            \draw[thick,blue] (0.1,0.4) -- (4.2,2);
        \end{scope}
        %
        \foreach \X in {over,good,under}
        {\draw[gray,thin] ([xshift=-3pt,yshift=3pt]\X.north west) rectangle 
            ([xshift=3pt,yshift=-3pt]\X.south east);
            \draw[stealth-stealth,thick] ([xshift=-3pt,yshift=3pt]\X.north west) node[right=1.5pt,fill=white]{Valores} 
            |- ([xshift=3pt,yshift=-3pt]\X.south east) node[below left]{Datos};}
    \end{tikzpicture}
\end{document}

答案1

我认为更简单的方法是让 R 进行拟合并制作knitrtikz 图。

3x1 示例:

\documentclass{standalone}
\begin{document}
<<echo=F,dev='tikz', fig.height= 4, fig.width=4 >>=
set.seed(20)
x <- 1:60
y <- 50 - .001 * x^2 +  .001 * x^3
y <- y + rnorm(length(x), mean=30, sd=15)
plot(x,y, type="n",col="cyan",las=1, xlab="Datos",ylab="Valores",xaxs="i", yaxs="i")
grid(lwd=3)
points(x,y,pch=16)
model <- lm(y ~ poly(x,25))
predicted <- predict(model)
lines(x,predicted,col='tan4',lwd=5)
model <- lm(y ~ poly(x,7))
predicted <- predict(model)
lines(x,predicted,col='orange',lwd=5)
model <- lm(y ~ poly(x,3))
predicted <- predict(model)
lines(x,predicted,col='red',lwd=5)
@
\end{document}

姆韦

相关内容