Help - Search - Members - Calendar
Full Version: Nested loops
Modelica Forum > Modelica > Modelica
Rene Nölle
Hello everybody

I've read in the book of Mr.Tiller (chapter 6, page 152) that piece of code related to nested for loops

CODE
   Real A[5,3];
   Real B[3,7];
   Real C[5,7];
  
algorithm
  C = fill(0,5,7);
  for i in 1:size(A,1) loop
    for j in 1:size(B,2) loop
      for k in 1:size(A,2) loop
        C[i,j] = C[i, j] + A[i, k]*B[k, j];
      end for;
    end for;
  end for;

but while simulation it gives Singularity Error

QUOTE
The problem is structurally singular for the element type Real.
The number of scalar Real unknown elements are 71.
The number of scalar Real equation elements are 140.
The problem for Real elements is overdetermined.

The problem is that :
The nested for loops generate the whole equations at the begining and then try to solve, and this means that we have always more equations than the variables

Is that true? and if so, how to solve such problem in order to use looping as we know in all programming languages?
Roland
QUOTE(Rene Nölle @ Apr 14 2008, 08:02 AM) *

The problem is that :
The nested for loops generate the whole equations at the begining and then try to solve, and this means that we have always more equations than the variables

Is that true? and if so, how to solve such problem in order to use looping as we know in all programming languages?

No, this is not true.
In fact, the error message you get is incorrect - there are not 69 equations too many - there are 36 equations missing.
The problem with Mike's book is, that the "examples" are not examples, they are incomplete code. In your example, there are no equations given to determine the values for A (15 values) and B (21 values).

The example is running if you change the first two lines to:
CODE
Real A[5,3] = fill(1, 5, 3);
Real B[3,7] = fill(1, 3, 7);
(Of course this example is pretty boring, since now the matrices are filled with ones...)

To clarify the nested loop question: The algorithm section is calculated from top to bottom at every solver step. If you would duplicate the whole section you would still have the same number of equations - it would just do the calculation twice.
In the algorithm section you can use all loops as you know them from any other programming language.

Regards,
Roland
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.