Search code examples
modelica

How can i fix this problem? There are 4 redundant equations


Too easy but I can`t fix it. Thank you very much.

Error:
There are 4 redundant equations

Code:

model Test1
  Real T[4];
  Real S[3];
  Real alpha;
  Real beta;
equation 
  T[1] = 5;
  for i in 1:3 loop 
    if T[i] > 2 then 
      alpha = 2;
      beta = 1;
    else
      alpha = 1;
      beta = 2;
    end if;
    S[i] = T[i] - 1;
    T[i + 1] = T[i] - (alpha * 2) / (2 * S[i] + beta);
  end for;
end Test1;

Solution

  • In case you use an equation-section (see MBE: equations), you need to make sure that you have the same number of equations and unknowns/variables. In your code, this is not the case for alpha and beta. The reason is, that these are only two variables, but for each of them, three equations are generated in the for-loop. So you have six equations and two unknowns, which is a difference of four redundant equations.

    There are multiple ways to fix this issue:

    1. Balance the number of equations

    By only generating a single equation for alpha and beta: You need to decide, based on which entry of the vector T[:] the if-statement shall assign the variables. Using entry three, this could look like the following code:

    model Test1
      Real T[4];
      Real S[3];
      Real alpha;
      Real beta;
    
    equation 
      T[1] = 5;
      
      for i in 1:3 loop
        S[i] = T[i] - 1;
        T[i + 1] = T[i] - (alpha * 2) / (2 * S[i] + beta);
      end for;
      
      if T[3] > 2 then
        alpha = 2;
        beta = 1;
      else
        alpha = 1;
        beta = 2;
      end if;  
        
    end Test1;
    

    2. Use assignments

    Instead of generating equations, Modelica can also handle imperative code. This way, variables can be assigned multiple times with only the last assignment setting the value for the current time step.

    The algorithm below will overwrite the values for alpha and beta two times and use the values from the last assignments.

    model Test1
      Real T[4];
      Real S[3];
      Real alpha;
      Real beta;
    
    algorithm 
      T[1] :=5;
    
      for i in 1:3 loop
        if T[i] > 2 then
          alpha :=2;
          beta :=1;
        else
          alpha :=1;
          beta :=2;
        end if;
    
        S[i] :=T[i] - 1;
        T[i + 1] :=T[i] - (alpha*2)/(2*S[i] + beta);
    
      end for;
    end Test1;
    

    Note

    Usually, it is preferred to use equations over algorithm. Algorithms are the right way to go if the order of the assignments is of importance. A bit more information can be found here.