在对拉格朗日插值函数 进行求积分运算的时候得到这个 > integrate(f,lower=0,upper=1)$value 错误于integrate(f, lower = 0, upper = 1) : evaluation of function gave a result of wrong type 错误 这个改怎么解决 我需要的是 对这个插值函数进行用字母表示后进行求积分。 以下是拉格朗日插值函数的代码,在单独运行这个函数的时候可以直接运行处所要的插值函数。但是一到积分就不可以了。求教 LagrangePolynomial <- function(x,y) { len = length(x) if(len != length(y)) stop("length not equal!")
if(len < 2) stop("dim size must more than 1")
#pretreat data abd alloc memery xx <- paste("(","t -",x,")") m <- c(rep(0,len))
#combin express for(i in 1:len) { td <- 1 tm <- "1" for(j in 1:len) { if(i != j) { td <- td*(x - x[j]) tm <- paste(tm,"*",xx[j]) } } tm <- paste(tm,"/",td) m<-tm #m <- parse(text=tm) }
#combin the exrpession m <- paste(m,"*",y) r <- paste(m,collapse="+")
#combin the function fbody <- paste("{ return(",r,")}") f <- function(t) {}
#fill the function's body body(f) <- parse(text=fbody)
return(f) }
|