Borrar filtros
Borrar filtros

Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

index exceeds matrix dimensions

2 visualizaciones (últimos 30 días)
Gigglemello
Gigglemello el 16 de Mzo. de 2017
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
Hello, I've tried to do a reflecting random walk simulator 1d. I've did it first without the barrier and it graphs perfectly. However, when I tried adding reflecting barrier to it, it kept on producing the "index exceeds matrix dimension". Do you know what is causing the error? Thank you.
% This program will compute and plot the paths of a set of many
% random walkers, which are confined by a pair of barriers at +B and -B.
% Assume all walkers start at z = 0
nwalks = 10;
nsteps = 100;
figure;
hold on;
for j = 1:nwalks
x = randn(nsteps,1);
z = zeros(nsteps,1);
for k = 1:nsteps
% Reflecting barrier
B = 3;
if z(k+1) > B
z(k+1) = B - abs(B - z(k+1));
elseif z(k+1) < (-B)
z(k+1) = (-B) + abs((-B) - z(k+1));
end
z(k+1) = z(k) + x(k);
end
% Plot
plot(z);
xlabel('Time (step number)'), ylabel(' Distance');
hold off;
end

Respuestas (1)

Image Analyst
Image Analyst el 16 de Mzo. de 2017
z has nsteps elements. k goes from 1 to nsteps. But in the loop you are trying to assign z(k+1), in other words, z(nsteps + 1). Try k = 1:(nsteps-1)

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by