Nested While and For Loop

2 次查看(过去 30 天)
HoboHarry
HoboHarry 2018-10-31
回答: Stephen23 2018-10-31
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

采纳的回答

Torsten
Torsten 2018-10-31
Eqn2 = @(x)zeros(size(x));

更多回答(1 个)

Stephen23
Stephen23 2018-10-31
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by