I am trying to find a function that fits my almost linear data. A high order polynomial model has too much residual. So I was hoping to use Mathematica to fit splines to the curve.

This is what I would like to see in an example.

  • FindFit with a spline function
  • How to get model stats like what LinearModelFit provides.
  • How to dump the spline terms and control points so I can implement in "C"

I can then use the cubic spline on my embedded platform.

So can I get a symbolic representation of this function so I can implement it?

You can get the sample data here

  • 1
    $\begingroup$ can we have a sample of your data? is it only y-values or {x,y} pairs? Full data would be much better. And do you need the fit curve to pass exactly through all points or just reflect on general trend? $\endgroup$ Sep 11, 2012 at 23:14
  • 1
    $\begingroup$ The function just needs to reflect a trend. I added a link to the data. It is just {x,y} pairs. $\endgroup$
    – witkamp
    Sep 11, 2012 at 23:29
  • 2
    $\begingroup$ Are you sure they are {x,y} and not {y,x}? I mean: the integers are y values? $\endgroup$ Sep 12, 2012 at 0:21
  • 1
    $\begingroup$ You might like to have a look at BSplineCurve: reference.wolfram.com/mathematica/ref/… $\endgroup$ Sep 12, 2012 at 18:32
  • 2
    $\begingroup$ @witkamp yes, it's strange that these examples are on the BSplineCurve documentation page rather than the one for BSplineBasis. To get the goodness-of-fit statistics you can probably use BSplineBasis with LinearModelFit rather than constructing the design matrix manually as shown in the example. I'd have posted an answer except I wasn't sure if that was what you were looking for and haven't used splines for anything before, so I have no real familiarity with them. Please feel free to self-answer if you like. $\endgroup$ Sep 14, 2012 at 1:04

1 Answer 1


Taking your data in account from link you provided:


Find the model:

model = Fit[data, x^# & /@ Range[0, 10], x]

20.2513 + 43.3389 x - 0.208411 x^2 + 0.193888 x^3 - 0.0341689 x^4 + 
 0.00281455 x^5 - 0.000131003 x^6 + 3.64629*10^-6 x^7 - 
 6.01724*10^-8 x^8 + 5.43205*10^-10 x^9 - 2.06702*10^-12 x^10

Verify it is more or less correct:

Show[ListPlot[data, PlotStyle -> Directive[PointSize[.02], Opacity[.02], Red]], 
 Plot[model, {x, -7, 55}, PlotStyle -> Thickness[.005]], 
 Frame -> True, Axes -> False, ImageSize -> 500]

enter image description here

The blue line inside is your model. Red line is your data points blended together (too many of them) with applied opacity. I've chosen so many polynomial terms to take in account well little bent at the beginning. You can play with number of polynomial terms.

Export your model to C:


20.251253486790134 + 43.33892854755122*x - 0.20841104603541305*Power(x,2) + 
   0.19388822209706186*Power(x,3) - 0.03416888859439315*Power(x,4) + 
   0.0028145533596680857*Power(x,5) - 0.0001310033312242676*Power(x,6) + 
   3.646291289683582e-6*Power(x,7) - 6.017238075935027e-8*Power(x,8) + 
   5.432049184033492e-10*Power(x,9) - 2.0670190082996488e-12*Power(x,10)
  • $\begingroup$ Just in case this works much better for the left "tail" f[x_]:=24.9*x + 2.206*x*Sin@x + x^2*Sin@x + Tan[0.2407*x] - 5.611*x^2 $\endgroup$ Sep 12, 2012 at 0:11
  • 3
    $\begingroup$ If you're expecting to be doing high-order polynomial fits, I would recommend using LinearModelFit[], taking one basis function at a time, and monitoring the value of "AdjustedRSquared" to guard against overfitting (or use the fancier methods, such as cross-validation). $\endgroup$ Sep 12, 2012 at 1:35
  • $\begingroup$ @belisarius Ahh, what's the name of the program you used to get this expression? The one that fits models of different complexity to the data... $\endgroup$
    – Ajasja
    Sep 12, 2012 at 8:50
  • 1
    $\begingroup$ @Ajasja, Eureqa? $\endgroup$ Sep 12, 2012 at 10:43
  • 2
    $\begingroup$ The residuals for the 12th order polynomial are too high. That is why I would like to fit a SPLINE to the curve. Also this needs to be implemented on an embedded platform with software FP. $\endgroup$
    – witkamp
    Sep 12, 2012 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.