Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
1  
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? –  Vitaliy Kaurov Sep 11 '12 at 23:14
1  
The function just needs to reflect a trend. I added a link to the data. It is just {x,y} pairs. –  witkamp Sep 11 '12 at 23:29
2  
Are you sure they are {x,y} and not {y,x}? I mean: the integers are y values? –  belisarius Sep 12 '12 at 0:21
1  
You might like to have a look at BSplineCurve: reference.wolfram.com/mathematica/ref/… –  Oleksandr R. Sep 12 '12 at 18:32
1  
@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. –  Oleksandr R. Sep 14 '12 at 1:04
show 7 more comments

1 Answer

Taking your data in account from link you provided:

data={{......}};

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:

CForm[model]

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)
share|improve this answer
    
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 –  belisarius Sep 12 '12 at 0:11
3  
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). –  J. M. Sep 12 '12 at 1:35
    
@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... –  Ajasja Sep 12 '12 at 8:50
1  
@Ajasja, Eureqa? –  J. M. Sep 12 '12 at 10:43
1  
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. –  witkamp Sep 12 '12 at 17:14
show 12 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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