问题描述
这是求解线性方程组的简单示例,也是对许多方程使用for循环的示例.
Here is a simple example of solving the system of linear equations and the example of using for loop for many equations.
import numpy as np
from gekko import GEKKO
m = GEKKO(remote=False)
# Random 3x3
A = np.random.rand(3,3)
# Random 3x1
b = np.random.rand(3)
# Gekko array 3x1
x = m.Array(m.Var,(3))
# solve Ax = b
eqn = np.dot(A,x)
for i in range(3):
m.Equation(eqn[i]==b[i])
m.solve(disp=False)
X = [x[i].value for i in range(3)]
print(X)
print(b)
print(np.dot(A,X))
具有正确的输出.结果为X(np.dot(A,X)== b)-正确!
with the correct output. With the result X (np.dot(A,X)==b) - correct!
[[-0.45756768428], [1.0562541773], [0.10058435163]]
[0.64342498 0.34894335 0.5375324 ]
[[0.64342498]
[0.34894335]
[0.5375324 ]]
在最近的Gekko 0.2rc6中,还引入了用于线性编程的axb()函数.使用此功能可能会解决相同的问题,但是我不确定如何获得正确的结果.
In the recent Gekko 0.2rc6 there is also introduced axb() function for linear programing. This might be the same problem solved with this function, but I am not sure how to get the correct result.
m = GEKKO(remote=False)
# Random 3x3
A = np.random.rand(3,3)
# Random 3x1
b = np.random.rand(3)
# Gekko array 3x1
x = m.Array(m.Var,(3))
# solve Ax = b
m.axb(A,b,x=x)
m.solve(disp=False)
X = [x[i].value for i in range(3)]
print(X)
print(b)
print(np.dot(A,X))
但是似乎我错过了一些事情,因为输出不是解决方案???结果X(np.dot(A,X)== b)-不正确!
but it seems I missed something because the output is not the solution??? With the result X (np.dot(A,X)==b) - is not correct!
[[0.2560342704], [0.7543346092], [-0.084190799732]]
[0.27262652 0.61028723 0.74616952]
[[0.4201021 ]
[0.5206979 ]
[0.39195592]]
推荐答案
m.axb()
当前在Gekko v0.2.8中存在一个错误.它已在下一版Gekko中修复,或者您可以从GitHub获取预发行版本: https://github.com/BYU-PRISM/GEKKO/blob/master/gekko/gekko.py ,bin文件夹中有APM可执行文件.该修复程序可用于Gekko v1.0.0.
The m.axb()
currently has a bug in Gekko v0.2.8. It is fixed in the next version of Gekko or you can get the pre-release version from GitHub: https://github.com/BYU-PRISM/GEKKO/blob/master/gekko/gekko.py with APM executable in the bin folder. The fix is available with Gekko v1.0.0.
这篇关于求解线性方程组-gekko的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!