ode.solve: Solve Ordinary Differential Equations (ODE)

Description Usage Arguments Value Examples

View source: R/diffeqr.R

Description

Solves an ODE with u'=f(u,p,t), for u(0)=u0 over the tspan

Usage

1
2
ode.solve(f, u0, tspan, p = NULL, alg = "nothing", reltol = 0.001,
  abstol = 1e-06, maxiters = 1e+06, saveat = NULL)

Arguments

f

the derivative function.

u0

the initial condition. Can be a number or (arbitrary dimension) array.

tspan

the timespan to solve over. Should be a list of two values: (initial time, end time).

p

the parameters. Defaults to no parameters. Can be a number or an array.

alg

the algorithm used to solve the differential equation. Defaults to an adaptive choice. Algorithm choices are done through a string which matches the DifferentialEquations.jl form.

reltol

the relative tolerance of the ODE solver. Defaults to 1e-3.

abstol

the absolute tolerance of the ODE solver. Defaults to 1e-6.

maxiters

the maximum number of iterations the adaptive solver is allowed to try before exiting. Defualt value is 1000000.

saveat

the time points to save values at. Should be an array of times. Defaults to automatic.

Value

sol. Has the sol$t for the time points and sol$u for the values.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 ## diffeq_setup() is time-consuming and requires Julia+DifferentialEquations.jl

diffeqr::diffeq_setup()

# Scalar ODEs

f <- function(u,p,t) {
return(1.01*u)
}
u0 = 1/2
tspan <- list(0.0,1.0)
sol = diffeqr::ode.solve(f,u0,tspan)
plot(sol$t,sol$u,"l")

saveat=1:10/10
sol2 = diffeqr::ode.solve(f,u0,tspan,saveat=saveat)
sol3 = diffeqr::ode.solve(f,u0,tspan,alg="Vern9()")
sol4 = diffeqr::ode.solve(f,u0,tspan,alg="Rosenbrock23()")

# Systems of ODEs

f <- function(u,p,t) {
  du1 = p[1]*(u[2]-u[1])
  du2 = u[1]*(p[2]-u[3]) - u[2]
  du3 = u[1]*u[2] - p[3]*u[3]
return(c(du1,du2,du3))
}

u0 = c(1.0,0.0,0.0)
tspan <- list(0.0,100.0)
p = c(10.0,28.0,8/3)
sol = diffeqr::ode.solve(f,u0,tspan,p=p)
udf = as.data.frame(sol$u)
matplot(sol$t,udf,"l",col=1:3)
#plotly::plot_ly(udf, x = ~V1, y = ~V2, z = ~V3, type = 'scatter3d', mode = 'lines')

f <- JuliaCall::julia_eval("
function f(du,u,p,t)
 du[1] = 10.0*(u[2]-u[1])
 du[2] = u[1]*(28.0-u[3]) - u[2]
 du[3] = u[1]*u[2] - (8/3)*u[3]
end")
sol = diffeqr::ode.solve('f',u0,tspan)

JuliaDiffEq/diffeqr documentation built on Nov. 24, 2019, 7:53 p.m.