Can't figure out how to fix endless while loop
Show older comments
In my code, I am running into a problem with my while loop. I want to reset the Prf variable back to it's initial value of one for each iteration (using Prf = 1;) but then it turns into an endless loop and I'm not sure why.
The code below does not include the value reset and that's the only way I can get a value output but it's incorrect and I'm not sure how to get it to work.
%Givens
M = u / sqrt(1.4*R*T_atm);
To_2 = T_atm*(1+((0.2)*(M^2)));
To_a = To_2;
Po_2 = P_atm*((1+ef_d*(To_2/T_atm - 1))^(gma_d/(gma_d-1)));
%Po3 and To3 related to Prc
for Prc = 7:25
Po_3_zeros(Prc,1) = Prc*Po_2;
To_3_zeros(Prc,1) = To_2*(1+(1/ef_c)*(Prc^((gma_c-1)/gma_c)-1));
end
%getting rid of zeros in array
Po_3 = nonzeros(Po_3_zeros);
To_3 = nonzeros(To_3_zeros);
%Value initialization
Po_5 = 10;
Po_8 = 300000;
Prf = 1;
To_4 = 1875;
Po_4 = Po_3;
Prc = 7:25;
%While loop to find Prf values
while abs(Po_5-Po_8)>100
Po_8 = Po_2*Prf;
To_8 = To_2*(1+(1/ef_f)*(((Prf)^((gma_f-1)/gma_f))-1));
To_5 = To_4 - (To_3-To_2) - B*(To_8-To_a);
Po_5 = Po_4.*((1-(1/ef_t).*(1-(To_5/To_4))).^(gma_t/(gma_t-1)));
Prf = Prf+0.0001;
end
4 Comments
Walter Roberson
on 1 Nov 2022
Where were you resetting it?
You want to reset Prf for each iteration, but each iteration of what variable?
James
on 1 Nov 2022
Jan
on 1 Nov 2022
Maybe something like this:
k = 1;
while abs(Po_5 - Po_8) > 100
Po_8 = Po_2*Prf;
To_8 = To_2 * (1 + (1 / ef_f) * (Prf^((gma_f - 1) / gma_f) - 1));
To_5 = To_4 - (To_3(k) - To_2) - B * (To_8 - To_a);
Po_5 = Po_4 .* ((1-(1/ef_t) .* (1 - To_5/To_4(k))) .^ (gma_t / (gma_t-1)));
Prf = Prf + 0.0001;
k = k + 1;
end
I've reduced the number of parentheses and inserted some spaces to improve the readability.
James
on 1 Nov 2022
Answers (2)
Jan
on 1 Nov 2022
k = 1;
Output = zeros(size(To_3)); % Pre-allocation
while abs(Po_5 - Po_8) > 100
Po_8 = Po_2 * Prf;
To_8 = To_2 * (1 + (1 / ef_f) * (Prf^((gma_f - 1) / gma_f) - 1));
To_5 = To_4 - (To_3(k) - To_2) - B * (To_8 - To_a);
Po_5 = Po_4 .* ((1 - 1 / ef_t .* (1 - To_5 / To_4(k))) .^ (gma_t / (gma_t-1)));
Prf = Prf + 0.0001;
Output(k) = ???
k = k + 1;
end
You can add an index to the other used variables also.
2 Comments
James
on 1 Nov 2022
Jan
on 2 Nov 2022
"I tried inputting another while loop for k < 20" - please post the code instead of describing what you expect the code to do. I cannot guess, what the problem is.
Maybe you want:
while abs(Po_5 - Po_8) > 100
... as above
k = k + 1;
if k == 20
warning('Did not converge');
break
end
end
or
while abs(Po_5 - Po_8) > 100 && k < 20
... as above
end
Walter Roberson
on 1 Nov 2022
I suspect you are looking for something like
initial_Prf = 1;
num_k = length(To_3);
Output = zeros(num_k, 1); % Pre-allocation
for k = 1 : num_k
%Value initialization
Po_5 = 10;
Po_8 = 300000;
Prf = initial_Prf + 0.0001 * (k-1);
To_4 = 1875;
Po_4 = Po_3;
Prc = 7:25;
while abs(Po_5 - Po_8) > 100
Po_8 = Po_2 * Prf;
To_8 = To_2 * (1 + (1 / ef_f) * (Prf^((gma_f - 1) / gma_f) - 1));
To_5 = To_4 - (To_3(k) - To_2) - B * (To_8 - To_a);
Po_5 = Po_4 .* ((1 - 1 / ef_t .* (1 - To_5 / To_4(k))) .^ (gma_t / (gma_t-1)));
end
Output(k) = ???
end
Categories
Find more on Programming 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!