# Fitting the sine function by a polynomial
  x <- seq(0, pi, length.out=25)
  y <- sin(x)
  p <- polyfit(x, y, 6)
  
## Not run: 
#   # Plot sin and fitted polynomial
#   plot(x, y, type="b")
#   yf <- polyval(p, x)
#   lines(x, yf, col="red")
#   grid()## End(Not run)
## Not run: 
#   n <- 3
#   N <- 100
#   x <- linspace(0, 2*pi, N); y = sin(x) + 0.1*rnorm(N)
#   xfix <- c(0, 2*pi); yfix = c(0, 0)
# 
#   xs <- linspace(0, 2*pi); ys <- sin(xs)
#   plot(xs, ys, type = 'l', col = "gray",
# 	   main = "Polynom Approximation of Degree 3")
#   grid()
#   points(x, y, pch='o', cex=0.5)
#   points(xfix, yfix, col = "darkred")
# 
#   p0 <- polyfit(x, y, n)
#   lines(xs, polyval(p0, xs), col = "blue")
# 
#   p1 <- polyfix(x, y, n, xfix, yfix)
#   lines(xs, polyval(p1, xs), col = "red")
# 
#   legend(4, 1, c("sin", "polyfit", "polyfix"),
#          col=c("gray", "blue", "red"), lty=c(1,1,1))## End(Not run)
Run the code above in your browser using DataLab