用Processing模拟布朗运动


布朗运动

介绍

布朗运动(英语:Brownian motion)是微小粒子或者颗粒在流体中做的无规则运动。布朗运动过程是一种正态分布的独立增量连续随机过程。它是随机分析中基本概念之一。其基本性质为:布朗运动W(t)是期望为0、方差为t(时间)的正态随机变量。对于任意的 $r$ 小于等于 $s$,$W(t)-W(s)$ 独立于 $W(r)$,且是期望为 $0$、方差为 $t-s$ 的正态随机变量。

模拟布朗运动的重点就在于W(t)是期望为0、方差为t(时间)的正态随机变量

在一维的形式下,就是说只要时间足够长,它累计向左,向右的距离是相同的,二维、三维形式下也是如此。

思路

实际上,processing自带的函数 random 就可以实现这种操作,多次调用 random(-range, range) 产生的数,就满足平均数接近 $0$,产生的数之和满足正态分布

即,令 $sum(x)$ 为产生的 $x$ 个随机数之和,那么 $sum(x)$ 服从正态分布 $N$。

这也就为什么不能用 noise 函数,因为 noise 产生的数本身就符合正态分布,它们之和不一定符合正态分布。

流程

在这里插入图片描述

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
int num = 2000;
int range = 12;

float[] ax = new float[num];
float[] ay = new float[num];

void setup()
{
size(1920, 1080);
for(int i = 0; i < num; i++) {
ax[i] = width/2;
ay[i] = height/2;
}
frameRate(30);
}

void oneStep()
{
background(51);
println("frameCount: "+frameCount);
//Shift all elements 1 place to the left
for(int i = 1; i < num; i++) {
ax[i-1] = ax[i];
ay[i-1] = ay[i];
}

// Put a new value at the end of the array
ax[num-1] += random(-range, range);
ay[num-1] += random(-range, range);

// Constrain all points to the screen
ax[num-1] = constrain(ax[num-1], 0, width);
ay[num-1] = constrain(ay[num-1], 0, height);

// Draw a line connecting the points
for(int i=1; i<num; i++) {
float val = float(i)/num * 204.0 + 51;
stroke(val);
line(ax[i-1], ay[i-1], ax[i], ay[i]);
}
}

void draw()
{
oneStep();
}

Author: BY 水蓝
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source BY 水蓝 !
  TOC