Create a histogram of y-axis positions at x=100?

2 views (last 30 days)
I'm simulating atomic jumps in a 1D lattice.
This code takes a series of 30 of a sequence of 100 random single integer atomic jumps and plots them on a single plot.
for i=1:30
n = 100;
P = zeros(n,1);
P(1) = 0; % Starting value
for i=2:n % i is the number of steps from 1 to 100
R = rand;
if R < 0.5
S = -1;
elseif R > 0.5
S = 1;
end
P(i) = S+P(i-1) % Gives the next random walk from the new position P every time
end
ylabel('Position')
xlabel('Jump Count')
title('Random Atomic Jumps in a 1-D Lattice')
plot(1:n,P)
hold on
end
Can you help provide the code to create a histogram of the 30 y-axis positions on this plot, at x=100?

Answers (1)

Mario Malic
Mario Malic on 23 Nov 2020
Hello,
I have changed a bit of your code so it doesn't overwrite the values within i loop.
n = 100;
P = zeros(i,n); % Array initialisation
for i=1:30
P(1) = 0; % Starting value
for j=2:n % i is the number of steps from 1 to 100
R = rand;
if R < 0.5
S = -1;
elseif R > 0.5
S = 1;
end
P(i,j) = S+P(i,j-1); % Gives the next random walk from the new position P every time
end
ylabel('Position')
xlabel('Jump Count')
title('Random Atomic Jumps in a 1-D Lattice')
end
plot(1:n, P);
histogram(P(:,end)); % Gets the last value of each series

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!