DifferentiableFunction model element
Function defines a differentiable real function of one variable, y = f(x), and provides root finding and integration capabilities for it.
(Notice that there is also a RealFunction element for non-differentiable functions.)
The element encapsulates access to Analysis routines
in the Apache Common numerics library.
Usage
To add a differentiable function element to your model, drag the icon element to the list of your model elements
and edit it to define the function and its derivative, and (optionally) change the root finding and integration methods.
The element implements the following basic methods:
- double value(double x): evaluates the function at the argument
- double solve(int maxEvals, double min, double max): finds a root in the given interval, with the prescribed maximum number of function evaluations
- double solve(int maxEvals, double min, double max, double start): finds a root in the given interval starting at the given point, with the prescribed maximum number of function evaluations
- double solve(int maxEvals, double start): finds a root in the vicinity of the given point, with the prescribed maximum number of function evaluations
- double integrate(int maxEvals, double min, double max): integrates the function in the given interval, with the prescribed maximum number of function evaluations
All these methods print an error message and return a Double.NaN value if there was any error in the computation.
The element also implements the following methods for backwards compatibility, which call the corresponding methods above with a maximum number of evaluations equals to Integer.MAX_VALUE:
- double solve(double min, double max)
- double solve(double min, double max, double start)
- double solve(double start)
- double integrate(double min, double max)
Besides this, the encapsulated Apache objects can be accessed using the methods:
- void setSolver(BaseUnivariateSolver<UnivariateFunction> solver): sets the root finding solver
- BaseUnivariateSolver<UnivariateFunction> getSolver(): returns the root finding solver
- void setDiffSolver(BaseUnivariateSolver<DifferentiableUnivariateFunction> solver): sets the root finding differentiable solver
- BaseUnivariateSolver<DifferentiableUnivariateFunction> getSolver(): returns the root finding differentiable solver, if any
- void setIntegrator(UnivariateIntegrator integrator): sets the integrator
- UnivariateIntegrator getIntegrator(): returns the integrator
Example of use
double y = function.value(0); // Evaluates the function at 0
double root = function.solve(1000, 0,1); // Finds a root in the interval [0,1], allowing for a maximum of 1000 function evaluations
double integral = function.integrate(1000,0,1); // Integrates in the interval [0,1], allowing for a maximum of 1000 function evaluations