Differential equations in R

Lorenz attractor

\[ \begin{align} \frac{\text{d}x}{\text{d}t} & = \sigma (y-x) \\ \frac{\text{d}y}{\text{d}t} & = x(\rho-z)-y \\ \frac{\text{d}y}{\text{d}t} & = xy - \beta z \end{align} \]

library(deSolve)
library(plotly)

parameters <- c(rho = 28, sigma = 10, beta = 8/3)
state <- c(x = 1, y = 1, z = 1)
time <- seq(0, 100, by = 0.01)

lorenz <- function(time, state, parameters) {
  with(as.list(c(state, parameters)), {
    dx <- sigma * (y - x)
    dy <- x * (rho - z) - y
    dz <- x * y - beta * z
    list(c(dx, dy, dz))
  })
}


out <- as.data.frame(ode(y = state, times = time, func = lorenz, parms = parameters))

plot_ly(out, x = ~x, y = ~y, z = ~z, type = 'scatter3d', mode = 'lines', color = ~time, colors = 'Blues')