Why do i receive error in line 36:Index in position 1 is invalid. Array indices must be positive integers or logical values.
Mostra commenti meno recenti
%start by defining the parameters
N = 50; % Number of particles
domainSize = 99; % Size of the simulation domain
s = 3/5; % Probability to move South
w = 1/5; % Probability to move West
e = 1/5; % Probability to move East
% Initialize variables to store particle positions and results
particlePositions = zeros(N, 2); % Each row stores [row, column] position
reachedBottom = zeros(N, 1); % 1 if particle reached the bottom, 0 otherwise
% Loop over particles and simulate biased walks
for i = 1:N
% Choose start position
if rand < 0.5
startColumn = 1; % Particle starts at column 1 (P = 1)
else
startColumn = randi(domainSize); % Random start position (rand)
end
particlePositions(i, :) = [1, startColumn]; % Initialize particle position
% Simulate biased walk
while particlePositions(i, 1) < domainSize % While particle not at bottom row
% Choose direction with probabilities
direction = rand();
if direction < s
particlePositions(i, 1) = particlePositions(i, 1) + 1; % Move South
elseif direction < s + w
particlePositions(i, 2) = mod(particlePositions(i, 2) - 2, domainSize) + 1; % Move West
else
particlePositions(i, 2) = mod(particlePositions(i, 2), domainSize) + 1; % Move East
end
% Check if particle's next position is occupied
if particlePositions(i, 1) < domainSize && ...
particlePositions(i, 2) == particlePositions(i -1, 2) && ...
particlePositions(i, 1) == particlePositions(i -1, 1) + 1
reachedBottom(i) = 1; % Particle reached bottom row
break;
end
end
end
% Calculate statistics
reachedBottomN50 = sum(reachedBottom);
reachedBottomPctN50 = reachedBottomN50 / N * 100;
% Display results
fprintf('For N = %d particles:\n', N);
fprintf('Number of particles reaching bottom row: %d\n', reachedBottomN50);
fprintf('Percentage of particles reaching bottom row: %.2f%%\n', reachedBottomPctN50);
Risposte (1)
Walter Roberson
il 17 Ago 2023
for i = 1:N
i starts at 1.
% Check if particle's next position is occupied
Inside the for loop, i has not been reassigned anything by the time you reach this line. On the first iteration, i will still be 1.
if particlePositions(i, 1) < domainSize && ...
particlePositions(i, 2) == particlePositions(i -1, 2) && ...
particlePositions(i, 1) == particlePositions(i -1, 1) + 1
On the first iteration, when i is 1, is there any reason why particlePositions(i,1) should definitely be >= domainSize so that the test fails immediately? Because if the test does not fail immediately and you proceed to the next clause, then particlePositions(i -1,2) is used for the calculation, but when i is 1, that is particlePositions(1-1,2) which is particlePositions(0,2) which is not a valid index.
2 Commenti
Sophie
il 17 Ago 2023
Walter Roberson
il 17 Ago 2023
particlePositions(i, :) = [1, startColumn]; % Initialize particle position
when i is 1, you are initializing the first column of particlePositions(1,:) to be 1.
domainSize = 99; % Size of the simulation domain
1 is < 99 so
while particlePositions(i, 1) < domainSize % While particle not at bottom row
starts out true
if direction < s
particlePositions(i, 1) = particlePositions(i, 1) + 1; % Move South
that random selection might not be true, but if it is then the position would increment from 1 to 2; if it is false, the position stays where it is. Going through the first iteration, after the end of that if and all elseif and else we know the position will be either 1 or 2.
if particlePositions(i, 1) < domainSize && ...
particlePositions(i, 2) == particlePositions(i -1, 2) && ...
particlePositions(i, 1) == particlePositions(i -1, 1) + 1
1 and 2 are both < 99, so the first part of the test will be true, and you proceed with the rest of the test, which is trying to loop at the previous particle position... but there is no previous particle position.
By the way, I would suggest that you consider code such as
just_moved_south = false;
if direction < s
particlePositions(i, 1) = particlePositions(i, 1) + 1; % Move South
just_moved_south = true;
elseif etc
end
if particlePositions(i, 1) < domainSize && ...
particlePositions(i, 2) == particlePositions(i -1, 2) && ...
just_moved_south
if that is what your code is intending by the third part of that test.
Categorie
Scopri di più su Programming in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!