# NOT RUN {
## evenly spaced grid-points
su <- seq(0,1,length.out=10)
## irregularly spaced grid-points
s <- su^3
## create approximation on the irregularly spaced grid
ch <- Vectorize(chebappxg(exp(s),list(s)))
## test it:
ch(su) - exp(su)
# try one with three variables
f <- function(x) exp(-sum(x^2))
grid <- list(s,su,su^2)
ch2 <- chebappxg(evalongrid(f,grid=grid),grid)
# test it at 10 random points
replicate(10,{a<-runif(3); ch2(a)-f(a)})
# Try Runge's function on a uniformly spaced grid.
# Ordinary polynomial fitting of high degree of Runge's function on a uniform grid
# creates large oscillations near the end of the interval. Not so with chebappxgf
f <- function(x) 1/(1+25*x^2)
chg <- Vectorize(chebappxgf(f,seq(-1,1,length.out=15)))
# also compare with Chebyshev interpolation
ch <- Vectorize(chebappxf(f,15))
\dontrun{
# plot it
s <- seq(-1,1,length.out=200)
plot(s, f(s), type='l', col='black')
lines(s, chg(s), col='blue')
lines(s, ch(s), col='red')
legend('topright',
legend=c('Runge function','chebappxg on uniform grid','Chebyshev'),
col=c('black','blue','red'), lty=1)
}
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab