While loop is not recognizing a defined variable
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am currently getting an error that a defined variable set used in the decision of a while loop ie( while x < j, with x being the issue) that that x value is undefined at some point after the loop has been running multiple times. I have checked and that value x is defined both before and in the while loop.
The code I am running this in has a over 30 loops used throughout but there is only one location where this issue is occurring. Is it possible that the program is lagging and not reading the variable before it executes the while loop? Either way what would be the best way to fix this?
4 commentaires
Stephen23
le 8 Nov 2018
@Sam Crawford: please show us the complete error message. This means all of the red text.
Star Strider
le 8 Nov 2018
At several points in your code you specifically clear your ‘To6’ variable.
Réponses (2)
Stephen23
le 8 Nov 2018
Modifié(e) : Stephen23
le 8 Nov 2018
"While loop is not recognizing a defined variable"
Because that variable is not defined.
You clear To6 several times inside the while loop, on lines 2574 and 2592. It is quite easy to show some evaluation paths exist where you do not define To6 again. Here is a trimmed-down version of your code, showing just the relevant lines:
To6 = 0;
while To6 < To5_2max
...
To6 = (Efab * fab * HV + (1 + fb + fib) * cpab * To5_2) / ((1 + fb + fib + fab) * cpab); % stagnation temperature at the exit of the afterburner
...
if To6 > To5_2max
break
end
for nm = 1:2
...
if specific_thrust > reqST
...
clear Po6 To6 To5_2 To5_m To4 To5_1 To5_i To3 totaleff specific_thrust fb fib b fab; fab=0; fib=0; b=0; fb=0;
...
for bleed = 1:6
...
if fmin == 1
clear To6 To1 To4 To5_i
...
if To3 > To4imax
break % ??? What happens if you break here ???
end
...
else
...
if To3 > To4imax
break % ??? or here ???
end
...
while To4 < To4imax
...
if To4 > To4imax
break % ??? or here ???
end
...
To6 = 0;
...
if ab == 1
while To6 < To5_2max
...
To6 = (Efab * fab * HV + (1 + fb + fib) * cpab * To5_2) / ((1 + fb + fib + fab) * cpab); % stagnation temperature at the exit of the afterburner
...
if To6 > To5_2max
break
end
...
end
else
To6 = To5_m;
...
end
end
end
end
end
end
end
Consider the bleed for loop: if fmin==1 then you clear To6 and then if To3>To4imax then you break out of the bleed for loop. After that for loop To6 is not defined anywhere again, so the while condition tries to use a variable that does not exist anymore. You need to consider what happens to the variables when you break out of a loop where you have cleared the variable.
0 commentaires
Cris LaPierre
le 8 Nov 2018
Modifié(e) : Cris LaPierre
le 8 Nov 2018
You define To6 just before entering the while loop at line 2435, so the issue is within the loop (2435-3516) you are clearing To6 and not giving it a new value before the the loop restarts. You clear it in lines 2574 and 2592.
To reach this scenario,
- y = 2
- f = 1
- interburner = 1
- afterburner = 2
- fmin = 1
It therefore has nothing to do with the code getting ahead of itself, so you can remove the pause(2) in your code at line 2433. It's only increasing your run time.
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!