numericalSolution: Numerical solution and plotting

View source: R/numericalSolution.R

numericalSolutionR Documentation

Numerical solution and plotting

Description

Numerically solves a two-dimensional autonomous ODE system for a given initial condition, using ode from the package deSolve. It then plots the dependent variables against the independent variable.

Usage

numericalSolution(
  deriv,
  y0 = NULL,
  tlim,
  tstep = 0.01,
  parameters = NULL,
  type = "one",
  col = c("red", "blue"),
  add.grid = TRUE,
  add.legend = TRUE,
  state.names = c("x", "y"),
  xlab = "t",
  ylab = state.names,
  method = "ode45",
  ...
)

Arguments

deriv

A function computing the derivative at a point for the ODE system to be analysed. Discussion of the required structure of these functions can be found in the package vignette, or in the help file for the function ode.

y0

The initial condition. Should be a numeric vector of length two reflecting the location of the two dependent variables initially.

tlim

Sets the limits of the independent variable for which the solution should be plotted. Should be a numeric vector of length two. If tlim[2] > tlim[1], then tstep should be negative to indicate a backwards trajectory.

tstep

The step length of the independent variable, used in numerical integration. Decreasing the absolute magnitude of tstep theoretically makes the numerical integration more accurate, but increases computation time. Defaults to 0.01.

parameters

Parameters of the ODE system, to be passed to deriv. Supplied as a numeric vector; the order of the parameters can be found from the deriv file. Defaults to NULL.

type

If set to "one" the trajectories are plotted on the same graph. If set to "two" they are plotted on separate graphs. Defaults to "one".

col

Sets the colours of the trajectories of the two dependent variables. Should be a character vector of length two. Will be reset accordingly if it is of the wrong length. Defaults to c("red", "blue").

add.grid

Logical. If TRUE, grids are added to the plots. Defaults to TRUE.

add.legend

Logical. If TRUE, a legend is added to the plots. Defaults to TRUE.

state.names

The state names for ode functions that do not use positional states.

xlab

Label for the x-axis of the resulting plot.

ylab

Label for the y-axis of the resulting plot.

method

Passed to ode. See there for further details. Defaults to "ode45".

...

Additional arguments to be passed to plot.

Value

Returns a list with the following components:

add.grid

As per input.

add.legend

As per input.

col

As per input, but with possible editing if a character vector of the wrong length was supplied.

deriv

As per input.

method

As per input.

parameters

As per input.

t

A numeric vector containing the values of the independent variable at each integration step.

tlim

As per input.

tstep

As per input.

x

A numeric vector containing the numerically computed values of the first dependent variable at each integration step.

y

A numeric vector containing the numerically computed values of the second dependent variable at each integration step.

y0

As per input.

Author(s)

Michael J Grayling

See Also

ode, plot

Examples

# A two-dimensional autonomous ODE system, vanDerPol.
vanDerPol_numericalSolution <- numericalSolution(vanDerPol,
                                                 y0         = c(4, 2),
                                                 tlim       = c(0, 100),
                                                 parameters = 3)


phaseR documentation built on Sept. 2, 2022, 5:07 p.m.