2

I am trying to draw line segments between a smooth rnd noisy function (blue) and some other function (red) at given sample positions. However the random numbers change when I revaluate the function, so the lines do not contact with the noisy function. I'm unsure how to correct this.

tik

Here is the code:

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\begin{document}
\begin{tikzpicture}[
    declare function={
        g(\x)=((0.1*(\x-2)^3)+1)/4;
        gn(\x)=g(\x)+0.1*rnd;
        f(\x)=(0.2*(\x-1.4)^2+0.8)/4;
      }
  ]
  \begin{axis}[
      width=6.5cm,
      axis x line=center,
      axis y line=center,
      xtick={0.0,1,...,4.0},
      ytick={0.0,0.1,...,0.59},
      domain = 0:4,
      samples = 31,
      ymin=0.0,
      xmin=0.0,
      xmax=4.0,
      ymax=0.6,
    ]
    \pgfmathsetseed{126838}
    \addplot+[mark=none,color=blue,thick, smooth] {gn(x)};
    \addplot+ [
      red,
      only marks,
      error bars/.cd,
      y dir=both,
      y explicit,
      error mark=none,
    ] coordinates {
        (0.2,{f(0.2)}) += (0,{gn(0.2)-f(0.2)})
        (1.0,{f(1.0)}) += (0,{gn(1.0)-f(1.0)})
        (1.2,{f(1.2)}) += (0,{gn(1.2)-f(1.2)})
        (2.5,{f(2.5)}) += (0,{gn(2.5)-f(2.5)})
        (3.0,{f(3.0)}) += (0,{gn(3.0)-f(3.0)})
      };
    \addplot+[mark=none,color=red,thick] {f(x)};
  \end{axis}
\end{tikzpicture}
\end{document}
cwkx
  • 21
  • May be https://tex.stackexchange.com/a/59934/1952 and https://tex.stackexchange.com/a/60370/1952 can help you – Ignasi Sep 09 '20 at 08:26

0 Answers0