MATLAB Answers

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

1 view (last 30 days)
Anthony DiMascio
Anthony DiMascio on 23 Nov 2020
Commented: VBBV on 23 Nov 2020
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?

  0 Comments

Sign in to comment.

Answers (2)

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

  0 Comments

Sign in to comment.


VBBV
VBBV on 23 Nov 2020
hold on
Y = 1:30;
X = 1:100;
bar(X(end),Y*0.1,'stacked'); % scaled to represent bar with plot
You can use the above at the end of program

  1 Comment

VBBV
VBBV on 23 Nov 2020
If you dont want to scale, delete 0.1 and just use Y

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!