MATLAB How to make a while loop stop when reaching a certain value

Click For Summary
The discussion focuses on how to effectively stop a while loop when a calculated value, Q_1, approaches a target value of 30,000. Initial attempts to use a while loop with a specific range or condition were unsuccessful, leading to confusion about logical operators and loop termination. The correct approach involves checking if Q_1 is greater than or less than the target value and adjusting T_1out accordingly. A refined code example demonstrates how to track the closest value to 30,000 by comparing previous and current Q_1 values. Ultimately, the goal is to ensure the loop exits when Q_1 is as close to 30,000 as possible.
WhiteWolf98
Messages
89
Reaction score
8
TL;DR
I basically want Q in the code to be 30,000 or something near to 30,000 (perhaps between two values, for example between 29,750 and 30,250). I don't know how to define the range though, and the code keeps going on infinitely
Matlab:
m_1 = 1;
C_p1 = 2219;
T_1in = 100 + 273.15;
T_1out = 75 + 273.15;

Q_1 = (m_1)*C_p1*(T_1in-T_1out);

while Q_1 ~= 30000
    T_1outnew = T_1out + 0.1;
    Q_1 = (m_1)*C_p1*(T_1in-T_1outnew);
    T_1out = T_1outnew;
end

T_1outnew keeps increasing, but the code never stops; and naturally, Q becomes negative
 
Physics news on Phys.org
I'm not sure how a for loop would help me here, I just want Q to be in a range. What if I defined another variable, which was a range, and used the while loop with that? For example:

Matlab:
m_1 = 1;
C_p1 = 2219;
T_1in = 100 + 273.15;
T_1out = 75 + 273.15;
Y = 29750:30250;

Q_1 = (m_1)*C_p1*(T_1in-T_1out);

while Q_1 ~= Y
    T_1outnew = T_1out + 0.1;
    Q_1 = (m_1)*C_p1*(T_1in-T_1outnew);
    T_1out = T_1outnew;
end

It still doesn't work, but I don't see how to use the for loop because I just want a check to see if something is between two numbers
 
WhiteWolf98 said:
I'm not sure how a for loop would help me here, I just want Q to be in a range. What if I defined another variable, which was a range, and used the while loop with that? For example:

Matlab:
m_1 = 1;
C_p1 = 2219;
T_1in = 100 + 273.15;
T_1out = 75 + 273.15;
Y = 29750:30250;

Q_1 = (m_1)*C_p1*(T_1in-T_1out);

while Q_1 ~= Y
    T_1outnew = T_1out + 0.1;
    Q_1 = (m_1)*C_p1*(T_1in-T_1outnew);
    T_1out = T_1outnew;
end

It still doesn't work, but I don't see how to use the for loop because I just want a check to see if something is between two numbers
Let's say you want Q_1 to be within the two values lower_lim and upper_lim. You can change your code to this:
Matlab:
m_1 = 1;
C_p1 = 2219;
T_1in = 100 + 273.15;
T_1out = 75 + 273.15;
lower_lim = 29750;
upper_lim = 30250;

Q_1 = (m_1)*C_p1*(T_1in-T_1out);

while (lower_lim < Q && Q < upper_lim)
    T_1outnew = T_1out + 0.1;
    Q_1 = (m_1)*C_p1*(T_1in-T_1outnew);
    T_1out = T_1outnew;
end
 
  • Like
Likes WhiteWolf98
I'm unfamiliar with &&, so I searched it up quickly. Since lower_lim < Q is not true, won't that stop the code there? I don't get any output anymore
 
Never mind, I apologise, while (lower_lim < Q && Q > upper_lim). One of the signs needed to be flipped.

Thanks a lot for your help, Wrichik
 
Last edited:
WhiteWolf98 said:
I'm unfamiliar with &&
That is logical AND. If you are unfamiliar with logical operators, I would advise you to stop here and go look them up. They are fundamental to any programming language.
WhiteWolf98 said:
Never mind, I apologise, while (lower_lim < Q && Q > upper_lim). One of the signs needed to be flipped.
That is not the way to check whether Q_1 is in a certain range.

You want Q_1 to be between 29750 and 30250. More explicitly, you want it to be greater than 29750 AND less than 30250.

With the code you posted, you are checking whether Q_1 is greater than 29750 AND greater than 30250. Does this seem correct?

I sense some ambiguity building up here. Let's look at what you wrote in the first post of this thread:
WhiteWolf98 said:
Summary:: I basically want Q in the code to be 30,000 or something near to 30,000
30000 is the critical point. You want your loop to give you the value of Q_1 that is nearest to 30000.

In the code inside the while loop, the value of Q_1 will decrease. At one point, it will be just greater than 30000 (say that value is Q_1a), and then it becomes just less than 30000 (Q_1b). If Q_1a - 30000 is less than 30000 - Q_1b, it means Q_1a is closer to 30000 than Q_1b. Otherwise, it means that Q_1b is closer to 30000. We will choose the final value of Q_1 accordingly, and break out of the loop.
Matlab:
m_1 = 1;
C_p1 = 2219;
T_1in = 100 + 273.15;
T_1out = 75 + 273.15;

Q_1 = (m_1) * C_p1 * (T_1in-T_1out);

old_Q1 = Q_1;
criticalValue = 30000;

while true
    T_1out = T_1out + 0.1;
    Q_1 = (m_1) * C_p1 * (T_1in - T_1out);
    
    if (Q_1 == criticalValue)
        % Q_1 is as near 30000 as possible -- it is equal to 30000. So
        % break from loop.
        break;
    elseif (Q_1 > criticalValue)
        old_Q1 = Q_1;
    else
        if ((old_Q1 - criticalValue) < (criticalValue - Q_1))
            Q_1 = old_Q1;
        end
        break;
    end
end
The value of Q_1 comes out to be 29956.4999999942 using the above code. The code assumes that Q_1 is > 30000 in line 6.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
Replies
5
Views
8K
Replies
7
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
4
Views
4K