Simple ODE

## =============================================================================
## R-Code to solve example 3.1.2 from the book
## K. Soetaert, J. Cash and F. Mazzia, 2012.  
## Solving differential equations in R. UseR, Springer, 248 pp. 
## http://www.springer.com/statistics/computational+statistics/book/978-3-642-28069-6.
## implemented by Karline Soetaert
## =============================================================================

## A simple ordinary differential equation model

a    <- -8/3 ; b <- -10; c <- 28
yini <- c(X = 1, Y = 1, Z = 1)

Lorenz <- function (t, y, parms) {
 with(as.list(y), {
    dX <- a * X + Y * Z
    dY <- b * (Y - Z)
    dZ <- -X * Y + c * Y - Z
    list(c(dX, dY, dZ))
 })
}

times <- seq(from = 0, to = 100, by = 0.01) 
out   <- ode(y = yini, times = times, func = Lorenz, parms = NULL)

plot(out, lwd = 2)

library(plot3D)
pm <- par (mar = c(1, 1, 1, 1))
lines3D(out[,"X"], out[,"Y"], out[,"Z"], colkey = FALSE, 
   colvar = out[,"Z"], bty = "f", main = "butterfly", 
   phi = 0)
par (mar = pm)

Go back