Apache Math 3.6 and 4 return wrong values using FiniteDifferencesDifferentiator

Jay van Bruggen <[email protected]> Sun, 15 Dec 2024 20:53:15 +0100
Newsgroups gmane.comp.jakarta.commons.user
Message-ID <[email protected]>
Hi, I have used Apache math a lot lately,
to my great satisfaction.
However,
Using FiniteDifferencesDifferentiator, In both 3.6 and 4.0 I have tried 
and compared the
Numerical Differentiation methods, using functions I can differentiate 
by hand for control.
For 2 simple functions , I get wrong values returned.

the first derivative of the function

f(x) = 2x^3 - 3x^2 + 4x - 1

f'  = 6x^2 -6x + 4

should return   16   in x=2      .
Apache always returns 11     , I have tried many different parameter 
settings.   That's wrong.

Lowering the bar ,

the function
f(x) = 15x^2 + 3x
f'()  =   30x+3
should return 303.

It returns 1530         which is very wrong.

something seems quite wrong off in the FiniteDifferencesDifferentiator.
I could always be mistaken , but  everything compiles, and I have 
checked many times.
If I am wrong I apologize, but I should at least mention this.

Below is two implementations I did using mentioned functions.

I've tried several approaches and i have used many other Apache math 
methods like integration and curvefitting before,
But I cannot get this one to work, It's pretty serious if it returns 
something wrong.

I have a hard time finding the right entrance for this bug report, doI 
have really to create a Jira account?
What can I do to get in contact to report this problem, if this is not 
the right way?

Greets,

Jay van Bruggen

Java 17 , IntelliJ pro 2024.3    Windows 10.

// Import statements import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
import org.apache.commons.math3.analysis.differentiation.FiniteDifferencesDifferentiator;

public class chatdiff {

     public static void main(String[] args) {
         // Define the function f(x) = 2x^3 - 3x^2 + 4x - 1 UnivariateFunction function = x ->2 * Math.pow(x,3) -3 * Math.pow(x,2) +4 * x -1;

         // Create a finite differences differentiator // The first argument is 
the number of points for interpolation; the second is the range size FiniteDifferencesDifferentiator differentiator =new FiniteDifferencesDifferentiator(5,0.01);

         // Differentiate the function UnivariateFunction derivative = differentiator.differentiate(function);

         // Evaluate the first derivative at x = 2 double x =2.0;
         double derivativeAtX = derivative.value(x);

         // Print the result System.out.println("The derivative of f(x) at x = " + x +" is approximately: " + derivativeAtX);
     }
}

     // Numerical differentiation using finite differences System.out.println("\nNumerical Differentiation using Finite Differences:");
     UnivariateFunction f =new UnivariateFunction() {
         @Override public double value(double x) {
             return 15 * Math.pow(x,2) +3 * x;
         }
     };

     FiniteDifferencesDifferentiator differentiator =new FiniteDifferencesDifferentiator(5,0.25);
     UnivariateFunction derivative_numerical = differentiator.differentiate(f);

     // Compare results at x = 2 double x_eval =10.0;
     System.out.println("Numerical derivative at x = " + x_eval +": " + derivative_numerical.value(x_eval));
     System.out.println("Exact derivative at x = " + x_eval +": " + derivative.value(x_eval));
}