# Using TSpline for Fit

I have a set of 2D data points, x and y. I wish to fit function to these data. The function I want to fit is a spline, but I don’t want the knots to be the same as the data points. I.e. I’d like to fit a piece-wise polynomial with the overall function having some degree of smoothness.

From searching, I can easily find how to create a TSpline3/5 where the knots are exactly the (x,y) points. But that’s not what I want. I want some smaller number of knots, with the positions of the knots and the polynomial coefficients of the pieces to be the fit parameters. I’d be ok if the position of the knot was a fixed parameter, with only the coefficients left floating.

I hope I explained my problem properly. It’s difficult because almost all uses of splines in the examples are for interpolating or smoothing data. I want to use a spline to fit data points, using much fewer parameters than the number of points.

Thanks for any help,
Jean-François

Hi,

You can still fit the data points with a polynomial but I don;t think is this what you want.
Maybe the class TGraphSmooth has some of the functions you are looking for
(see root.cern.ch/root/html/TGraphSmo … th:Approx1)
Otherwise, probably what you are looking is what provided in R by the function predict,
stat.ethz.ch/R-manual/R-devel/li … pline.html

You can always use from ROOT r functions, using the ROOT-R interface provided in

root.cern.ch/drupal/content/how- … -interface

Best Regards

Lorenzo

You can always create a TSpline embedded in a TF1:

``````Double_t spline_4nodes(Double_t *x, Double_t *par)
{
/*Fit parameters:
par[0-3]=X of nodes (to be fixed in the fit!)
par[4-7]=Y of nodes
par[8-9]=first derivative at begin and end (to be fixed in the fit!)
*/
Double_t xx = x;

Double_t xn = { par, par, par, par };
Double_t yn = { par, par, par, par };

Double_t b1 = par;
Double_t e1 = par;

TSpline3 sp3("sp3", xn, yn, 4, "b1e1", b1, e1);

return sp3.Eval(xx);
}

TF1 *f_spline4 = new TF1("f_spline4", spline_4nodes, xmin, xmax, npars); // npars = 2*nodes+2
hist->Fit(f_spline4); ``````