x <- seq(-1, 1, length.out = 101)
y <- runge(x)
plot(x, y, type = "l", lwd = 2, col = "navy", ylim = c(-0.2, 1.2))
grid()
n <- c(6, 11, 16)
for (i in seq(along=n)) {
xp <- seq(-1, 1, length.out = n[i])
yp <- runge(xp)
p <- polyfit(xp, yp, n[i]-1)
y <- polyval(p, x)
lines(x, y, lty=i) }
Run the code above in your browser using DataLab