Curve fitting of Data

1 view (last 30 days)
Habib Khan
Habib Khan on 18 Aug 2016
Commented: John D'Errico on 18 Aug 2016
I have a data acquisition system and I want to write a script to find the best curve fit for the received data. But my problem is that I don't know the data before hand and want to find the best curve fitting without user intervention i.e. the program to decide the best fit and provide the equation which is then further used inside another Matlab function automatically. How can I achieve this goal?

Accepted Answer

the cyclist
the cyclist on 18 Aug 2016
Let's take a simple example to show that what you are asking for is not useful.
Suppose your data are x = [2 3 5 7] and y = [4 5 6 7]. You want to find the "best" fit for f(x) = y.
Well, this is simple. The function is
f(2) = 4
f(3) = 5
f(5) = 6
f(7) = 7
and f(x) where x is not equal to 2,3,5, or 7 can be anything. That is a perfect fit to your data.
Now, I know that that is not what you want. I'm not dumb.
It only makes sense to speak of the "best fit" in the context of a predefined set of functions. (You can think of this as your hypothesis space.)
For an introduction to how to think about these things, I strongly recommend the book Learning From Data.
  1 Comment
John D'Errico
John D'Errico on 18 Aug 2016
As cyclist points out, this is less easy than you may think. In fact, there are infinitely many functions that can pass through ANY set of data, and do so exactly.
So finding the best fit is easy. Just pick one of those infinitely many exact solutions. Any one will do.
If you want it to be completely general, for ANY set of data and ANY possible curve shape, this can be an extremely difficult problem.
The point is, you need to pick a model. Even picking a model can be a difficult thing, since it helps if you know something about the process you are modeling. And there are a wide variety of different classes of models.
You might be best off reading some about modeling. In fact, given that wide variety of models, this can take some work, because those various models also come from disparate parts of mathematics.
For example, a simple family of models are the polynomials. But to really understand polynomial modeling, you need to learn at least something about numerical linear algebra. You need to understand some numerical analysis to to understand the limits of polynomials. You might want to learn about orthogonal polynomial bases, as sometimes a better way to achieve a fit. That is just polynomials. Don't forget other basis expansions, like Fourier series.
But then you might look at a variety of nonlinear models, which can be put together in many ways. There are many ways to do the fitting too.
Next, you could consider a variety of other tools, everything from interpolating splines, neural networks, wavelet decomposition tools, smoothing splines, various kinds of least squares splines, differential equation models, yada yada yada.
Oh, so what is the "best" model anyway? Best must be defined in terms of what you need to do with it. Your goals must be brought into play. As well, you have knowledge about the system under study. Is there noise in the data? How much noise? Should you be trying to fit the data exactly? Is there a bump in the data? Is that just noise to be ignored/smoothed away? Are there outliers in your data? Is the noise classical additive Gaussian noise? Or is the noise perhaps proportional noise?
So wanting a magic tool that will take any set of data and find the best model will take some serious effort to write, but only after you invest a serious amount in reading, in learning how modeling can be done.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!