# runge2.s J F Monahan, June 2001 # second example of runge phenomenon # with unequally spaced points npts <- 100 # number of points to evaluate n <- 5 # number of interpolation points x <- 4*cos( ( 2*seq(n)-1 )*pi/(2*n) ) "interpolation points" x b <- 1/(1 + x*x) "values of f(x)" b # make matrix of polynomial interpolation A <- matrix(0,n,n) A[,1] <- 1 for(j in 2:n) A[,j] <- x*A[,j-1] # solve to get polynomial coefficients in c c <- solve(A,b) xi <- 4.1*( 2*seq(npts)-npts-1)/(npts-1) fxi <- 1/(1 + xi*xi) # Horner's rule for evaluating polynomial horner <- function(u) { # actually degree n-1 polynomial v <- 0 for( k in n:1) v <- u*v + c[k] v } y <- c( sapply(xi,horner) ) xy <- paste( 'xi', format(xi),'fxi',format(fxi),'y',format(y) ) cat(xy,sep="\n") rm(list=ls())