Filter löschen
Filter löschen

Nested While and For Loop

2 Ansichten (letzte 30 Tage)
HoboHarry
HoboHarry am 31 Okt. 2018
Beantwortet: Stephen23 am 31 Okt. 2018
Have the following code with an error stating "Array indices must be positive integers or logical values." Could someone point me in the direction of what this error means or why i'm getting it. It occurring in the second while and i'm assuming will happen again in the third
Eqn1 = @(x) 5*(1-erf(3*x));
Eqn2 = 0;
Eqn3 = @(x) 5*(1-erf(3*(x-30)));
X0 = 0;
X1 = 1;
X29 = 29;
X30 = 30;
Int1 = 10;
Int2 = 100;
Value1 = ((X1-X0)/Int1);
Value2 = ((X29-X1)/Int2);
AreaReq5 = 0;
while (X0 < X1)
AreaReq5 = AreaReq5+(Value1/2)*(Eqn1(X0)+Eqn1(X0+Value1));
X0 = X0+Value1;
while (X1 < X29)
AreaReq5 = AreaReq5+(Value2/2)*(Eqn2(X1)+Eqn2(X1+Value2));
X1 = X1+h5b;
end
while (X29 < X30)
AreaReq5 = AreaReq5+(Value1/2)*(Eqn3(X29)+Eqn3(X29+Value1));
XNow = X29+Value1;
end
end

Akzeptierte Antwort

Torsten
Torsten am 31 Okt. 2018
Eqn2 = @(x)zeros(size(x));

Weitere Antworten (1)

Stephen23
Stephen23 am 31 Okt. 2018
You define Eqn2 to be zero:
Eqn2 = 0;
and then inside the loop you try to access it using indexing:
Eqn2(X1+Value2)
When the error occurs the value of that index is:
>> X1+Value2
ans = 1.2800
Clearly this is not an integer.

Kategorien

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

Community Treasure Hunt

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

Start Hunting!

Translated by