viadam.runge.kutta {viadam} | R Documentation |
One step of the fourth-order Runge-Kutta integration algorithm for a first order system of differential equations.
viadam.runge.kutta(x,dx,y,f)
x |
the starting point of the integration. |
dx |
the step size in x . |
y |
the initial value of the solution at x
(a vector of the same length as the number of equations). |
f |
a function expressing the derivative of y with respect
to x as a function of x and y ,
that is the right hand side of the differential equation
dy/dx = f(x,y) .
f must be defined as a function of two arguments
(a single number x and a vector y of the same
length as the number of equations) returning a vector (of the
same length as the number of equations). |
The function returns the solution y
at x+dx
.
viadam.runge.kutta
is typically called repeatedly increasing the value of x
by dx
in every iteration as shown in the example. The argument
f
must be a function object, which can either be defined beforehand
and referred to by its name, or be defined on the fly as in
viadam.runge.kutta(x,dx,y,(function(x,y){y}))
.
This function is written for use in the function mound.jump
and is not intended for other use. It is included in the viadam
package to make the package independent of any other utility package for R. There are many other packages that contain good Runge-Kutta integration routines that are more suitable for general use for solving differential equations. This routine is, however, good enough for the problem that needs to be solved in mound.jump
.
Tomas Johannesson
## Not run: yp <- function(x,y) {c(y[2],y[1])} n <- 10; dx <- 1/n; x <- 0; y <- c(1,1) for (i in 1:n) { y <- viadam.runge.kutta(x,dx,y,yp); x <- x+dx } y[1] y[1]-exp(1) ## End(Not run)