求解线性方程组

编程入门 行业动态 更新时间:2024-10-10 17:23:20
本文介绍了求解线性方程组-gekko的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是求解线性方程组的简单示例,也是对许多方程使用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获取预发行版本: github/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: github/BYU-PRISM/GEKKO/blob/master/gekko/gekko.py with APM executable in the bin folder. The fix is available with Gekko v1.0.0.

更多推荐

求解线性方程组

本文发布于:2023-11-30 06:12:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648984.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线性方程组

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!