Re: Apache Math 3.6 and 4 return wrong values using FiniteDifferencesDifferentiator
Jay van Bruggen <[email protected]> Sun, 15 Dec 2024 20:58:38 +0100
| Newsgroups | gmane.comp.jakarta.commons.user |
|---|---|
| Message-ID | <[email protected]> |
allow me to add one more code for demonstration :
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.differentiation.FiniteDifferencesDifferentiator;
import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
//hier kwam Claude mee om de numerieke differentiatie te testen. Het is
nog steeds fout public class dif2 {
public static void main(String[] args) {
// Our function f(x) = 2x³ - 3x² + 4x - 1 UnivariateFunction f =new UnivariateFunction() {
@Override public double value(double x) {
return 15 * Math.pow(x,2) -3 * x;
}
};
// Test different step sizes and number of points double x_eval =10.0;
System.out.println("Testing numerical differentiation at x = " + x_eval);
System.out.println("Expected value: 16.0");
System.out.println("\nVarying step sizes with 5 points:");
double[] stepSizes = {1.0,0.1,0.01,0.001,0.0001};
for (double stepSize : stepSizes) {
FiniteDifferencesDifferentiator diff =new FiniteDifferencesDifferentiator(5, stepSize);
UnivariateFunction derivative = diff.differentiate(f);
System.out.printf("Step size: %.6f -> Result: %.6f%n",
stepSize, derivative.value(x_eval));
}
System.out.println("\nVarying number of points with step size 0.01:");
int[] numPoints = {3,5,7,9,11};
for (int points : numPoints) {
FiniteDifferencesDifferentiator diff =new FiniteDifferencesDifferentiator(points,0.01);
UnivariateFunction derivative = diff.differentiate(f);
System.out.printf("Number of points: %d -> Result: %.6f%n",
points, derivative.value(x_eval));
}
// Verify exact solution double[] coefficients = {0,3,15,};
PolynomialFunction polynomial =new PolynomialFunction(coefficients);
PolynomialFunction derivative = polynomial.polynomialDerivative();
System.out.printf("%nExact derivative value: %.6f%n", derivative.value(x_eval));
}
}
On 15/12/2024 20:53, Jay van Bruggen wrote:
>
>
> 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));
> }
>
>
>
>
>
>