MATLAB – “else” 在 “if” 条件不再为真时不会运行

huangapple go评论52阅读模式
英文:

MATLAB - "else" does not run after "if" condition is no longer true

问题

我遇到了一些困难,无法弄清楚为什么在条件sum(T(:,j)<Tm)==layersT(:,j)<Tm不再成立后,“else”部分不会运行。

clc
clear all
rho = 0.9;
k = 0.0018;
cp = 1.92;
l = 0.1525;
layers = 10;
dx = l/layers;
z = l-(dx/2): - dx: dx/2;
time = 25;
dt = 1;
t = 1: dt: time;
T0 = 25;
Tm1 = 165;
Tm2 = 135;
X01 = 0.25;
X02 = 0.3;
F0 = 2.202;
F = zeros(1,length(t));
T = zeros(length(z), length(t));
n = 1;
modulatingcycles = 4;

Tm = Tm1*ones(length(z),1);
for i = 1:round(length(z)*X01/(X01+X02))
    ind1 = randi([1 length(z)],1,1);
    Tm(ind1) = Tm2;
end

for j = 1 : length(t)
    if T(:,j)<Tm
        F(j) = F0;
        n = n + 1;
    else
        x = ceil((length(t) - n)/modulatingcycles);
        y = n;
        F(y:y+x) = F0/2;
        F(y+x+1:y+x+x) = F0;

        if j == y + x + x
            y = y + x + x + 1;
        end
    end
    for i = 1 : length(z)
        T(i,j) = T (i,j) + T0 + ((F(j)*t(j))/(rho*cp*l)) + ((F(j)*l/k) * (3*(z(i)^2) - (l^2)) / (6*l^2));
    end
}
英文:

I am having some trouble figuring out why my "else" section would not run after the condition sum(T(:,j)<Tm)==layers or T(:,j)<Tm is no longer true.

clc
clear all
rho = 0.9;                      
k = 0.0018;                     
cp = 1.92;                      
l = 0.1525;                
layers = 10; 
dx = l/layers;                  
z = l-(dx/2): - dx: dx/2;       
time = 25;  
dt = 1; 
t = 1: dt: time;                        
T0 = 25;                        
Tm1 = 165;                      
Tm2 = 135;                      
X01 = 0.25;                     
X02 = 0.3;                      
F0 = 2.202;                     
F = zeros(1,length(t));
T = zeros(length(z), length(t)); 
n = 1;
modulatingcycles = 4;

Tm = Tm1*ones(length(z),1);
for i = 1:round(length(z)*X01/(X01+X02))
    ind1 = randi([1 length(z)],1,1);
    Tm(ind1) = Tm2;              
end

for j = 1 : length(t)                                    
    if T(:,j)<Tm  
       %sum(T(:,j)<Tm)==layers      
        F(j) = F0;
        n = n + 1;
    else
        x = ceil((length(t) - n)/modulatingcycles);
        y = n;
        F(y:y+x) = F0/2;
        F(y+x+1:y+x+x) = F0;
        
        if j == y + x + x
            y = y + x + x + 1;
        end 
    end                         
    for i = 1 : length(z)
        T(i,j) = T (i,j) + T0 + ((F(j)*t(j))/(rho*cp*l)) + ((F(j)*l/k) * (3*(z(i)^2) - (l^2)) / (6*l^2));
    end
end

答案1

得分: 2

对于你提供的代码部分,以下是翻译的结果:

"A simple debugging shows that the T(:,j)<Tm is always true, hence the else branch never executes."

简单的调试显示T(:,j)<Tm始终为真,因此else分支永远不会执行。

"I've slightly modified your code to add the 'condition_results' matrix that stores the result of the T(:,j)<Tm condition on each iteration of the loop. After the loop, I print the 'condition_results'. Run it and you'll see that each element of this matrix is '1' ('true'). That shows that T(:,j)<Tm is always true (why it's always 'true' is a whole another question, though)."

我稍微修改了你的代码,添加了存储循环每次迭代中T(:,j)<Tm条件结果的'condition_results'矩阵。循环结束后,我打印了'condition_results'。运行代码,你会看到这个矩阵的每个元素都是'1'('true')。这表明T(:,j)<Tm始终为真(为什么它总是为真是另一个问题,不过)。

"Hope that helps. Here's the modified code:"

希望这有所帮助。以下是修改后的代码:

英文:

A simple debugging shows that the T(:,j)<Tm is always true, hence the else branch never executes.

I've slightly modified your code to add the 'condition_results' matrix that stores result of the T(:,j)<Tm condition on each iteration of the loop. After the loop I print the 'condition_results'. Run it and you'll see that each element of this matrix is '1' ('true'). That shows that T(:,j)<Tm is always true (why it's always 'true' is a whole another question, though).

Hope that helps. Here's the modified code:

clc
clear all
rho = 0.9;                      
k = 0.0018;                     
cp = 1.92;                      
l = 0.1525;                
layers = 10; 
dx = l/layers;                  
z = l-(dx/2): - dx: dx/2;       
time = 25;  
dt = 1; 
t = 1: dt: time;                        
T0 = 25;                        
Tm1 = 165;                      
Tm2 = 135;                      
X01 = 0.25;                     
X02 = 0.3;                      
F0 = 2.202;                     
F = zeros(1,length(t));
T = zeros(length(z), length(t)); 
n = 1;
modulatingcycles = 4;

Tm = Tm1*ones(length(z),1);
for i = 1:round(length(z)*X01/(X01+X02))
    ind1 = randi([1 length(z)],1,1);
    Tm(ind1) = Tm2;              
end

condition_results = [];

for j = 1 : length(t)
    result = T(:,j)&lt;Tm
    condition_results = [condition_results result];
    if T(:,j)&lt;Tm  
       %sum(T(:,j)&lt;Tm)==layers      
        F(j) = F0;
        n = n + 1;
    else
        x = ceil((length(t) - n)/modulatingcycles);
        y = n;
        F(y:y+x) = F0/2;
        F(y+x+1:y+x+x) = F0;
        
        if j == y + x + x
            y = y + x + x + 1;
        end 
    end
    for i = 1 : length(z)
        T(i,j) = T (i,j) + T0 + ((F(j)*t(j))/(rho*cp*l)) + ((F(j)*l/k) * (3*(z(i)^2) - (l^2)) / (6*l^2));
    end
end

disp(condition_results);

huangapple
  • 本文由 发表于 2023年2月27日 01:38:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定