package com.imsl.test.example.math; import com.imsl.math.*; /** *

* Solves an optimization problem with supplied numerical gradients.

* * This example uses MinUnconMultiVar to minimize $$f(x_1,x_2) = * 100(x_2 - x_1^2)^2 + (1-x_1)^2$$ while using * NumericalDerivatives to compute the numerical gradients. * * @see Code * @see Output */ public class NumericalDerivativesEx6 { static private int m = 1, n = 2; static double fcnEvaluation(double[] x) { return 100. * ((x[1] - x[0] * x[0]) * (x[1] - x[0] * x[0])) + (1. - x[0]) * (1. - x[0]); } static class MyFunction implements MinUnconMultiVar.Gradient { @Override public double f(double[] x) { return fcnEvaluation(x); } @Override public void gradient(double[] x, double[] gp) { NumericalDerivatives.Function fcn = new NumericalDerivatives.Function() { @Override public double[] f(int varIndex, double[] y) { double[] tmp = new double[m]; tmp[0] = fcnEvaluation(y); return tmp; } }; NumericalDerivatives nderv = new NumericalDerivatives(fcn); double[][] jacobian = nderv.evaluateJ(x); gp[0] = jacobian[0][0]; gp[1] = jacobian[0][1]; } } public static void main(String args[]) throws Exception { MinUnconMultiVar solver = new MinUnconMultiVar(n); solver.setGuess(new double[]{-1.2, 1.0}); double x[] = solver.computeMin(new MyFunction()); System.out.println("Minimum point is (" + x[0] + ", " + x[1] + ")"); } }