在Open Modelica中的矩阵乘法

huangapple go评论75阅读模式
英文:

Matrix multiplication in Open Modelica

问题

I've made a simple model and doesn't seem to work. I get this errors: "方程太多,系统过度确定。该模型有6个方程和2个变量" and "sum_vs = sum_vs + v[1] * A[1,1] 的大小为1,但变量为0 ()"。

I've been staring at this code all morning, it is a simple example because I want to make it more complicated in the future (bigger dimensions). This is the code:

parameter Real A[2,2]=[1,2;1,2];
parameter Real v[2]={1,2};
parameter Real sum_vs=0; //counter
Real vs[2];
equation
for i in 1:2 loop
  for j in 1:2 loop
     sum_vs = sum_vs + v[j] * A[i,j];
  end for;
  vs[i]=sum_vs;
end for;
end MatrixMultiplication;

The ideal result is vs[1,2]=A[2,2]*v[2];

英文:

I've made a simple model and dosen't seem to work. I get this errors: "Too many equations, over-determined system. The model has 6 equation(s) and 2 variable(s)" and sum_vs = sum_vs + v[1] * A[1,1] has size 1 but 0 variables ().

I've been staring at this code all morning, it is a simple example because I want to make it more complicated in the future (bigger dimensions). This is the code:

model MatrixMultiplication
parameter Real A[2,2]=[1,2;1,2];
parameter Real v[2]={1,2};
parameter Real sum_vs=0; //counter
Real vs[2];
equation
for i in 1:2 loop
  for j in 1:2 loop
     sum_vs = sum_vs + v[j] * A[i,j];
  end for;
  vs[i]=sum_vs;
end for;
end MatrixMultiplication;

The ideal result is vs[1,2]=A[2,2]*v[2];

Thanks!

答案1

得分: 7

你所定义的方程基本上是有缺陷的,即sum_vs = sum_vs + v[j] * A[i,j];只是一个赋值语句,而不是一个有效的方程。你所写的是一个赋值操作,而不是一个方程,详见 https://mbe.modelica.university/behavior/equations/equations/ 了解Modelica中方程的概念。你需要确保理解平衡模型的概念,在这种模型中,方程和未知数的数量需要相同。

另外的错误包括:

  • 在声明中为sum_vs赋值将不允许你定义额外的方程。
  • parameter的值不能在equation部分中更改,因为它在模拟过程中是常数,而equation中的值可以改变。

如果你想在Modelica中进行这种“编程”(通常不推荐出于各种原因),你需要定义一个algorithm部分,而不是一个equation部分。你的示例重建为算法的工作方式如下(不确定是否会得到你期望的结果,因为我不理解“理想结果”方程):

  parameter Real A[2,2]=[1,2;1,2];
  parameter Real v[2]={1,2};
  Real sum_vs;
  Real vs[2];

algorithm 
  for i in 1:2 loop
    for j in 1:2 loop
       sum_vs :=sum_vs + v[j]*A[i, j];
    end for;
    vs[i]:=sum_vs;
  end for;
end MatrixMultiplication;

关于algorithm和方程部分之间的区别,可以阅读 https://stackoverflow.com/questions/20078276/difference-between-equation-and-algorithm-section

一个有效的方程可以通过以下代码定义:

  parameter Real A[2,2]=[1,2;1,2];
  parameter Real v[2]={1,2};
  Real vs[2];

equation 
  vs = A * v;
end MatrixMultiplication;

再次强调,我不确定这是否是你想要计算的内容,但至少这是一个有效的方程。

英文:

The equation you have defined is fundamentally flawed, i.e. sum_vs = sum_vs + v[j] * A[i,j]; is simply not a valid equation. What you are writing is an assignment not an equation, see e.g. https://mbe.modelica.university/behavior/equations/equations/ to understand what an equation is in Modelica. You need to make sure to understand the concept of balanced models in which the number of equations and unknowns need to be the same.

Additionally errors are:

  • Assigning a value to sum_vs in its declaration will not allow you to define an additional equation
  • The value of a parameter cannot be changed in the equation section, as it is constant for the time of the simulation and values in the equation can change.

If you want to do that kind of "programming" in Modelica (which is generally not recommended for various reasons), you need to define an algorithm- not an equation-section. Your example re-built to work as an algorithm would look like (not sure if it gives the result you expect, because I don't understand the "ideal result" equation):

model MatrixMultiplication
  parameter Real A[2,2]=[1,2;1,2];
  parameter Real v[2]={1,2};
  Real sum_vs;
  Real vs[2];

algorithm 
  for i in 1:2 loop
    for j in 1:2 loop
       sum_vs :=sum_vs + v[j]*A[i, j];
    end for;
    vs[i]:=sum_vs;
  end for;
end MatrixMultiplication;

Regarding the difference between algorithm and equation sections, e.g. read https://stackoverflow.com/questions/20078276/difference-between-equation-and-algorithm-section.

A valid equation could be defined by the following code:

model MatrixMultiplication
  parameter Real A[2,2]=[1,2;1,2];
  parameter Real v[2]={1,2};
  Real vs[2];

equation 
  vs = A * v;
end MatrixMultiplication;

Again, I'm not sure this is what you want to compute. But at least it is a valid equation.

huangapple
  • 本文由 发表于 2023年6月27日 18:53:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定