Search code examples
rdifferential-equations

Solve variable coefficients second order linear ODE?


For the variable coefficients second order linear ODE

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

I have the numerical values (in terms of vectors) for $\beta_1(t)$ and $\beta_0(t)$, does anyone know some R package to do that? And some simple examples to illustrate would be great as well.

I googled to find 'bvpSolve' can solve constant coefficients value.


Solution

  • 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
    

    into a pair of coupled first-order ODEs:

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

    Then it's straightforward:

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

    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 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 ...