x<-c(0:100)
degree<-100
fc<-factorial(c(1:degree))
k<- -1; #-1
xpoly<-poly(x*k,degree=degree,raw=T)
y<-exp(k*x)
y_poly<-rep(0,length(x))
for(i in c(1:length(x)))
{
y_poly[i]<-sum(xpoly[i,]/fc)+1
}
plot(x,y, log="", main="fitting exponential with polynomial")
lines(x,y_poly, col=2, lty=2 )
legend(5,0.8, c("exponential exp(-x)","polynomial with degree of 200"), col=c(1,2), pch=c(1,-1), lty=c(-1,2))
#y<-y-1
xpoly.fac<-xpoly/fc
lm.ne<-lm(formula = y ~ xpoly,
#weights = 1/factorial(floor(x + 0.1))
weights=1/exp(x)
)
## start doing polynomial fitting of exponential to see how far we need to go with degrees
#fitpoly<-lm(y~poly(x,degree=4, raw=TRUE), weights= 1/factorial(floor(x + 0.1)))
y.noise<-y+rnorm(length(y),0,1)
lm.nens<-lm(formula = y.noise ~ xpoly,
#weights = 1/factorial(floor(x + 0.1))
weights=1/exp(x)
)
x_poly_1<-cbind(1,xpoly.fac)
exp.nnls<-nnls(x_poly_1,y.noise)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.