且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

求解变系数二阶线性ODE?

更新时间:2021-12-15 15:21:58

要使用 deSolve ,必须制作二阶ODE

In order to use deSolve, you have to make your second-order ODE

x''(t) + \beta_1(t) x'(t) + \beta_0 x(t)=0

进入一对耦合的一阶ODE:

into a pair of coupled first-order ODEs:

x'(t) = y(t)
y'(t) = - \beta_1(t) y(t) - \beta_0 x(t)

然后很简单:

gfun <- function(t,z,params) {
      g <- with(as.list(c(z,params)),
       {
         beta0 <- sin(2*pi*t)
         beta1 <- cos(2*pi*t)
       c(x=y,
         y= -beta1*y - beta0*x))
     list(g,NULL)
}
library("deSolve")
run1 <- ode(c(x=1,y=1),times=0:40,func=gfun,parms=numeric(0))

我任意选择了一些初始条件( x(0)= 1 x'(0)= 1 );您可能还想向模型添加参数(即,使 parms 成为 numeric(0)以外的东西)

I picked some initial conditions (x(0)=1, x'(0)=1) arbitrarily; you might also want to add parameters to the model (i.e. make parms something other than numeric(0))

PS如果您不愿意手工转换为耦合的一阶ODE,并且想要一个可以无缝处理二阶ODE的程序包,那么我不知道答案...

PS if you're not happy doing the conversion to coupled first-order ODEs by hand, and want a package that will seamlessly handle second-order ODEs, then I don't know the answer ...