Search code examples
matlab

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


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

Solution

  • 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)<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);