Reputation: 12650
I'm beginning one of my first C# projects--need to find the curve fit for several x-y data points.
For example:
x: 1,2,3,4,5,6 y: 0.5,5,0.5,2.5,5,0.5
As it happens, the proper curve fit I need for these points is a sixth-order polynomial, according to excel.
How can I get the coefficients and exponents of this curve to write the proper expression in C#? I want to stay away from libraries, because this will most likely end up being converted to C for use on microprocessors.
I'm new to C# and know very little about hardware/software integration.
That said, I'm reading about numerical methodology right now...step two of this project will be to take the curve and numerically integrate between successive minimums...
Any advice/pointers are greatly appreciated.
The input will be given by six x-y coordinates...
Problem #1: How do I write a polynomial given six coordinates?
Upvotes: 3
Views: 9766
Reputation: 933
I successfully used MathNET.Numerics library for this purpose. It can be installed as a Nuget package and seems to be actively maintained. Curve fitting is described in its documentation here.
Upvotes: 0
Reputation: 13937
The problem of finding the coefficients of a polynomial given n points evaluated at certain xi is known as polynomial interpolation problem. You can read the details of the problem and its solutions here (wikipedia.org).
You should pay close attention to the section Constructing the interpolation polynomial, where they mention that the matrix you need to invert can introduce large errors if you use Gaussian elimination, and check out Newton interpolation (wikipedia.org) for a better approach. It probably won't matter much for only six points, but it is worth knowing about.
As for implementation, you have two options: make use of a third party library that has linear algebra support - such as Science Code .Net (sciencecode.com), or start writing some basic abstractions for vectors and matrices, and implement basic operations such as multiplication, addition, and inversion. Back in the day, we used a library called "Numerical Recipes in C", and they may well have a port for C#. It may be worth checking out.
Upvotes: 3
Reputation: 16281
It looks like you are seriously overfitting if you think a sixth order polynomial is the best fit for six data points and I'm curious if you actually mean that you will have only six points in the actual data (in which case fitting doesn't make sense) or that you have only six sample points and are expected to predict the actual shape of the data.
https://www.coursera.org/course/ml has an excellent class on machine learning, more specifically relevant because it teaches about choosing the proper order of polynomial automatically, partitioning data and more fundamental aspects such as the matrix math that are underlying the whole thing.
It isn't the kind of thing you can point to "algorithm X" and hope it will come out right... the ML course covers a lot of the mistakes people make (over fitting, under fitting, poor data sampling, etc...) when doing fitting to data. It also discusses how to avoid them.
Upvotes: 0