简体   繁体   English

如何将 GEKKO 溶液转换为浮子

[英]How to convert GEKKO solutions into a float

I am using GEKKO to solve a system of non-linear differential equations.我正在使用 GEKKO 来求解非线性微分方程组。 It is able to solve the equations and give me solutions, however the the solutions seem to be saved in some GEKKO object kind of format called "class 'gekko.gk_operators.GK_Value'".它能够解决方程并给我解决方案,但是这些解决方案似乎以某种名为“类'gekko.gk_operators.GK_Value'”的GEKKO对象格式保存。

Is there any way for me to convert this into a float?我有什么办法可以将其转换为浮点数吗? I need to manipulate these values to find to find their average.我需要操纵这些值来找到它们的平均值。

Here is my code这是我的代码

from gekko import GEKKO
DELTA=1
OMEGA=0
GAMMA=1
J=3
m = GEKKO()
SZ1, SZ2, SZ3, SX1, SX2, SX3, SY1, SY2, SY3 = [m.Var(value=0) for i in range(9)]
m.Equations([4*J*(SX2*SY1-SX1*SY2)-GAMMA*(1+SZ1)+2*OMEGA*SY1==0,
         2*J*(-2*SX2*SY1+2*SX1*SY2+2*SX3*SY2-2*SX2*SY3)-GAMMA*(1+SZ2)+2*OMEGA*SY2==0,
         2*J*(-2*SX3*SY2+2*SX2*SY3)-GAMMA*(1+SZ3)+2*OMEGA*SY3==0,
         J*SY2*SZ1-0.5*GAMMA*SX1-DELTA*SY1==0,
         2*J*SY1*SZ2-2*J*SY3*SZ2-0.5*GAMMA*SX2-DELTA*SY2==0,
         J*SY2*SZ3-0.5*GAMMA*SX3-DELTA*SY3==0,
         J*SX2*SZ1+0.5*GAMMA*SY1-DELTA*SX1+2*OMEGA*SZ1==0,
         2*J*SX1*SZ2-2*J*SX3*SZ2+0.5*GAMMA*SY2-DELTA*SX2+2*OMEGA*SZ2==0,
         J*SX2*SZ3+0.5*GAMMA*SY3-DELTA*SX3+2*OMEGA*SZ3==0])
m.solve(disp=True)
print([SZ1.value, SZ2.value, SZ3.value])    
ans=(SZ1.value+SZ2.value+SZ3.value)/3
print(ans)

Is there any was for me to convert SZ1, SZ2 and SZ3 into float values?是否有任何将 SZ1、SZ2 和 SZ3 转换为浮点值的方法?

Printing SZ1.value gives output:打印 SZ1.value 给出输出:

[-1.0]

which looks like a python list看起来像一个python列表

Hence your code:因此你的代码:

SZ1.value+SZ2.value+SZ3.value

is just concatenating three list.只是连接三个列表。 If you want to get the float value, you can use:如果要获取浮点值,可以使用:

SZ1.value[0]

Changing your code to:将您的代码更改为:

ans=(SZ1.value[0]+SZ2.value[0]+SZ3.value[0])/3

should give the desired result.应该给出想要的结果。

Reference for GK_Value being a python list: https://github.com/BYU-PRISM/GEKKO/blob/master/gekko/gk_operators.py#L118 GK_Value 作为 python 列表的参考: https : //github.com/BYU-PRISM/GEKKO/blob/master/gekko/gk_operators.py#L118

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM