polyfunc <- function(order,x) { ## the given data points dat1<-array(c(0:0), dim = c(11,1)) dat2<-array(c(1:1), dim = c(10,1)) datapt <- c(dat1,dat2) ## the parts for the van matrix data<-array(c(-10:10), dim = c(21,order)) power<-array(c(0:(order-1)), dim = c(order,21)) pnew<-t(power) ## the van matrix u <- (data^pnew) ## take svd - returns d u v get element by saying out$u out <- svd(u) ## assign to WVU convention w<- out$d V <- out$v U<-out$u ## then to get coefficients one has to flip the diagonal W components and then test ##whether they are ## larger then a threshold invw <- 1/w ## create a true fals mask to make those elements zero mask <- (invw>0.0001) invw <- invw*mask ## w need to be turned into matirx W<-diag(invw) ##then multiply the inverse etc of VWU res <- V %*% W %*% t(U) ## multiply result with the datapoint vector ##gives the coeficients fin <- res %*% datapt ##plot(x,datapt) ##lines(x,5.846163e-01 + (1.341669e-01*x) + ##(-3.652122e-03*x*x) + (-9.534706e-04*x*x*x) + (-4.522241e-05*x*x*x*x)) ##fin ##plot(x,polyseries(x,fin,order)) ##plot(x,datapt) ##plot(x,polyseries(x,fin,order)) ##lines(x,polyseries(x,fin,order)) } polyseries <- function(x,coeff,order) { return <- c(0:(length(x)-1)) for (i in 1:length(x)) { pow <- c(0:(order-1)) series <- array(c(x[i]), dim = c(1,order)) pow <- series^pow return[i] <- sum(t(coeff) * pow) } return }