1

我正在模拟指数衰减。为什么它不起作用?这是代码

from math import *
from random import random
from time import sleep

class atom():
    def __init__(self, pos):
        self.pos = pos
        self.isalive = True

class substance():
    def __init__(self, halflife):
        self.halflife = float(halflife)
        self.rate = float(log(2) / self.halflife)
        self.life = float(0)
        self.probdecay = float(0)
        self.struct = [atom(i) for i in range(100)]
        self.isalive = True
        print self.rate

    def sim(self):
        print self.probdecay
        for i in range(len(self.struct)):
            if self.struct[i].isalive:
                if random() < self.probdecay:
                    self.struct[i].isalive = False

        self.life += 1.0
        self.probdecay = 1.0 - pow(1.0 - self.rate, self.life)

    def getstr(self):
        s = ""
        for i in self.struct:
            if i.isalive:
                s += "o"
            else:
                s += " "
        return s

s = substance(5)

while s.isalive:
    s.sim()
    print s.getstr()
    sleep(1)

这是输出

0.138629436112
0.0
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
0.138629436112
oooooo o ooooooooooo oooooooo ooooooo o oooooooooooo oooooo oooooooo oooo ooo oooooooo  oooo   oooo 
0.258040751667
o  ooo o   oo oo ooo o    ooo o ooooo o oo  oooooo o   oooo  oo oooo oo   ooo ooo  ooo   ooo   oo   
0.360898143882
   o o     oo     oo o    ooo   ooo o   oo  o o oo     o o    o    o o    ooo o o   oo   o o   oo   
0.449496473814
   o        o        o    o o    o      o     o        o      o      o     o                   oo   
0.525812467226
   o        o                    o      o     o                      o                              
0.591548817506
            o                           o                                                           
0.648172174615

0.696945867656

0.738958091134

0.775146183762

显然,这不是半衰期为 5 的指数衰减。这是怎么回事?

4

1 回答 1

1

看起来你混淆了粒子某个步骤衰减的概率,以及一个粒子在给定步骤衰减的概率。无论在哪一步,粒子在任何给定步骤上衰变的概率都是相同的,而粒子在给定步骤中衰变的概率单调递增的。当您初始化时,不要逐渐增加probdecay,而是设置一次substance

def __init__(self, halflife):
    self.probdecay = 1 - 0.5 ** (1.0 / halflife)
    ...
于 2013-07-21T08:08:17.207 回答