Search code examples
algorithmmatrixvectormodelicadymola

Modelica: assign array return value to scalars


The following model tries to assign a vector return value of a function call to a vector of scalars. It checks and works if it is inside an equation section, but it fails inside an algorithm section. Is that a bug in the Modelica tool I am using, or am I doing something wrong? And, how can I write it without introducing the intermediate variable x[2]?

model returnVector
  Real x1;
  Real x2;
  Real x[2];
  Real A[2,2] = [1,2;3,4];
  Real b[2] = {8,7};
algorithm 
  x = Modelica.Math.Matrices.solve(A,b);
  {x1, x2} = Modelica.Math.Matrices.solve(A,b);
end returnVector;

Solution

  • You are doing something wrong :-)

    In equations the left-hand-side is any expression; so you could even write Modelica.Math.Matrices.solve(A,b)={x1,x2};.

    In algorithms the left-hand-side must be a component-reference (section 11.2 in Modelica 3.4; https://modelica.org/documents/ModelicaSpec34.pdf ) and the right-hand-side is evaluated and then assigned to the left-hand-side variable.