Can't figure out how to fix endless while loop

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

Where were you resetting it?
You want to reset Prf for each iteration, but each iteration of what variable?
I guess a better question would be is there a way to go through the while loop with each iteration using a new element of To_3 and Po_4?
My To_3 and Po_4 are arrays of values right now and if I manually input say To_3(1) and Po_4(1) and then run the while loop to find Prf and then do it again but manually changing To_3 and Po_4 to their second element in their arrays (i.e. To_3(2) and Po_4(2) ) and so on, I get the correct Prf values. I'm not sure how to traverse across the To_3 and Po_4 arrays automatically though.
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.
Thanks! How could I save the value of each iteration in an array? I don't have the best understanding of loops.

Sign in to comment.

Answers (2)

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

How would you stop k from incrementing? I'm running into the issue of index exeeds number of array elements. I have 19 elements in the array. I tried inputting another while loop for k < 20 but it doesn't compute then.
"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

Sign in to comment.

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

Asked:

on 1 Nov 2022

Commented:

Jan
on 2 Nov 2022

Community Treasure Hunt

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

Start Hunting!