Help me fix my code - Random Walk
2 次查看(过去 30 天)
显示 更早的评论
Hey guys, I'm working on plotting an entropy graph based off a random walk. I've done most of it but now I'm stuck. The lecturer helping me has given me advice and I'm just wondering if you all could help me.
Firstly, the code is:
tic
title(' "Cream in coffee" ');
grid on
prompt = 'How many steps do you want to take? ';
N_max = input(prompt);%step/time
prompt = 'How many molecules will there be? ';
M = input(prompt);%molecules
N_size=100;%Sizing of container
move=mod(floor(4*rand(1,M)),4);
drawnow
L(1)=0;
part=[zeros(1,M);zeros(1,M)];
figure
plot (part(1,:),part(2,:),'o','MarkerFaceColor','k','MarkerSize',5,'MarkerEdgeColor','none'),...
axis([-N_size N_size -N_size N_size]);
i = 1:50;
j = 1:50;
p = 1:10;
q = 1:10;
u = p .* 5;
l = (p-1) .* +1;
Pu = u./.5;
Pl = (l-1)./5 +1;
B = zeros(10);
for i = 1:50;
for j = 1:50;
p = ceil(i./5);
q = ceil(j./5);
% B(p,q) = B(p,q) + M(i,j);
end
end
P = B./N;
S = zeros(0);
for p = 1:10
for q = 1:10
S = S + P(p,q) .* (log (P(p,q)));
end
end
%Algorithm
for N = 1:N_max,
%Determine the infinite elements
ym=isinf(1./move);
xp=isinf(1./(move-1));
yp=isinf(1./(move-2));
xm=isinf(1./(move-3));
%part is a variable of type double
%A single : in a subscript position is shorthand notation for 1:end and is often used to select entire rows or columns
part(1,:) = part(1,:) + xp(1,:) - xm(1,:);
part(2,:) = part(2,:) + yp(1,:) - ym(1,:);
move=mod(floor(4*rand(1,M)),4);
plot(part(1,:),part(2,:),'o','MarkerFaceColor','k','MarkerSize',5,'MarkerEdgeColor','none'),...
set(gca,'Color',[1 1 1]);
axis([-N_size N_size -N_size N_size]);
pause(0.5)
dist=part(1,:).^2+part(2,:).^2;
L(N+1)= mean(dist);
end
%Entropy graph
% p is the probability of one class i.e how many times it appears in a grid box
% Just going to ask user for probability, for now, makes it easier.
% time = 0:N_max;%x
% p = 1;
% S = -sum(p .* log(p)):N_max;%y
% x = (time);
% y = (s);
% figure
% plot(x,y)
%
% title('Entropy versus time') %title
% xlabel('time (steps)') % x-axis label
% ylabel('Entropy') % y-axis label
% legend('Mixing of cream in coffee') %legend
toc
The advice given was as followed
The first thing is that the block of code to measure the entropy is not in the time loop. This needs to be called at each time step and after you have done the diffusion step.
Try to implement this yourself first.
Next you need a way to identify how many particles are within the box. I had thought you had a matrix keeping track of the number of particles at each location but instead you have a matrix of particles recording their position. You either need to identify how
1.To change these records of particles into a lattice of sites recording the number of particles at each site or
2.To change the algorithm for measurement of entropy to identify for each particle what its location is and thus what box it is in.
Also your system size is N_size=200 and by the looks of it it goes from -200 to 200 making the system 401 square in size. The algorithm will need adjustment to take account of this bigger system.
Thanks in advance
0 个评论
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!