c# Numerical Polynomial Regression

c#, numerical-methods, polynomial-math

Solution

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.

Problem

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?

Original source