3
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ Do you want the curve goes exactly through the data points? $\endgroup$ Nov 27, 2014 at 14:06
  • $\begingroup$ not really, as long as the curve is close to the data points, it is still okay for me with some deviations. $\endgroup$ Nov 27, 2014 at 14:07
  • $\begingroup$ 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. $\endgroup$ Nov 27, 2014 at 14:17
  • $\begingroup$ 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 $\endgroup$ Nov 27, 2014 at 14:30
  • $\begingroup$ Try reading about lagrange interpolation on wikipedia. That should help $\endgroup$
    – syusim
    Nov 27, 2014 at 15:30

1 Answer 1

1
$\begingroup$

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)
# puts a spline through the points - read Simon Woods work to learn more about this
g<-gam(y~s(t),data=data.frame(t=t,y=y))
lines(t,g$fitted.values,col=3)

and for your recently added data

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')
$\endgroup$
2
  • $\begingroup$ 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 $\endgroup$ Nov 27, 2014 at 14:55
  • $\begingroup$ 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 $\endgroup$
    – Sam Bhatt
    Nov 27, 2014 at 16:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .