0

私は現在、モデリング「ランダムウォーク」と人口モデリングを実践しています。私は 1D ランダム ウォークで動作する作業コードを持っています。将来、次元を追加したいと考えています。現在のコードでは、それがより困難になると思います。for ループを開始する前に、最初の 2 つの位置をプロットする必要があります。理想的には、その問題を取り除き、最初のステップから開始したいと考えています。

numjumps = 20; %number of steps
R = 0.5; %probability of step to right
more off
prev_position = [0 0]; %first position
x = rand(1); %generate random number 0-1
if x >= R;
  step = 1; %take a step to the right
elseif x < R
  step = -1; %take a step to the left
end

position = [1 prev_position(2) + step];

close all;
figure;
hold on;

plot([prev_position(1) position(1)], [prev_position(2) position(2)]);
axis([0 numjumps -5 5])
for i = (2:numjumps);
  prev_position(i) = position(i-1);
  x = rand(1); %generate random number 0-1
  if x >= R;
    step = 1; %take a step to the right
  elseif x < R
    step = -1; %take a step to the left
  end
  position(i) = [prev_position(i) + step]; %add step to position
  plot(position);
  axis([0 numjumps -5 5])
end

total = sum(position);
mean_position = total/numjumps;
disp(mean_position);

どんな助けでも大歓迎です。

4

1 に答える 1