This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/programming/code/simplerandomwalk.m

27 lines
472 B
Matlab

num_runs = 10;
max_steps = 1000;
positions = zeros(max_steps, num_runs);
for run = 1:num_runs
for step = 2:max_steps
x = randn(1);
if x < 0
positions(step, run) = positions(step-1, run) + 1;
elseif x > 0
positions(step, run) = positions(step-1, run) - 1;
end
end
end
figure()
hold on
for run = 1:num_runs
plot(1:max_steps, positions(:, run))
end
xlabel('Number of steps')
ylabel('Position')
box off