rk4 {demodelr}R Documentation

Runge Kutta method solution for a differential equation.

Description

rk4 solves a multi-dimensional differential equation with Runge-Kutta 4th order method. The parameters listed as required are needed See the vignette for detailed examples of usage.

Usage

rk4(
  system_eq,
  initial_condition,
  parameters = NULL,
  t_start = 0,
  deltaT = 1,
  n_steps = 1
)

Arguments

system_eq

(REQUIRED) The 1 or 2 dimensional system of equations, written in formula notation as a vector (i.e. c(dx ~ f(x,y), dy ~ g(x,y)))

initial_condition

(REQUIRED) Listing of initial conditions, as a vector

parameters

The values of the parameters we are using (optional)

t_start

The starting time point (defaults to t = 0)

deltaT

The timestep length (defaults to 1)

n_steps

The number of timesteps to compute solution (defaults to n_steps = 1)

Value

A tidy of data frame for the calculated solutions and the time

See Also

See Runge Kutta methods for more explanation of Runge-Kutta methods, as well as the code euler

Examples

# Define the rate equation:
quarantine_eq <- c(
 dSdt ~ -k * S * I,
 dIdt ~ k * S * I - beta * I
)
# Define the parameters (as a named vector):
quarantine_parameters <- c(k = .05, beta = .2)
# Define the initial condition (as a named vector):
quarantine_init <- c(S = 300, I = 1)
# Define deltaT and the number of time steps:
deltaT <- .1 # timestep length
n_steps <- 10 # must be a number greater than 1
# Compute the solution via Euler's method:
out_solution <- rk4(system_eq = quarantine_eq,
                   parameters = quarantine_parameters,
                   initial_condition = quarantine_init, deltaT = deltaT,
                   n_steps = n_steps
)

[Package demodelr version 1.0.1 Index]