Author: luc Date: Mon Oct 1 16:01:16 2012 New Revision: 1392414 URL: http://svn.apache.org/viewvc?rev=1392414&view=rev Log: Fixed example code in documentation.
Modified: commons/sandbox/nabla/trunk/src/site/xdoc/index.xml Modified: commons/sandbox/nabla/trunk/src/site/xdoc/index.xml URL: http://svn.apache.org/viewvc/commons/sandbox/nabla/trunk/src/site/xdoc/index.xml?rev=1392414&r1=1392413&r2=1392414&view=diff ============================================================================== --- commons/sandbox/nabla/trunk/src/site/xdoc/index.xml (original) +++ commons/sandbox/nabla/trunk/src/site/xdoc/index.xml Mon Oct 1 16:01:16 2012 @@ -161,30 +161,38 @@ </p> <p> - We can therefore find the maximal value by calling a solver on the - derivative. In this example, we will use - the <a href="http://commons.apache.org/math/apidocs/org/apache/commons/math3/analysis/solvers/BracketingNthOrderBrentSolver.html"> - bracketings n<sup>th</sup> order Brent solver</a> from the <a href="http://commons.apache.org/math/">Apache - Commons Math</a> library. Functions passed to any Apache Commons Math solvers must implement a specific + We can therefore find the maximal value by calling a solver on the derivative. + As functions passed to any Apache Commons Math solvers must implement a specific interface: <a href="http://commons.apache.org/math/apidocs/org/apache/commons/math3/analysis/UnivariateFunction.html"> - UnivariateFunction</a>. In order to comply with this - requirement, we wrap the derivative object into another - object adapting the interface and pass this wrapped - derivative to the solver: + UnivariateFunction</a>. In order to comply with this interface, we wrap the + derivative instance: </p> <source> UnivariateFunction wrappedDerivative = new UnivariateFunction() { public double value(double x) { + // the VALUE of this new function is the DERIVATIVE of the original function DerivativeStructure t = new DerivativeStructure(1, 1, 0, x); - return derivative.f(t).getPartialDerivative(1); + return derivative.value(t).getPartialDerivative(1); } }; - UnivariateSolver solver = new .BracketingNthOrderBrentSolver(5); - double tMax = solver.solve(wrappedDerivative, 0.5, 1.5); + </source> + + <p> + The final step is to call a solver on the derivative, as the roots of the + derivative are the local extremum of the original function. In this example, + we will use the <a + href="http://commons.apache.org/math/apidocs/org/apache/commons/math3/analysis/solvers/BracketingNthOrderBrentSolver.html"> + bracketing n<sup>th</sup> order Brent solver</a> from the <a href="http://commons.apache.org/math/">Apache + Commons Math</a> library: + </p> + + <source> + UnivariateSolver solver = new BracketingNthOrderBrentSolver(1.0e-6, 5); + double tMax = solver.solve(100, wrappedDerivative, 0.5, 1.5); double yMax = derivative.value(new DerivativeStructure(1, 1, 0, tMax)).getValue(); System.out.println("max value = " + yMax + ", at t = " + tMax + - " (" + solver.getIterationCount() + " iterations)"); + " (" + solver.getEvaluations() + " evaluations)"); </source> <p> @@ -192,7 +200,7 @@ </p> <source> - max value = 2.1097470218140537, at t = 0.8987751653383649 (7 iterations) + max value = 2.1097470218140533, at t = 0.8987751646846582 (11 evaluations) </source> </subsection>