# [R] fitting sine wave

Tonja Krueger tonja.krueger at web.de
Wed Jul 27 16:36:44 CEST 2011

```Dear R-helpers

I have 7 data points that I want to fit a continuous curve to, that should look similar to a sine wave
My data points would mark the local minima and maxima respectively.
This is what I’ve got so far. And I would keep doing so, but sadly nls() then says that it has reached the maximum number of Iterations…

m<-c(-0.2061826,0.5888406,0.2026079,1.0000000,0.2342754,0.6865078,-0.1265754)
x <- c(1,2,3,4,5,6,7)
p <- nls(m~k1*x+k2*cos(x)+k3*sin(x)+k4*cos(2*x)+k5*sin(2*x)+k6*cos(3*x),start = list(k1=0,k2=0,k3=0.1,k4=0.1,k5=0,k6=0))
par  <- c(pk1=summary(p)\$parameters[1,1],pk2=summary(p)\$parameters[2,1],pk3=summary(p)\$parameters[3,1],pk4=summary(p)\$parameters[4,1],pk5=summary(p)\$parameters[5,1],pk6=summary(p)\$parameters[6,1])
xx <- seq(1,7,length.out=500)
mm <- par*xx+par*cos(xx)+par*sin(xx)+par*cos(2*xx)+par*sin(2*xx)+par*cos(3*xx)
plot(x,m)
points(xx,mm,type="l")

I was also thinking of using fft(), but when I use the inverse function I only get my 7 original points back, but no smooth sine function.