下文代码是用来求解微分代数方程的,正确的解应该是一个三角函数震荡曲线,但是目前的求解结果不正确,我也看不出问题,麻烦各位大佬帮忙看一眼好不?而且初始条件的指定貌似也有问题
from pyomo.environ import *
from pyomo.dae import *
import pyomo.environ as pyo
import pyomo.dae as dae
m = ConcreteModel()
m.t =ContinuousSet(bounds=(0, 100)) # 时间范围
m.x1 = Var(m.t, domain=Reals,initialize=1) # y的定义
m.x2 = Var(m.t, domain=Reals,initialize=0) # v的定义
m.x3 = Var(m.t, domain=Reals,initialize=-1) # v的定义
m.dx1dt = DerivativeVar(m.x1, wrt=m.t) # y对t的导数
m.dx2dt = DerivativeVar(m.x2, wrt=m.t) # v对t的导数
# 微分方程
def _ode1(m, t):
return m.dx1dt[t] == m.x2[t]
m.ode1 = Constraint(m.t, rule=_ode1)
def _ode2(m, t):
return m.dx2dt[t] == m.x3[t]
m.ode2 = Constraint(m.t, rule=_ode2)
def _ode3(m, t):
return m.x3[t]+m.x1[t]==0
m.ode3 = Constraint(m.t, rule=_ode3)
m.obj = Objective(expr=0)
# Declare the discretizer
discretizer = pyo.TransformationFactory('dae.collocation')
discretizer.apply_to(m,nfe=10,scheme='LAGRANGE-RADAU',ncp=18)
solver = pyo.SolverFactory('ipopt')
solver.solve(m,tee=True)