Skip to contents

stm_ivps*() set of functions solve IVPs for ordinary differential equations (ODEs) using various numerical methods. Supports solving single equations or systems of ODEs.

Usage

stm_ivps_abm2(
  odeqtn,
  exactsol = NULL,
  vars = c("t", "y"),
  start_values_or_method = c("rk4", "feuler", "meuler", "heun3"),
  time_span = NULL,
  y0 = NULL,
  step_size = NULL,
  nsteps = 10,
  maxit = 100,
  show_iters = NULL,
  view_table = FALSE,
  show_plot = TRUE,
  digits = 8
)

Arguments

odeqtn

A function defining the ODE system. It should take two arguments: the current time and state vector as y = f(t, y).

exactsol

Optional. A function representing the exact solution for comparison. Default is NULL.

vars

Independent and dependent variables.

start_values_or_method

A character string specifying a method for generating start values or a vector of initial state values.

time_span

A numeric vector specifying the time range for the solution. Should be of length 2.

y0

Initial state values for the ODE system.

step_size

The step size for the numerical method. If NULL, it is computed based on the time span and number of steps.

nsteps

The number of time steps to take. Default is 10.

maxit

Maximum number of iterations for iterative methods. Default is 10.

show_iters

Logical. If TRUE, iteration details are displayed. Default is NULL.

view_table

Logical. If TRUE, the results are viewed in the R spreadsheet. Default is FALSE.

show_plot

Logical. If TRUE, the results are plotted. Default is TRUE.

digits

The number of significant digits to display in the results. Default is 8.

Value

A data frame containing the time points and corresponding solution values. If an exact solution is provided, the error at each time point is also included.

  • Time (t): Time points.

  • Approx. (yi): State values at each time point.

  • Exact (y): Exact solution if exactsol is provided.

  • Error: |y - yi|: The error at each time point if exactsol is provided.

Details

These functions solve initial value problems for ODEs using a specified numerical method. They can handle both single ODEs and systems of ODEs by providing state variables as a vector. If an exact solution is provided, the error at each time point will be calculated for comparison.

Examples

f <- "y - t^2 + 1"
ft <- "(t + 1)^2 - 0.5 * exp(t)"
result <- stm_ivps_abm2(
  odeqtn = f,
  exactsol = ft,
  start_values_or_method = "rk4",
  time_span = c(0, 2),
  y0 = 0.5,
  nsteps = 10,
  show_plot = FALSE
)
stm_gtable(result)
Time (t) Approx. (yi) Exact (y) Error: |y - yi|
0.0 0.5000000 0.5000000 0.00000000
0.2 0.8292933 0.8292986 0.00000529
0.4 1.2138308 1.2140876 0.00025685
0.6 1.6483189 1.6489406 0.00062165
0.8 2.1260940 2.1272295 0.00113553
1.0 2.6390124 2.6408591 0.00184665
1.2 3.1771244 3.1799415 0.00281719
1.4 3.7282728 3.7324000 0.00412719
1.6 4.2776043 4.2834838 0.00587945
1.8 4.8069706 4.8151763 0.00820566
2.0 5.2941978 5.3054720 0.01127415