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

* Solves a nonlinear least squares problem using a finite difference Jacobian.

* * A nonlinear least squares problem is solved using a finite difference * Jacobian. * The problem is to minimize the vector-function, $$ f(x_1,x_2) = * \begin{bmatrix}10.0(x_2-x_1^2)\\ 1.0-x_1 \end{bmatrix} $$ * *

The analytic Jacobian is * $$ J(x_1,x_2) = \begin{bmatrix}-20.0x_1 & 10.0\\ -1.0 & 0.0 * \end{bmatrix}. $$ *

* @see Code * @see Output */ public class NonlinLeastSquaresEx1 { public static void main(String args[]) throws NonlinLeastSquares.TooManyIterationsException { NonlinLeastSquares.Function zsf = new NonlinLeastSquares.Function() { @Override public void f(double x[], double f[]) { f[0] = 10. * (x[1] - x[0] * x[0]); f[1] = 1. - x[0]; } }; int m = 2; int n = 2; double xguess[] = {-1.2, 1.}; double xscale[] = {1., 1.}; double fscale[] = {1., 1.}; NonlinLeastSquares zs = new NonlinLeastSquares(m, n); zs.setGuess(xguess); zs.setXscale(xscale); zs.setFscale(fscale); double[] x = zs.solve(zsf); for (int k = 0; k < n; k++) { System.out.println("x[" + k + "] = " + x[k]); } } }