[英]Solving differential equation with GEKKO with wrong results
When I run the code, I got y=0.0 as t->infinite;当我运行代码时,我得到 y=0.0 作为 t->infinite; however, the printed figure told me that the final value of y should be 2.0.
然而,打印出来的数字告诉我 y 的最终值应该是 2.0。 I don't know what's wrong with my code for the totally different answers.
对于完全不同的答案,我不知道我的代码有什么问题。
My code is as follow:我的代码如下:
import numpy as np
from gekko import GEKKO
import matplotlib.pyplot as plt
m = GEKKO()
m.time = np.linspace(0,40,401)
u_step = np.zeros(401)
u_step[100:] = 2.0
u = m.Param(value=u_step)
y = m.Var(1.0)
m.Equation(5 * y.dt()==-y+u)
m.options.IMODE = 4
m.solve(disp=False)
plt.plot(m.time,y,'r-',label='Output (y(t))')
plt.plot(m.time,u,'b-',label='Input (u(t))')
m.options.IMODE = 3
m.solve(disp=True)
print('Final Value: ' + str(y.value))
plt.show()
Gekko takes the first value of u
, not the last value of u
when performing the steady state calculation.壁虎采用的第一个值
u
,而不是最后一个值u
执行稳态计算时。 A small adjustment with u.value = 2
selects the last value to give the correct result. u.value = 2
小调整选择最后一个值以给出正确的结果。
import numpy as np
from gekko import GEKKO
import matplotlib.pyplot as plt
m = GEKKO()
m.time = np.linspace(0,40,401)
u_step = np.zeros(401)
u_step[100:] = 2.0
u = m.Param(value=u_step)
y = m.Var(1.0)
m.Equation(5 * y.dt()==-y+u)
m.options.IMODE = 4
m.solve(disp=False)
plt.plot(m.time,y,'r-',label='Output (y(t))')
plt.plot(m.time,u,'b-',label='Input (u(t))')
m.options.IMODE = 3
u.value = 2
m.solve(disp=True)
print('Final Value: ' + str(y.value))
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.