I couldn't find an
R implementation of
Neville's algorithm online, here's my (rather inefficient) implementation. Modifications to make it more efficient are welcome.
# uncomment accordingly, msize is equal to n+1
msize <- 6
#msize <- 11
#msize <- 21
L=0 #reset our list, since R doesn't do it for us
# Let's generate the equally spaced points depending on the value of n
L[1]<--1
i<-2
while(i<=msize){
L[i]<-L[i-1]+(4/(msize-1))
i<-i+1
}
# Let's define our f(x)
f <- function(x) 1/(1+4*x^2)
neville <- function (L, msize) {
Q <- matrix(0, nrow = msize, ncol=msize, byrow=FALSE, dimnames =NULL)
Q[,1] <- f(L) #fill first column with f(list)
for (i in 1:(msize-1))
{
for(j in 1:i) Q[i+1,j+1] <- ((x-L[i-j+1])*(Q[i+1,j])-(x-L[i+1])*(Q[i,j]))/(L[i+1]-L[i-j+1])
}
Q[msize, msize]
}
y<-0 #initialize y
k<-1 #index for y array
for (x in seq(-1,3,by=0.001)){
y[k]<-neville(L, msize)
k<-k+1
}
x <- seq(-1,3,by=0.001)
plot (x, y)