Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

For and while loop problem

2 visualizaciones (últimos 30 días)
Yorick de
Yorick de el 20 de Sept. de 2020
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
High guys, I have about my loop containing a for and while loop. The code only produces 1 value for Sd, but in all cases T(i) >T(i+1). Does anyone know what my mistake is? Thanks!
%parameters
cp = 1000;
g = 9.81;
n = 1;
Sd = 0;
T = [292,760000000000 290,760000000000 290,260000000000 289,660000000000 289,060000000000 288,560000000000 287,860000000000 287,360000000000 286,760000000000 286,260000000000 285,860000000000]
h = [4 45 95 154 216 278 336 396 458 512 565]
for i = n
while T(i) > T(i+1)
Sd(i) = cp*T(i)+ g*h(i);
end
n = n+1;
end
  2 comentarios
the cyclist
the cyclist el 20 de Sept. de 2020
Editada: the cyclist el 20 de Sept. de 2020
It is a little difficult to know how to help you with this code.
You actually have two for loops over the same looping variable, i. That's a problem.
You have three different loops, but only two end statements. That's a problem.
Your for loop(s)
for i = n
will only "loop" over one value, because n is just a single value. That doesn't seem intentional.
All in all, I'm not sure how to help.
Yorick de
Yorick de el 20 de Sept. de 2020
That first for loop was incorrectly placed, sorry for that. I edited the problem like I wanted to state it in the first place.

Respuestas (1)

Walter Roberson
Walter Roberson el 20 de Sept. de 2020
while T(i) > T(i+1)
Sd(i) = cp*T(i)+ g*h(i);
end
Whenever you have a while loop, the condition is tested first, and if the condition is not all non-zero then the body of the loop is skipped. In that situation, the body of the loop is not executed even once.
If the condition is all non-zero, then the body of the loop will be executed once. Only the statements before the matching end will be executed. Then the statement will loop back to the begining and the condition will be tested again, using the current values of any variables (as altered by the body of the loop.) If the condition now has any zeros then the loop will be terminated, and otherwise the body of the loop will be executed again. And so on.
Therefore, if you enter into the body of the loop even once, then the only way to stop executing the body of the loop is if something affecting the condition being tested changes.
In terms of your code, your assignment to Sd(i) is going to be done indefinitely unless you change one of T(i) or T(i+1) or i . But you do not. So if your loop gets entered at all, then it will never be exited, with your code.
for i = n
Any changes to i because of the loop cannot happen until after the nested while loop terminates. The while loop is not proceeding "in parallel" with the for loop: the for is not continued until the while is finished.
What would be potentially valid is
for i = n
if T(i) > T(i+1)
Sd(i) = cp*T(i)+ g*h(i);
end
n = n+1;
end
However...
When you execute a for loop, the expression to the right hand side of the = is evaluated, and the result is recorded internally. Any changes afterwards to the variables mentioned in the expression do not have any effect on the execution of the while loop. Your n is a scalar value at the time the for loop is started, and that scalar value is recorded internally, and the n = n+1 you have does nothing to change how many times the for loop executes or the values that i will be set to.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by