다음과 같은 다이어그램을 만들려고 합니다.
점점 더 많은 노드가 있는 함수를 맞추려고 시도했지만 제대로 보이지 않습니다(더 잘 설명하기 위해 다이어그램에 3개의 곡선을 모두 표시).
\documentclass[10pt,landscape,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[UKenglish]{babel}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture} [scale = 0.5]
\foreach \p in {-5,...,5} \node[circle,fill=green] at (\p,2*rand) (\p) {};
\draw [cyan, xshift=4cm] plot [smooth, tension=1] coordinates { (-5) (-4) (-3) (-2) (-1) (0) (1) (2) (3) (4) (5) };
\draw [red, xshift=4cm] plot [smooth, tension=1] coordinates { (-5) (-3) (-1) (1) (3) (5) };
\draw [blue, xshift=4cm] plot [smooth, tension=1] coordinates { (-5) (5) };
\end{tikzpicture}
\end{document}
또한 다음과 같은 노드를 생성해 보았습니다(즉, 정의된 함수 x^2+x+1 + "noise"를 사용하여 대신:
\foreach \p in {-5,...,5} \node[circle,fill=green] at (\p, \p * \p + \p + 1 + rand) (\p) {};
하지만 그것은 전혀 작동하지 않습니다.
어쨌든 내 코드는 노드를 수동으로 나열하도록 강제하는데 이는 좋지 않습니다.
점점 더 높은 차수의 다항식을 보간하는 방법이 필요하다고 생각합니까? 하지만 어떻게 해야 할지 잘 모르겠습니다.
답변1
"보간" 좌표를 정의하고 이를 부드러운 플롯으로 연결할 수 있습니다.
\documentclass[10pt,landscape,a4paper]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture} [scale = 0.5]
\pgfmathtruncatemacro{\imin}{-5}
\pgfmathtruncatemacro{\imax}{5}
\foreach \p in {\imin,...,\imax} \node[circle,fill=green] at (\p,2*rand) (p\p) {};
\foreach \X [evaluate=\X as \Xm using {int(\X-1)},evaluate=\X as \Xp using
{int(\X+1)}] in {\imin,...,\imax}
{\ifnum\X=\imin
\path (p\X) coordinate (i\X);
\else
\ifnum\X=\imax
\path (p\X) coordinate (i\X);
\else
\path (barycentric cs:p\X=0.6,p\Xm=0.2,p\Xp=0.2) coordinate (i\X);
\fi
\fi
}
\draw [blue] plot [smooth, tension=1,variable=\X,samples at={\imin,...,\imax}]
(i\X);
\end{tikzpicture}
\end{document}
물론 장력 외에도 가중치를 변경할 수 있습니다.
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\foreach \X in {1,1.5,...,10,9.5,9,...,1.5}
{\begin{tikzpicture}
\pgfmathtruncatemacro{\imin}{-5}
\pgfmathtruncatemacro{\imax}{5}
\pgfmathsetmacro{\mainweight}{\X}
\pgfmathsetseed{27}
\node[anchor=north west,font=\sffamily] at (\imin,-2)
{weight is \pgfmathprintnumber{\mainweight}};
\foreach \p in {\imin,...,\imax} \node[circle,fill=green] at (\p,2*rand) (p\p) {};
\foreach \X [evaluate=\X as \Xm using {int(\X-1)},evaluate=\X as \Xp using
{int(\X+1)}] in {\imin,...,\imax}
{\ifnum\X=\imin
\path (p\X) coordinate (i\X);
\else
\ifnum\X=\imax
\path (p\X) coordinate (i\X);
\else
\path (barycentric cs:p\X=\mainweight,p\Xm=1,p\Xp=1) coordinate (i\X);
\fi
\fi
}
\draw [blue] plot [smooth, tension=0.5,variable=\X,samples at={\imin,...,\imax}]
(i\X);
\end{tikzpicture}}
\end{document}