%% 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));
粒子翻轉狀態的概率是多少? – 2012-03-25 08:39:53
我真的不知道你是什麼意思。我真正想要做的是模擬具有兩種不同擴散係數的粒子的擴散行爲,並且在一個狀態或另一個狀態中定義了更快和更慢的分量(f)和某種類型的壽命的一部分。我想首先模擬狀態(在本例中是兩個,但可能更多),然後根據狀態(更快或更慢)模擬位移和座標。我不知道這是否是最好的方法,但這是我心中的第一個方法:) – Art 2012-03-25 08:53:09
@ NoamN.Kremen當f = 0.3,狀態1的長度爲5.狀態0的長度爲on平均值應該是17(5/0.3),所以翻轉從0到1的變化是0.06。編輯:不確定這個陳述是否完全正確。 – Bernhard 2012-03-25 10:00:00