0

I want to model the Brownian motion with multiple molecules/particles and animate them. So far I have come up with this code solution but I am unable to get the desired result. Below is my code

N   =  500;  % number of samples
tau = .1;    % time interval in seconds
D   =  10;   % diffusion coefficient
NumMolecules  =  500;  % number of moelcues to be released
k  = sqrt(2*D*tau);   % scaling factor
dx = k * randn(N,NumMolecules);
dy = k * randn(N,NumMolecules);
x  = cumsum(dx);
y  = cumsum(dy);
h  = gscatter(x(1,:),y(1,:),1:NumMolecules,'b');
for k = 2:N
    for p = 1:NumMolecules
        h(p).XData = x(k,p);
        h(p).YData = x(k,p);
    end
    drawnow limitrate
end
drawnow

Can anyone tell me why I am unable to get the desired results?

Edit

Desired result: To animate the motion of the molecules using ‘drawnow’ function

Marouen
  • 907
  • 3
  • 13
  • 36
nashynash
  • 375
  • 2
  • 19
  • 1
    *the desired result*. Well, it completely depends what you desire! If you desire the code to output cupcakes, we cannot help. If your desire is something else, then please tell us, as we might be able to help. – Ander Biguri Oct 19 '17 at 15:21
  • @AnderBiguri I have added an Edit :) – nashynash Oct 19 '17 at 15:26
  • Why does it not work? Your code looks very similar to the answer [here](https://stackoverflow.com/questions/39632393/plotting-brownian-motion-matlab), and that code works – Ander Biguri Oct 19 '17 at 15:28
  • @AnderBiguri I get a straight line. And in the link you sent the diffusion coefficient is not taken into consideration – nashynash Oct 19 '17 at 15:32
  • The link I send plots random points and animates them, which is what you want. Ultimately, you just have a typo though – Ander Biguri Oct 19 '17 at 15:33

1 Answers1

2

you have h(p).YData = x(k,p); change it to h(p).YData = y(k,p);

It's plotting the same numbers against each other right now, which is why you get a straight line.

Brad Day
  • 400
  • 2
  • 9