sp1.poly: Shape-preserving polynomial approximation

Description Usage Arguments Value References See Also Examples

View source: R/sp1.R

Description

Approximates the function fn using shape-preserving polynomials evaluated at the points in grid.

Usage

1
2
3
4
sp1.poly(fn, range, iOrder, iPts, fn.opts = NULL, fn.vals = NULL,
  grid = NULL, n.shape = 0, sign.deriv = NULL, x0 = NULL,
  solver = "NLOPT_LD_SLSQP", tol = 1e-06, details = FALSE,
  quiet = FALSE)

Arguments

fn

a function f(x) or f(x,β) for β a list of function parameters. If the latter, must be coded with second argument a list names opts, i.e. fn <- function( x, opts )

range

the range of the approximation.

iOrder

the order of the polynomial approximation.

iPts

the number of points at which the approximation is computed. Must be at least as large as iOrder.

fn.opts

(optional) options passed to fn

fn.vals

the values of fn on grid. Useful if fn is very slow to evaluate.

grid

(optional) the grid on which the function is to be approximated.

n.shape

a vector of the number of shape-preserving points for each order of differentiation. For example, to specify the slope at 5 Chebychev points and the concavity at 10, use n.shape=c( 5, 10 )

sign.deriv

a vector of signs +1, 0, -1 defining the sign of each derivative. For example, for a concave approximation with positive slope sign.deriv=c(1,-1).

x0

initial guess of the polynomial approximation. Default is the standard Chebychev approximation.

solver

the nlopt solver to use in computing the best fit. Default is NLOPT_LD_SLSQP.

tol

tolerance for solver convergence. Default is 1e-06

details

If TRUE, returns extra details about the approximation.

quiet

Supresses output about success of least-error fitting. Failure will always be reported

Value

A function which approximates fn. If details=TRUE, return is a list with entries fn, poly, fn.deriv, poly.deriv, residuals, which are, respectively, the approximating function, the polynomial desciption over [-1,1], the derivative of the approximation, the polynomial desciption of the derivative, and the approximation errors.

References

nloptr documentation

See Also

d1.poly

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
base <- d1.poly( log, c(0,4), 6, 10, details=TRUE )
sp.compare <- sp1.poly(  log, c(0,4), 6, 10, details=TRUE )
sp.flat.x0 <- sp1.poly(  log, c(0,4), 6, 10, x0=c(1,1,1,1,1,1,1), details=TRUE )
print( base$poly - sp.compare$poly )
print( base$poly - sp.flat.x0$poly )
   # Comparison without using shape-preserving methods
sp.concave <- sp1.poly( log, c(0,4), 6, 10, n.shape=c(5,10), sign.deriv=c(1,-1) )
pp <-  seq( 0, 4, length.out=100 )
plot( pp, sapply(pp, base$fn), lwd=2, col=2, type='l' )
lines( pp, sapply(pp, log), lwd=2, col=1 )
lines( pp, sapply(pp, sp.concave), lwd=2, col=4 )
legend( 'bottomright', c( 'log', 'Order 6 polynomial approx',
     'Order 6 shape-preserving polynomial approx' ), lwd=2, 
     col=c(1,2,4), bty='n' )
   # Compare the Chebychev and shape-preserving approximations

squipbar/cheby documentation built on May 30, 2019, 8 a.m.