0

I am trying to make a project witch involves calculating an interpolation from raw data and its derivative. I have two arrays looking like this:

A = { 1, 2, 3, 4, ... , n }
B = { 0.23, 0.43, 0.24, 0.19, ... , n }

I want a function to describe the following arrays, so I am using apache-common-math library in order to interpolate the polynom that will describe a function where: F(A[i]) = B[i]. Afterwards, I wish to calculate the derivative of this function in order to be able to find extremum(max/min).

For some reason, I am having trouble with the derivative part.

Currently using:

        DividedDifferenceInterpolator devider = new DividedDifferenceInterpolator();
        PolynomialFunctionNewtonForm polynom = devider.interpolate(xArray,  
        yArray);

Now i have polynom, which is the function representing my previous arrays. How should I calculate its derivative..?

Thanks.

Sielar
  • 292
  • 2
  • 10
  • The interpolating polynomial function constructed using `N` points can potentially have `N-2` extrema. So are you interested in calculating all these extrema? Secondly, its better to use a optimization library instead of interpolating, differentiating and then finding the root.You may try the common maths optimization library to calculate extrema instead. The documentation [here](http://commons.apache.org/proper/commons-math/userguide/optimization.html) is self explanatory. – Sourabh Bhat Mar 08 '15 at 06:24
  • Thanks for your help, yes, i wish to find all of them or several in a certain region. I have tried working the the optimization library, and having trouble there as well. for some reason the derivative is a problem because i do not fully understand how should i calculate derivative for a newtonForm function from the explanation provided by apache. Thanks. – Sielar Mar 08 '15 at 09:49

2 Answers2

1

You can get the derivative by extracting the coefficients from the Newton form polynomial, creating a PolynomialFunction from it and then using that function's derivative method. Here is an example:

double[] x = {0, 1, 2, 3, 4, 5};
double[] y = new double[6];
for (int i = 0; i < 6; i++) {
    y[i] = 1 + 2 * x[i] + 3 * x[i] * x[i];
}
DividedDifferenceInterpolator divider = new DividedDifferenceInterpolator();
PolynomialFunctionNewtonForm polynom = divider.interpolate(x, y);
double[] coefficients = polynom.getCoefficients();
System.out.println(Arrays.toString(coefficients));
PolynomialFunction derivative =
 (PolynomialFunction) new PolynomialFunction(coefficients).derivative();
System.out.println(Arrays.toString(derivative.getCoefficients()));

The code above generates points from the polynomial y = 1 + 2x + 3x^2. The output should be

[1.0, 2.0, 3.0, 0.0, 0.0, 0.0]
[2.0, 6.0]
Phil Steitz
  • 644
  • 3
  • 10
0

You may find the derivative of the polynomial much easily by using Lagrange polynomial instead of using a library.

You may write a simple function (implement UnivariateFunction from apache Commons). Refer to the equation by @Krokop on this page.

Then you may use the root-finding library from commons math given here, look at the "typical usage" given on the page.

I am not sure if there is a much simpler way of doing it.

Community
  • 1
  • 1
Sourabh Bhat
  • 1,793
  • 16
  • 21