0

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;
Markus A.
  • 6,300
  • 10
  • 26
VINE
  • 5
  • 2

2 Answers2

5

You have multiple "assignments" to alpha and beta.

In each iteration of the for loop a new equation is set up for alpha and beta, resulting in 4 too many.

The work around will be using 'algorithm' section instead of 'equation' this will locally balance the variable and equations as you wish.

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);
    T[i + 1] := T[i];
  end for;
end Test1;
Akhil Nandan
  • 315
  • 2
  • 9
Rene Just Nielsen
  • 3,023
  • 12
  • 15
  • ohn,i see, thank u very much, one more question, this code is a test, if it is `T[100]` or more , can i assign `alpha` and `beta` in a `for` loop – VINE Oct 11 '22 at 06:59
  • If you want to assign `alpha` and `beta` in the loop within an `equation`-section, they have to have the same size as `T`. – Markus A. Oct 11 '22 at 07:07
2

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.

Markus A.
  • 6,300
  • 10
  • 26