As previously discussed, use of SNES for solving systems of
nonlinear equations and unconstrained minimization problems is quite
similar. When solving minimization problems, the user typically
provides routines for evaluating the function, gradient, and Hessian
corresponding to Equation (4
). The routine to evaluate
the scalar minimization function, f(x), should be set with
ierr = SNESSetMinimizationFunction(SNES snes, int (*FormMinFunction)(SNES snes,Vec x,double *f,void *ctx),void *ctx);The gradient vector, g(x), and gradient evaluation routine should be set with
ierr = SNESSetGradient(SNES snes,Vec g, int (*FormGradient)(SNES snes,Vec x,Vec g,void *ctx),void *ctx);In these routines, the argument ctx specifies an optional context for application-specific data, as described in Section Solving Systems of Nonlinear Equations .
The user must also set a routine to form some approximation of the Hessian
matrix, A, as is typically done with
ierr = SNESSetHessian(SNES snes,Mat A,Mat B,int (*FormHessian)(SNES snes,Vec x, Mat *A,Mat *B,MatStructure *flag,void *ctx),void *ctx);The arguments of the routine FormHessian() are the current iterate, x; the Hessian matrix, A; the preconditioner matrix, B (which is usually the same as A); a flag indicating information about the preconditioner structure; and an optional user-defined Hessian context, ctx. Reuse of matrix and preconditioner data during successive iterations of the nonlinear solvers is often critical for achieving good performance. This topic is discussed in detail for the case of solving systems of nonlinear equations in Section Solving Systems of Nonlinear Equations ; the options are identical for solving unconstrained minimization problems, and thus are not repeated here.
The directory ${}PETSC_DIR/src/snes/examples/tests/umin provides examples of solving unconstrained minimization problems.