Processing模拟阻尼谐振

近期闲来无事-稍微琢磨了一下二阶线性微分方程以及二阶系统的一些特性,不甘寂寞,觉得这个东西用Processing来展示会比较屌。随手写了个Processing程序。欢迎众位看官来围观一下。

QQ Photo20140411123815什么?你居然还在用Windows XP? 噢噢!对滴,我就是XP坚守党!那又怎么样?
QQ Photo20140411123935
好的,下面公布代码。从开源来,到开源去,这是我们一直奉行的…
这里有两个文件,都放在一个Processing的目录下就好
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
47
int x = 0;
int y = height/2;
Integrator initial;
 
void setup()
{
  clear();
  size(800, 400);
  initial = new Integrator(400, 0.98, 0.2);
  initial.target(height/2);
}
 
int temp;
void draw()
{
  smooth();
  temp = (int)initial.value;
  initial.update();
  stroke(255);
  line(x, temp, x ++, (int)initial.value);
 
  if ( x >= width-80 )
  {
    //clear();
    x = 0;
    while(true);
  }
 
  line(x, (int)initial.value, width-76, (int)initial.value);
  rectMode(CENTER);
  fill(205);
  stroke(255, 0, 0);
  rect(width-50, (int)initial.value, 50, 30);
  delay(50);
  stroke(0);
  line(x, temp, width-76, temp);
}
 
 
void Delay(int ms)
{
  try
  {
    Thread.sleep(ms);
  }
  catch(Exception e){}
}
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Integrator {
 
  final float DAMPING = 0.5f;
  final float ATTRACTION = 0.2f;
 
  float value;
  float vel;
  float accel;
  float force;
  float mass = 1;
 
  float damping = DAMPING;
  float attraction = ATTRACTION;
  boolean targeting;
  float target;
 
 
  Integrator() { }
 
 
  Integrator(float value) {
    this.value = value;
  }
 
 
  Integrator(float value, float damping, float attraction) {
    this.value = value;
    this.damping = damping;
    this.attraction = attraction;
  }
 
 
  void set(float v) {
    value = v;
  }
 
 
  void update() {
    if (targeting) {
      force += attraction * (target - value);      
    }
 
    accel = force / mass;
    vel = (vel + accel) * damping;
    value += vel;
 
    force = 0;
  }
 
 
  void target(float t) {
    targeting = true;
    target = t;
  }
 
 
  void noTarget() {
    targeting = false;
  }
}
对了,突然发现,还有所谓群摆,下右图,谁能搞个出来看看吖?让大家鼓个掌!
创元素的小伙伴们,努力吧?科学的道路上从来…..(此处省略一万字)
Simple_harmonic_oscillatorCoupled_oscillators
打赏

Leave a Reply