Attempted to access dz(1,1000); index out of bounds because numel(dz)=1.

1 Ansicht (letzte 30 Tage)
Alex
Alex am 23 Okt. 2013
Beantwortet: Walter Roberson am 23 Okt. 2013
Could you please check my codes? The error message "Attempted to access dz(1,1000); index out of bounds because numel(dz)=1." appears at the third last line.
k = 0.5;
t = 1;
n = 100;
M = 1000;
dt = t/n;
randn('state',101);
dz = randn()*sqrt(dt);
for m = 1:M
for i = 0:n-1
y((i+1),M) = exp(-k*(n*(dt))-i*(dt))*dz((i+1),M);
end;
end;

Antworten (2)

sixwwwwww
sixwwwwww am 23 Okt. 2013
Dear Alex, here is your working code, I made some modifications(If i understood correctly what you want to do):
k = 0.5;
t = 1;
n = 100;
M = 1000;
dt = t/n;
dz = randn(1000)*sqrt(dt);
i = 0:n - 1;
for m = 1:M
y(i+1, m(1:end)) = exp(-k*(n*(dt))-i*(dt))*dz(i+1,m(1:end));
end
In your code you initializing "dz" with 101x101 elemets however in your for loop you were trying to access element out of this range. I hope it is what you need. Good luck!

Walter Roberson
Walter Roberson am 23 Okt. 2013
The subexpression dz((i+1),M) on the first trip through the "for i" loop, is going to be trying to use dz((0+1),M) which is dz(1,1000) . But you initialized dz = randn()*sqrt(dt); which is only a single value not a vector of length 1000.
Perhaps you wanted
dz = randn(n, M)*sqrt(dt);
Caution: when you use a variable named "i", then when you refer to "i" in a formula, it becomes difficult to know whether you meant the variable from the for loop, or meant the built-in value for "i" as sqrt(-1). It is recommended that you do not use variables named "i" or "j".

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by