MATLAB -“如果”条件不再为真后,“else”不运行

yvgpqqbh  于 2023-03-03  发布在  Matlab
关注(0)|答案(1)|浏览(295)

我很难弄清楚为什么在条件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  
       %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
alen0pnh

alen0pnh1#

一个简单的调试表明T(:,j)〈Tm始终为真,因此else分支永远不会执行。
我稍微修改了代码,添加了'condition_results'矩阵,该矩阵存储了T(:,j)〈Tm条件在循环每次迭代时的结果。在循环结束后,我打印了'condition_results'。运行它,您将看到该矩阵的每个元素都是'1'('true ')。这表明T(:,j)〈Tm总是为真(但为什么它总是'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

condition_results = [];

for j = 1 : length(t)
    result = T(:,j)<Tm
    condition_results = [condition_results result];
    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

disp(condition_results);

相关问题