How to use two conditions in a same loop?

2 次查看(过去 30 天)
How to use two conditions in a single loop? for example i wnat to check the any value of x (cloumn) if it is less than 1, the code shall run iteration till any value reaches to 1. when any one value of x cloumn reaches to 1, the code shall proceed to the next row and repeat the same for this row, and run the code till the last column has the one value reaches to 1. here I am posting my code. Please help. Thanks in advance
i = 1:n-1
x=0;
while x<1
itn=itn+1
Rhn(1:n,i) = ((pi*rh(1:n,i).^4)/8*nuD*L).*[1./(x+(1-x)*M)];
qhn(1:n,i)=qh(1:n,i).*Rh(1:n,i)./Rhn(1:n,i);
Vh(1:n,i) = (qhn(1:n,i))./Ah(1:n,i);
if x<=0.5
delXmax = 0.1
else
delXmax = 0.07
end
deltat=min(min(delXmax./Vh(1:n,i)))
x= x+Vh(1:n,i)*deltat;
if x>=1
i = i+1
end
end
end

回答(1 个)

ag
ag 2024-10-3
Hi Irfan,
I understand that you want to iterate over columns of a matrix, checking each column's values to see any value of x is less than 1. And that once a value in the current column reaches 1, the iteration should move to the next column and repeat the process until all columns have been processed.
The below code outlines how you can structure the required logic:
% Assuming n and other utilized functions are pre-defined
itn = 1;
for i = 1:n-1
x = 0; % Initialize x for each column
while any(x < 1) % Check if any value of x is less than 1
itn = itn + 1;
% Update Rhn, qhn, and Vh based on provided code
Rhn(1:n, i) = ((pi * rh(1:n, i).^4) / (8 * nuD * L)) .* (1 ./ (x + (1 - x) * M));
qhn(1:n, i) = qh(1:n, i) .* Rh(1:n, i) ./ Rhn(1:n, i);
Vh(1:n, i) = qhn(1:n, i) ./ Ah(1:n, i);
% Determine delXmax based on the value of x
if any(x <= 0.5)
delXmax = 0.1;
else
delXmax = 0.07;
end
% Calculate deltat and update x
deltat = min(min(delXmax ./ Vh(1:n, i)));
x = x + Vh(1:n, i) * deltat;
if any(x >= 1)
break; %move to the next column
end
end
end
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by