Borrar filtros
Borrar filtros

Nested While and For Loop

2 visualizaciones (últimos 30 días)
HoboHarry
HoboHarry el 31 de Oct. de 2018
Respondida: Stephen23 el 31 de Oct. de 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

Respuesta aceptada

Torsten
Torsten el 31 de Oct. de 2018
Eqn2 = @(x)zeros(size(x));

Más respuestas (1)

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

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by