How to solve user defined from apache commons math

Hi People's,

Could anyone make a working class out example of the Apache's sample code? (code from website is here below)

UnivariateFunction function = // some user defined function object

final double relativeAccuracy = 1.0e-12;

final double absoluteAccuracy = 1.0e-8;

final int maxOrder = 5;

UnivariateSolver solver = new BracketingNthOrderBrentSolver(relativeAccuracy, absoluteAccuracy, maxOrder);

double c = solver.solve(100, function, 1.0, 5.0, AllowedSolution.LEFT_SIDE);

Cheers,

Re: How to solve user defined from apache commons math

What have you tried? What problems are you having?

Re: How to solve user defined from apache commons math

Do you not know what the sample code is asking?

UnivariateFunction is an interface. That comment is telling you that you need to create some class which implements the UnivariateFunction interface and perform some sort of function operations (such as the Abs class, which I think just gets the absolute value of the sent value), and create a new instance of that class where the comment is.

Re: How to solve user defined from apache commons math

This here beneath is what I have so far:

There is an error on the last line of the code which gives no further indication:

import org.apache.commons.math3.analysis.solvers.AllowedS olution;

import org.apache.commons.math3.analysis.solvers.Bracketi ngNthOrderBrentSolver;

import org.apache.commons.math3.analysis.solvers.BrentSol ver;

import org.apache.commons.math3.analysis.solvers.PegasusS olver;

import org.apache.commons.math3.analysis.solvers.Univaria teSolver;

import org.apache.commons.math3.analysis.solvers.Univaria teSolverUtils;

public class test {

static class MyFunction implements UnivariateFunction {

public double value(double x) {

double y = hugeFormula(2);

// if (somethingBadHappens) {

// throw new LocalException(x);

// }

return y;

}

private double hugeFormula(double x) {

// TODO Auto-generated method stub

return x*x+2;

}

}

public static void main(String [] Args){

UnivariateFunction function= new MyFunction();

final double relativeAccuracy = 1.0e-12;

final double absoluteAccuracy = 1.0e-8;

final int maxOrder = 5;

UnivariateSolver solver = new BracketingNthOrderBrentSolver(relativeAccuracy, absoluteAccuracy, maxOrder);

double c = solver.solve(100, function, 1.0, 5.0, AllowedSolution.LEFT_SIDE);

}

}