简体   繁体   中英

Solving differential equation with GEKKO with wrong results

When I run the code, I got y=0.0 as t->infinite; however, the printed figure told me that the final value of y should be 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. A small adjustment with u.value = 2 selects the last value to give the correct result.

阶跃响应

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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