setStepSize {rODE} | R Documentation |
setStepSize
Description
setStepSize uses either of two step parameters: stepSize and dt stepSize works for most of the applications dt is used in Pendulum
Usage
setStepSize(object, ...)
Arguments
object |
a class object |
... |
additional parameters |
Examples
# ++++++++++++++++++++++++++++++++++++++++++++++++++application: SpringRK4App.R
# Simulation of a spring considering no friction
importFromExamples("SpringRK4.R")
# run application
SpringRK4App <- function(verbose = FALSE) {
theta <- 0
thetaDot <- -0.2
tmax <- 22; dt <- 0.1
spring <- SpringRK4()
spring@state[3] <- 0 # set time to zero, t = 0
spring <- setState(spring, theta, thetaDot)
# spring <- setStepSize(spring, dt = dt) # using stepSize in RK4
spring@odeSolver <- setStepSize(spring@odeSolver, dt) # set new step size
rowvec <- vector("list")
i <- 1
while (getState(spring)[3] <= tmax) {
rowvec[[i]] <- list(t = getState(spring)[3], # angle
y1 = getState(spring)[1], # derivative of the angle
y2 = getState(spring)[2]) # time
i <- i + 1
spring <- step(spring)
}
DT <- data.table::rbindlist(rowvec)
return(DT)
}
# show solution
solution <- SpringRK4App()
plot(solution)
# ++++++++++++++++++++++++++++++++++++++++++++++++ example: ComparisonRK45App.R
# Compares the solution by the RK45 ODE solver versus the analytical solution
# Example file: ComparisonRK45App.R
# ODE Solver: Runge-Kutta 45
# ODE class : RK45
# Base class: ODETest
importFromExamples("ODETest.R")
ComparisonRK45App <- function(verbose = FALSE) {
ode <- new("ODETest") # create an `ODETest` object
ode_solver <- RK45(ode) # select the ODE solver
ode_solver <- setStepSize(ode_solver, 1) # set the step
# Two ways of setting the tolerance
# ode_solver <- setTolerance(ode_solver, 1e-8) # set the tolerance
setTolerance(ode_solver) <- 1e-8
time <- 0
rowVector <- vector("list")
i <- 1
while (time < 50) {
rowVector[[i]] <- list(t = getState(ode)[2],
s1 = getState(ode)[1],
s2 = getState(ode)[2],
xs = getExactSolution(ode, time),
counts = getRateCounts(ode),
time = time
)
ode_solver <- step(ode_solver) # advance one step
stepSize <- getStepSize(ode_solver)
time <- time + stepSize
ode <- getODE(ode_solver) # get updated ODE object
i <- i + 1
}
return(data.table::rbindlist(rowVector)) # a data table with the results
}
# show solution
solution <- ComparisonRK45App() # run the example
plot(solution)
[Package rODE version 0.99.6 Index]