5

I'm plotting a function f(x) with random values as follows:

\documentclass[tikz]{standalone}

\begin{document}
\begin{tikzpicture}
\draw[color=blue, samples=150]  plot[domain=0:10] (\x,rand) node[below] {$f(x)$};
\end{tikzpicture}
\end{document}

Now I want to plot a new Function g(x) which is defined as g(x)=f(x)+sin(x). Is there a way to do that?

I would really appreciate any help.

Thanks

9

Yes, this is easily possible if you set the seed directly before the plot and use the same samples and domain settings.

If you don’t want to set the seed manually, you can store it before the first plot and re-set it afterwards.

Code

\documentclass[tikz,convert=false]{standalone}
\makeatletter
\def\pgfmathstoreseed#1{\let#1\pgfmath@rnd@z}
\let\pgfmathrestoreseed\pgfmathsetseed
\makeatother
\begin{document}
\begin{tikzpicture}
  \pgfmathstoreseed\mySeed
  \draw[color=blue, samples=250] plot[domain=0:10] (\x,rand/10) node[right] {$f(x)$};
  \pgfmathrestoreseed\mySeed
  \draw[color=red,  samples=250] plot[domain=0:10] ({\x,rand/10 + sin (\x r)})
                                                       node[below] {$f(x) + \sin x$};
\end{tikzpicture}

\begin{tikzpicture}
  \pgfmathsetseed{126838}
  \draw[color=blue, samples=250] plot[domain=0:10] (\x,rand/10) node[right] {$f(x)$};
  \pgfmathsetseed{126838}
  \draw[color=red,  samples=250] plot[domain=0:10] ({\x,rand/10 + sin (\x r)})
                                                       node[below] {$f(x) + \sin x$};
\end{tikzpicture}
\end{document}
\end{document}

Output

enter image description here

  • 1
    That is exactly what I was looking for. Thank you. – Staple Aug 5 '13 at 18:41

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.