2012-03-25 147 views
2

可以說我想模擬一個粒子狀態,它可以是正常的(0)或興奮的(1)在給定的幀。粒子處於激發狀態f%的時間。如果粒子處於激發態,則它持續〜L幀(具有泊松分佈)。我想模擬N個時間點的狀態。因此,輸入例如:matlab:praticle狀態模擬

N = 1000; 
f = 0.3; 
L = 5; 

,結果會像

state(1:N) = [0 0 1 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 ... and so on] 

與總和(州)/ N接近0.3

如何做到這一點? 謝謝!

+0

粒子翻轉狀態的概率是多少? – 2012-03-25 08:39:53

+0

我真的不知道你是什麼意思。我真正想要做的是模擬具有兩種不同擴散係數的粒子的擴散行爲,並且在一個狀態或另一個狀態中定義了更快和更慢的分量(f)和某種類型的壽命的一部分。我想首先模擬狀態(在本例中是兩個,但可能更多),然後根據狀態(更快或更慢)模擬位移和座標。我不知道這是否是最好的方法,但這是我心中的第一個方法:) – Art 2012-03-25 08:53:09

+0

@ NoamN.Kremen當f = 0.3,狀態1的長度爲5.狀態0的長度爲on平均值應該是17(5/0.3),所以翻轉從0到1的變化是0.06。編輯:不確定這個陳述是否完全正確。 – Bernhard 2012-03-25 10:00:00

回答

2
%% parameters 
f = 0.3; % probability of state 1 
L1 = 5; % average time in state 1 
N = 1e4; 
s0 = 1; % init. state 
%% run simulation 
L0 = L1 * (1/f - 1); % average time state 0 lasts 
p01 = 1/L0; % probability to switch from 0 to 1 
p10 = 1/L1; % probability to switch from 1 to 0 
p00 = 1 - p01; 
p11 = 1 - p10; 
sm = [p00, p01; p10, p11]; % build stochastic matrix (state machine) 
bins = [0, 1]; % possible states 
states = zeros(N, 1); 
assert(all(sum(sm, 2) == 1), 'not a stochastic matrix'); 
smc = cumsum(sm, 2); % cummulative matrix 
xi = find(bins == s0); 
for k = 1 : N 
    yi = find(smc(xi, :) > rand, 1, 'first'); 
    states(k) = bins(yi); 
    xi = yi; 
end 
%% check result 
ds = [states(1); diff(states)]; 
idx_begin = find(ds == 1 & states == 1); 
idx_end = find(ds == -1 & states == 0); 
if idx_end(end) < idx_begin(end) 
    idx_end = [idx_end; N + 1]; 
end 
df = idx_end - idx_begin; 
fprintf('prob(state = 1) = %g; avg. time(state = 1) = %g\n', sum(states)/N, mean(df)); 
+0

這真棒,謝謝很多Serg! – Art 2012-03-26 03:28:58

2

激發態的平均長度爲5.正常狀態的平均長度,因此應該在12左右才能獲得。

策略可以是這樣的。

  • 開始在狀態0
  • 從與平均L*(1-f)/f
  • 泊松分佈繪製的隨機數aa零填充狀態陣列
  • 從與平均L一個泊松分佈繪製的隨機數b
  • 用狀態數組填充b
  • 重複

另一種選擇是考慮在切換概率,其中0-> 1和1-> 0的概率是不相等的條件。