Reputation: 2232
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.
Upvotes: 2
Views: 2471
Reputation: 2232
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)
Upvotes: 1
Reputation: 22979
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 Func
s 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) { ... }
}
Upvotes: 1