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

조회 수: 2 (최근 30일)
Anthony DiMascio
Anthony DiMascio 2020년 11월 23일
답변: Mario Malic 2020년 11월 23일
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?

답변 (1개)

Mario Malic
Mario Malic 2020년 11월 23일
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

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by