2

I know there have been several questions asked relating to interpolation, but I have not found any answers that would be helpful enough for me so I have the following question.

I have two arrays of points. One tracks time (x axis) and one tracks expenses (y axis) and I want to get something like this:

InterpolatingPolynomial[{{0, 10}, {1, 122}, {2, 3.65}, {3, 56.3}, {4, 12.4}, {5, 0}}, x]

(In Mathematica that returs a constructed polynomial that fits the points). Is it possible, to return a func<double,double> constructed from two double arrays in C#?

Thanks in advance.

nsconnector
  • 838
  • 6
  • 12
Andro
  • 2,232
  • 1
  • 27
  • 40

2 Answers2

1

This paper describes exactly what you want. The Vandermonde Determinant method is quite simple to implement as it requires to compute the determinant of a matrix in order to obtain the coefficients of the interpolating polynomial.
I'd suggest to build a class with an appropriate interface though, as building Funcs on the fly is pretty complicated (see here for an example). You could do something like:

public class CosineInterpolation {
    public CosineInterpolation(double[] x, double[] y) { ... }
    public double Interpolate(double x) { ... }
}
Community
  • 1
  • 1
BlackBear
  • 22,411
  • 10
  • 48
  • 86
  • Well this only describes the process of interpolation which I am familliar with. My question is of more programming nature, getting the func object out of the whole thing. Thanks, anyways, seems very iterestng paper though. +1 on a great paper though. – Andro Jan 26 '13 at 12:45
  • @Andrej building Funcs seem to be quite messy to me, I'd use a more "conventional" approach – BlackBear Jan 26 '13 at 12:53
1

I think I found the solution myself after a long day of search. I interpolate a function using Lagrange Interpolation. A Func<double,double> can be then easily constructed using DLINQ. e.g

public Func<doube,double> GetFunction()
{
    LagrangeInterpolation lagInter = new LagrangeInterpolation(xVals, yVals);
    return ( val => lagInter(GetValue(val) );
}

This returns the Func<double,double> object. (I know that creating a new object each time is not a good solution but this is just for demonstrational purposes)

Andro
  • 2,232
  • 1
  • 27
  • 40