英文:
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 theequation
section, as it is constant for the time of the simulation and values in theequation
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论