Home > other >  Is there a way to calculate f''(x)=0 in java?
Is there a way to calculate f''(x)=0 in java?

Time:12-07

Hi i need to know if there's a way to find f''(x) = 0 in java, I need this to calculate the inflexion point of a "random" function... I've done many research but i wasn't able to find a correct answer ( i can't calculate this in other language like python etc... I need to find a solution in java ), any idea ? thanks

For now I only have f''(x) but i don't know where to start to find f''(x)=0

        double[] x = { 0, -0.839, 1.33, -1.148, -1.0338, -0.8989 };
        double[] y = { -2, 0, -0.81, 2.152, 1.24267, 0.342398 }; // test
        double[] root;
        PolynomialRegression regression = new PolynomialRegression(x, y, 3);
        Function f = new Function(regression.toString());
        Derivative deriv = new Derivative(f);
        Function derive = deriv.findDerivative(f);
        Derivative derivSeconde = new Derivative(derive);
   

        System.out.println("Derivative is " deriv.findDerivative(f).f);
        System.out.println(regression.toString());
        System.out.println("Second derivative is " derivSeconde.findDerivative(derive).f);



Output :

Derivative is -3.0045962x^2 4.0051484x 0.0028687536
-1.0015320813678574x^3 2.002574163543064x^2 0.002868753687649257x -1.9999326254109058
Second derivative is -6.0091925x 4.0051484

CodePudding user response:

Hi i need to know if there's a way to find f''(x) = 0 in java, I need this to calculate the inflexion point of a "random" function... I've done many research but i wasn't able to find a correct answer ( i can't calculate this in other language like python etc... I need to find a solution in java ), any idea ? thanks

For now I only have f''(x) but i don't know where to start to find f''(x)=0

    double[] x = { 0, -0.839, 1.33, -1.148, -1.0338, -0.8989 };
    double[] y = { -2, 0, -0.81, 2.152, 1.24267, 0.342398 }; // test
    double[] root;
    PolynomialRegression regression = new PolynomialRegression(x, y, 3);
    Function f = new Function(regression.toString());
    Derivative deriv = new Derivative(f);
    Function derive = deriv.findDerivative(f);
    Derivative derivSeconde = new Derivative(derive);


    System.out.println("Derivative is " deriv.findDerivative(f).f);
    System.out.println(regression.toString());
    System.out.println("Second derivative is " derivSeconde.findDerivative(derive).f);

CodePudding user response:

import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction;

public class SecondDerivativeExample {
    public static void main(String[] args) {
        // Define the original function
        UnivariateFunction f = x -> x * x * x - x * x   1;

        // Create a differentiable function from the original function
        UnivariateDifferentiableFunction fPrime = f::value;

        // Find the second derivative of the original function
        UnivariateDifferentiableFunction fDoublePrime = fPrime.univariateDerivative(2);

        // Evaluate the second derivative at x = 0
        double x = 0;
        DerivativeStructure ds = new DerivativeStructure(1, 1, 0, x);
        double y = fDoublePrime.value(ds);

        // Print the result
        System.out.println("f''(x) = "   y);
    }
}

In this example, the univariateDerivative method is used to find the second derivative of the function f(x) = x^3 - x^2 1. The second derivative is then evaluated at x = 0 using the value method of the UnivariateDifferentiableFunction interface. The result is printed to the console.

  • Related