x <- c(1,2, 10, 20, 50, 100, 1000, 1e4)
nu <- c(outer(c(1,2,5), 10^(0:9)))
ks <- 0:4; ks <- setNames(ks, paste0("k=",ks))
if(requireNamespace("sfsmisc")) { # Mostly for plotting
formatN <- if(packageVersion("sfsmisc") >= "1.1.26")
sfsmisc::frmtNum else sfsmisc::formatN
eaxis <- sfsmisc::eaxis
mult.fig <- sfsmisc::mult.fig
} else { ## no {sfsmisc}
formatN <- function(x) formatC(x, width=1)
eaxis <- axis
mult.fig <- function(nr.plots, main=NULL, ...) { # cheap but working substitute
mfrow <- n2mfrow(nr.plots)
tit.wid <- if(is.null(main)) 0 else 1 + 1.5 * par("cex.main")
old.par <- par(mfrow = mfrow, oma = c(0, 0, tit.wid, 0),
mar = 0.1 + c(4, 4, 2, 1), mgp = c(1.5, 0.6, 0))
if (!is.null(main)) {
plot.new(); mtext(main, side = 3, outer = TRUE, cex=par("cex.main")); par(new = TRUE)
}
invisible(list(old.par = old.par))
}
}
nus <- setNames(nu, paste0("nu=",formatN(nu)))
mI <- sapply(simplify = "array", ks, function(k.)
sapply(nus, function(n.)
besselI.nuAsym(x, nu=n., k.max = k., log = TRUE)))
mK <- sapply(simplify = "array", ks, function(k.)
sapply(nus, function(n.)
besselK.nuAsym(x, nu=n., k.max = k., log = TRUE)))
str(mK) # 7 x 30 x 5 array
##------------ Plotting besselI : I_[nu](x = x0) vs nu -----------------------------------
str(op <- mult.fig(length(x))$old.par); par("mfrow")
for(ix in seq_along(x)) {
matplot(nu, mI[ix,,], type = "l", log = "x",
xaxt = "n", xlab = quote(nu), ylab = quote(I[nu](x)),
main = substitute(I[nu](x==X, nu, k.max == 0:4), list(X = (xi <- x[ix]))))
eaxis(1)
}
par(op)
##------------ Plotting besselK : K_[nu](x = x0) vs nu -----------------------------------
str(mult.fig(length(x), main = expression(log(K[nu](x)) ~ vs ~ nu ~~ "x in log-scale"))$old.par)
par("mfrow")
for(ix in seq_along(x)) {
y <- mK[ix,,]
matplot(nu, y, type = "l", log = "x", ylim = c(min(y), min(max(y), 3*abs(min(y)))),
xaxt = "n", xlab = quote(nu), ylab = quote(K[nu](x)),
main = substitute(K[nu](x==X, nu, k.max == 0:4), list(X = (xi <- x[ix]))))
abline(h = 0, col = "gray20", lty = 3)
eaxis(1)
}
par(op)
str(mult.fig(length(x), main = expression(log(K[nu](x)) ~ vs ~ nu ~~ "in log-log"))$old.par)
for(ix in seq_along(x)) {
matplot(nu, mK[ix,,], type = "l", log = "xy", # --> 14 warnings: each plot h
xaxt = "n", yaxt = "n", xlab = quote(nu), ylab = quote(K[nu](x)),
main = substitute(K[nu](x==X, nu, k.max == 0:4), list(X = (xi <- x[ix]))))
eaxis(1); eaxis(2)
}
par(op)
## Compare with besselK() -- which I can use expon.scaled !
dimnames(mI)[[1]] <- dimnames(mK)[[1]] <- paste0("x=", formatN(x))
str(mK)
rbind(t(mK[,"nu=200",]), log.besselK = log(besselK(x, nu=200, expon.scaled=TRUE)) - x)
##--> apart from k=0 the asymptotic approximation seems good already for k=1 here
rbind(t(mK[,"nu=500",]), log.besselK = log(besselK(x, nu=500, expon.scaled=TRUE)) - x)
##--> apart from k=0 the asymptotic approximation seems good already for k=1 here
##---> need other, smaller x[] range - to get non-Inf besselK()
Run the code above in your browser using DataLab