Description Usage Arguments Value References See Also Examples
Approximates the function fn
using shape-preserving polynomials
evaluated at the points in grid.
1 2 3 4 |
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 |
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 |
fn.opts |
(optional) options passed to |
fn.vals |
the values of |
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 |
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 |
tol |
tolerance for solver convergence. Default is |
details |
If |
quiet |
Supresses output about success of least-error fitting. Failure will always be reported |
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.
nloptr
documentation
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
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.