# How to plot a smooth curve function from given points

I have several points, how can I plot a smooth curve that pass through those points?

Is there any function that I can create or formula that I can use to get all points in the curve?

I have read about bezier curves, but I don't really understand how to plot a graph from it because I don't think I need the Bezier specific parameters like its control points.

Anyone has any idea?

Here is my data example, they are used for a measurement in given time interval (time,value), I want to plot all points in the given time interval, let's say [0..400]: value = [(10,30), (111,100), (171,128), (181,86), (201,42), (211,44), (281,39), (321,59), (341,20), (351,4), ...]

Thanks

• Do you want the curve goes exactly through the data points? Nov 27, 2014 at 14:06
• not really, as long as the curve is close to the data points, it is still okay for me with some deviations. Nov 27, 2014 at 14:07
• OK. So we are speaking about curve fit. Do you have a model ? If they are not too many, put the points in your post. I shall have a look. Nov 27, 2014 at 14:17
• I have put it in my post, it just one points sample of my data though, I am searching for a general solution if there is any Nov 27, 2014 at 14:30
• Try reading about lagrange interpolation on wikipedia. That should help Nov 27, 2014 at 15:30

some options in R would be

t=seq(0,10,0.01)
y=sin(t)+rnorm(length(t))
plot(t,y,cex=0.1)

# puts a loess smoother through the points
lines(loess.smooth(t,y),col=2)

library(mgcv)
g<-gam(y~s(t),data=data.frame(t=t,y=y))
lines(t,g\$fitted.values,col=3)


t=1:400
y=rep(NA,length(t))
y[10]<-30
y[111]<-100
y[171]<-128
y[181]<-86
y[201]<-42
y[211]<-44
y[281]<-39
y[321]<-59
y[341]<-20
y[351]<-4
library(mgcv)

g<-gam(y~s(t),data=data.frame(t=t,y=y))
lm<-predict.gam(g,data.frame(t=t,y=y))

plot(t,y,cex=0.5,pch=16,ylim=c(min(lm),max(lm)))
lines(t,lm,col='red')

• I never learn R language before, can this create some points from my input? Because I need the points to plot it with gnuplot after that Nov 27, 2014 at 14:55
• yes, you can put your input, get an output then use the function write.csv to output a csv file. But if you don't want to use R then your easiest solution is to code a cubic spline which is relatively easy and there is abundant pseudocode Nov 27, 2014 at 16:27