# How can I draw a Brownian motion inside a circle?

I want to draw a two-dimensional Brownian motion or - more generally - the Euler-Maruyama discretized solution of a two-dimensional stochastic differential equation (SDE) inside a circle.

If I got it right, this should be possible using pgfplots and tikz. In this post, it is at least shown how a Brownian motion can be drawn. Now I "simply" need to generalize this to a simple SDE solver (which is hard for me, since at this point the pgfplots/tikz code is quite obscure to me and I don't have much time to read into this) and figure out how the drawing can be placed inside a circle. The plot should scale with the radius of the circle. And I will need to place multiple circles with different SDE solutions inside in a single figure, if that's of importance.

Hopefully the solution is easy. I can provide more information if necessary. A reference to a guide where it is shown what I'm asking would already be really helpful.

• You say it is hard for you, so what code have you tried and what problem did you encounter?
– cfr
Jan 15 at 23:58
• @cfr How can I let the drawing happen inside another object (the circle in this case) at all? Jan 16 at 9:50
• Which drawing? Which circle? If you show us what you've tried, it may be easier to help. Understand that I don't know what you're trying to create, what you've tried or where you're stuck. It's not at all clear to me in what sense you want to put a plot into a circle. Do you just want to draw a circle around the whole thing, axes and all? Why? Or do you want to clip some part of it? Or?
– cfr
Jan 17 at 2:03

\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\newcommand{\Emmett}[5]{% points, advance, rand factor, options, end label
\draw[#4] (0,0)
\foreach \x in {1,...,#1}
{   -- ++(#2, rand*#3)
}
node[right] {#5};
}
\foreach \r [count=\i] in {2.2,2.1,...,0.6}{
\begin{scope}[shift={(50*\i:{4*sin(70*\i)})}]
\pgfmathparse{0.9*rnd+0.3}
\definecolor{MyColor}{rgb}{\pgfmathresult,\pgfmathresult,\pgfmathresult}
\Emmett{int(300*\i)}{0.04}{0.02*\i}{red}{}
\end{scope}
}
\end{tikzpicture}
\end{document}


Edit:

\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}[line join=bevel]
\foreach \r [count=\i] in {1,1.4,...,2.2}{
\begin{scope}[shift={(5*\i,0)}]
\draw[red, scale=\r] (0,0) coordinate(start) \foreach \x in {1,...,1000} { -- ++(0.05*rand, 0.05*rand)} coordinate(end);
\fill (start) circle[radius=2pt] node[above]{$X_0$};
\fill (end) circle[radius=2pt] node[above]{$X_t$};
\end{scope}
}
\end{tikzpicture}
\end{document}


rand is a new pseudo random number each time. -- ++(dx,dy) means draw a line to a new coordinate that is dx larger in x and dy larger in y. -and finally have this new coordinate as the start of e.g. further lines.

• Thank yo uvery much for your answer! It works quite nicely! Two questions left: (a) What I want is a path of a 2d Brownian motion. I've tried to replace the thing inside the foreach loop by -- ++(rand * #3, rand * #3). Seems to work, but does each rand give a new random number or are here the same random numbers used twice? Also: What exactly does the ++ before the parenthesis? Jan 18 at 19:59
• (b) How can I make filled circles marking the starting point and end point of the path? They should also contain a test (like $X_0$ and $X_t$). I've tried to write \filldraw[fill= green] (0,0) circle[radius = .1]; in the beginning of the command, but that doesn't seem to be the right place. I'm also not sure how exactly I can change the starting point. I've tried to change the (0, 0), but nothing happened ... Jan 18 at 19:59
• To be honest, I did not think you could use this for anything. I will look at it more closely tomorrow. Jan 18 at 20:02
• Thanks in advance! Can you quickly tell me if I somehow can return the last point created? I need to draw an arc from that one to another circle. Jan 18 at 20:04
• Replace node[right] {#5}; with node[right] (nodename) {#5};. Now you have a named coordinate (nodename) that can be use like e.g. \draw (0,0) -- (nodename); Jan 18 at 20:07