Learn R Programming

pwlapprox2d (version 0.1.0)

adaptive_pwl_fit: Adaptive Piecewise Linear Approximation of a Continuous Function

Description

Approximates a continuous function \(f\) on a domain \([a, b]\) by adaptively discretizing the domain and building a piecewise linear (PWL) envelope until the maximum error between the PWL and \(f\) is within a given tolerance.

Usage

adaptive_pwl_fit(
  f,
  domain,
  tol = 0.001,
  max_iter = 50,
  initial_points = 5,
  smallconst = 1e-04
)

Value

A list with components:

PWL

A matrix of piecewise linear segments: slope, intercept, lower bound, upper bound.

data

The final discretization points (x, y) used in fitting.

max_error

Maximum absolute error between \( f \) and the PWL approximation.

Arguments

f

A continuous function \( f(x) \) to approximate.

domain

Numeric vector of length 2 specifying the interval \([a, b]\).

tol

Numeric tolerance for maximum allowed approximation error (default 1e-3).

max_iter

Maximum number of refinement iterations (default 20).

initial_points

Initial number of discretization points (default 5).

smallconst

Numeric small constant used in building PWL envelope (default 1e-4).

Examples

Run this code
f <- function(x) log(x)
domain <- c(1, 10)
res <- adaptive_pwl_fit(f, domain, tol = 1e-4, initial_points = 10, smallconst = 0.01)

cat("x,y\n")
for(i in 1:nrow(res$data)) {
  cat(paste(res$data[i, 1], res$data[i, 2], sep = ","), "\n")
}

Run the code above in your browser using DataLab