set.seed(123)
n <- 16; nout <- 360
x <- sort(runif(n))
##' ## let 'xout' contain n + nout points including nodes
xout <- sort(c(x, seq(from = x[1] + 1e-8, to = x[n] - 1e-8, length.out = nout)))
res <- cardinalBasis_natSpline(x, xout = xout)
matplot(res$x, res$CB, type = "n", main = "Cardinal Basis")
abline(v = x, h = 1.0, col = "gray")
points(x = x, y = rep(0, n), pch = 21, col = "black", lwd = 2, bg = "white")
matlines(res$x, res$CB, type = "l")
## compare with 'splines'
require(splines)
y <- sin(2* pi * x)
sp <- interpSpline(x, y)
test <- rep(NA, 3)
der <- 0:2
names(test) <- nms <- paste("deriv. ", der, sep = "")
for (i in seq(along = der)) {
resDer <- cardinalBasis_natSpline(x, xout = xout, deriv = der[i])
test[nms[i]] = max(abs(predict(sp, xout, deriv = der[i])$y - resDer$CB \%*\% y))
}
test
## Lebesgue's function
plot(x = xout, y = apply(res$CB, 1, function(x) sum(abs(x))), type = "l",
lwd = 2, col = "orangered", main = "Lebesgue\'s function", log = "y",
xlab = "x", ylab = "L(x)")
points(x = x, y = rep(1, n), pch = 21, col = "black", lwd = 2, bg = "white")
Run the code above in your browser using DataLab