Skip to main content
10-Marble
February 22, 2014
Solved

Fitting a function to Data

  • February 22, 2014
  • 1 reply
  • 12789 views

Attached is a Prime 3 document which has a data table and graph. I have been trying to fit a function to this data but all my attempts have failed using the canned curve fit functions. Does anyone have a specialised proceedure which will do the job?

Thanking you in advance.

Regards, Mark

Best answer by Werner_E

I played around a bit with your data and found a sum of 6 Gaussians which fit pretty well.

I didn't use genfit() but minerr() as the results will be the same and with minerr() the whole thing is better managable. I used Mathcad 15 for this because that version is much more user friendly and and quicker reacting and converted the sheet later in P3 format - just in case you are wondering why the datavectors are defined in a different way.

You may play around with the number of summands and the guess value and i think you will get good fits with less summands, too. But you will notice that the fit is VERY sensible wrt the guess values (just change the top left 1010 in Matrix M to 1020 or to 1000 and see yourself).

I'm not sure but I guess you'll like the splined f1 in my last post more.

But as you wrote that the data is synthetic hydrograph I would imagine that the other two regressions in my previous post are describuig the event more realistic as they don't follow every peak and valley.

22.02.png

1 reply

25-Diamond I
February 22, 2014
  • I don't see anything you have tried! What do you mean with "all my attempts have failed"? Didn't it work and you got an error message or was the fit not good enough in your opinion (then: what did you expect?)?
  • What do you know about your data? Any functional behaviour you know about? You could use genfit() for your purpose but you will have to provide the kind of formula (polyinomials, combination of gaussians, or whatever) which you think will fit best.
  • The values at 1200, 1500 sec may be outliers, you may consider dealing with them first before trying a fit (cutting off, or smoothing). Would this be an option?
  • Whats the purpose of the fit? What will you do with the fitting function? Would an interpolation (spline or even linear) also do the job?
10-Marble
February 22, 2014

Werner,

Sorry for the bad explanation. I did try various polynomial curve fit with various degrees 6 through 10 but with no luck i.e. the goodness of fit is bad as it doesn't fit the shape of the curve very well. Also tried another graphing program I have which has many other functions log fit, exponential fits etc all seem to fit very poorly. Also tried MS Excel linear fits etc. etc. No standard function seems to fit this data which is a synthetic hydrograph. If I get a good fitting function I can integrate the curve or route it through a reservoir using the solve function in Mathcad i.e. I would have to write a program using Eulers method etc which is prone to error. I seem to recall someone telling me that Mathematica had an advanced database of functions which it would try use one by one until it got the best match. Can genfit() do a similiar thing i.e. fins a function automatically? Forgive my ignorance is an interpolation the same as a continuous function in Mathcad i.e. can you do numerical integration on such a thing?

Mark

25-Diamond I
February 22, 2014

I also tried a polynomial fit and also a fit with two gaussians which both look very similar and good if the goal is a smooth curve. Ofcourse they are far away from those ouliers(if they are?) and I guess you would need more data points to decide which are outliers or not. A fit using a special function like logarithms, exponatial functions or combinations of them makes sense if you are searching the parameters of the underlying physics, but then you would have to provide the general form of the function derived from the physics.

As it seems that you just have the data and no more information about it I guess a polynomial fit or s spline interpolation (which runs exactly through all your datapoints) is as good as any.

Genfit() won't find the type of curve which fits best automatically but you will have to provide it, genfit() will only find the "optimal" parameters and very often is VERY sensible concerning the initial guess values you also have to provide. There are specialized programs which try to find the best type of function, one of them is "CurveExpert Professional". Not sure if they offer a trial version to download.

And yes, you can integrate and differentiate "functions" derived by interpolation routines - numerically only, of course.

See if the attached sheet helps. Think f1 is what you want to use but maybe you are even happy with linear interpolation (f0).

22.02.png