## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----setup, echo = FALSE------------------------------------------------------
library(minimaxApprox) # nolint undesireable_function_linter

## ----fig.width = 6, fig.height = 4--------------------------------------------
fit <- minimaxApprox(sin, 0, 1, 5)
plot(fit)

## -----------------------------------------------------------------------------
fit <- minimaxApprox(exp, 0, 1, 5)
fit

## -----------------------------------------------------------------------------
coef(fit)

## -----------------------------------------------------------------------------
x <- seq(0, 1, length.out = 11)
minimaxEval(x, fit)

## ----error = TRUE-------------------------------------------------------------
try({
tryCatch(minimaxApprox(exp, 10, 20, 30, basis = "monomial"),
         error = function(e) message(conditionMessage(e)))
})

## -----------------------------------------------------------------------------
bfit <- minimaxApprox(exp, 10, 20, 30, basis = "barycentric")
bfit$ObsErr
bfit$Warning

## -----------------------------------------------------------------------------
bfit$convResid

## -----------------------------------------------------------------------------
f <- function(x) 1 / (x + 1.05)
poly8 <- minimaxApprox(f, -1, 1, 8)
poly8$ObsErr                                  # degree 8, still poor
rat21 <- minimaxApprox(f, -1, 1, c(2, 1))      # classical (Cody) rational
rat21$ObsErr

## -----------------------------------------------------------------------------
rat21b <- minimaxApprox(f, -1, 1, c(2, 1), basis = "barycentric")
rat21b$ObsErr
rat21b$Warning

## ----error = TRUE-------------------------------------------------------------
try({
g <- function(x) 1 / (x ^ 2 + 0.01)
tryCatch(minimaxApprox(g, -1, 1, c(3, 3), basis = "barycentric"),
         error = function(e) message(conditionMessage(e)))
})

## -----------------------------------------------------------------------------
relfit <- minimaxApprox(exp, 0.5, 2, 6, relErr = TRUE)
relfit$ObsErr   # this is now a maximum *relative* error, not absolute

## ----error = TRUE-------------------------------------------------------------
try({
tryCatch(minimaxApprox(sin, -1, 1, 5, relErr = TRUE),
         error = function(e) message(conditionMessage(e)))
})

## -----------------------------------------------------------------------------
chk <- minimaxApprox(exp, 5, 6, 10)
chk$Warning
chk$ObsErr                                            # what the object reports
grid <- seq(5, 6, length.out = 100001)
max(abs(minimaxErr(grid, chk)))                        # what a dense grid shows

