4. 最善近似の特徴:ぱらぱらめくる『A Short Course on Approximation Theory』

chebPoly(6)
> chebPoly(6)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    0    0    0    0    0    0    1
[2,]    0    0    0    0    0    1    0
[3,]    0    0    0    0    2    0   -1
[4,]    0    0    0    4    0   -3    0
[5,]    0    0    8    0   -8    0    1
[6,]    0   16    0  -20    0    5    0
[7,]   32    0  -48    0   18    0   -1

# Approximate sin(x) on [-pi, pi] with a polynomial of degree 9 !
# This polynomial has to be beaten:
# P(x) = x - 1/6*x^3 + 1/120*x^5 - 1/5040*x^7 + 1/362880*x^9

my.function<-function(x){
	sin(x)+cos(x^2)
}

# Compare these polynomials
#p1 <- rev(c(0, 1, 0, -1/6, 0, 1/120, 0, -1/5040, 0, 1/362880))
#p2 <- chebCoeff(sin, -pi, pi, 9)
p2 <- chebCoeff(my.function, -pi, pi, 10)

# Estimate the maximal distance
x  <- seq(-pi, pi, length.out = 101)
#ys <- sin(x)
ys <- my.function(x)
#yp <- polyval(p1, x)
#yc <- chebApprox(x, sin, -pi, pi, 9)
yc <- chebApprox(x, my.function, -pi, pi, 10)
#max(abs(ys-yp))                       # 0.006925271
max(abs(ys-yc))                       # 1.151207e-05

## Not run: 
# Plot the corresponding curves
plot(x, ys, type = "l", col = "gray", lwd = 5)
#lines(x, yp, col = "navy")
lines(x, yc, col = "red")
grid()
## End(Not run)