Nested While and For Loop

1 view (last 30 days)
HoboHarry
HoboHarry on 31 Oct 2018
Answered: Stephen23 on 31 Oct 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

Accepted Answer

Torsten
Torsten on 31 Oct 2018
Eqn2 = @(x)zeros(size(x));

More Answers (1)

Stephen23
Stephen23 on 31 Oct 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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!