Skip to content
Prev 205732 / 398506 Next

Polynomial equation

Hi Chris,

Curve fitting has nothing to do with statistics (even though it is used in statistics).
To get an idea of this, try the following:

x <- ((-100):100)/100    
cofs<-rnorm(4)    #create coefficients
y <- cofs[1] + cofs[2]*x + cofs[3]*x^2 +cofs[4]*x^3
y1 <- y +rnorm(201,0,0.1)    #add noise
mm <- lm(y1~poly(x,3,raw=TRUE))    #fit a polynomial of degree 3
y2 <- predict(mm,as.data.frame(x))   #compute the polynomial for every point of x
plot(x,y,type="l");lines(x,y1,col="red");lines(x,y2,col="blue")
cofs
mm$coefficients

For the exponential fit, there exist two options:
you are trying to fit y = exp(a*x+b)
one possibility is to fit log(y) = a*x+b by mm <- lm(log(y)~x) 
and the other (more "correct") one is to use any of the least squares packages.

I believe that you better read a little bit about curve fitting before doing all this.

Regards,

Moshe.
--- On Fri, 8/1/10, chrisli1223 <chrisli at austwaterenv.com.au> wrote: