### 24.3 Nonlinear Programming

Octave can also perform general nonlinear minimization using a successive quadratic programming solver.

— Function File: [x, obj, info, iter, nf, lambda] = sqp (x0, phi)
— Function File: [...] = sqp (x0, phi, g)
— Function File: [...] = sqp (x0, phi, g, h)
— Function File: [...] = sqp (x0, phi, g, h, lb, ub)
— Function File: [...] = sqp (x0, phi, g, h, lb, ub, maxiter)
— Function File: [...] = sqp (x0, phi, g, h, lb, ub, maxiter, tol)

> Solve the nonlinear program

>                    min phi (x)
>                     x

> subject to

>                    g(x)  = 0
>                    h(x) >= 0
>                    lb <= x <= ub

> using a sequential quadratic programming method.

> The first argument is the initial guess for the vector x0.

> The second argument is a function handle pointing to the objective function phi. The objective function must accept one vector argument and return a scalar.

> The second argument may also be a 2- or 3-element cell array of function handles. The first element should point to the objective function, the second should point to a function that computes the gradient of the objective function, and the third should point to a function that computes the Hessian of the objective function. If the gradient function is not supplied, the gradient is computed by finite differences. If the Hessian function is not supplied, a BFGS update formula is used to approximate the Hessian.

> When supplied, the gradient function phi`{2}` must accept one vector argument and return a vector. When supplifed, the Hessian function phi`{3}` must accept one vector argument and return a matrix.

> The third and fourth arguments g and h are function handles pointing to functions that compute the equality constraints and the inequality constraints, respectively. If the problem does not have equality (or inequality) constraints, then use an empty matrix ([]) for g (or h). When supplied, these equality and inequality constraint functions must accept one vector argument and return a vector.

> The third and fourth arguments may also be 2-element cell arrays of function handles. The first element should point to the constraint function and the second should point to a function that computes the gradient of the constraint function:

>                               [ d f(x)   d f(x)        d f(x) ]
>                   transpose ( [ ------   -----   ...   ------ ] )
>                               [  dx_1     dx_2          dx_N  ]

> The fifth and sixth arguments, lb and ub, contain lower and upper bounds on x. These must be consistent with the equality and inequality constraints g and h. If the arguments are vectors then x(i) is bound by lb(i) and ub(i). A bound can also be a scalar in which case all elements of x will share the same bound. If only one bound (lb, ub) is specified then the other will default to (-realmax, +realmax).

> The seventh argument maxiter specifies the maximum number of iterations. The default value is 100.

> The eighth argument tol specifies the tolerance for the stopping criteria. The default value is `sqrt(eps)`.

> The value returned in info may be one of the following:

> 101
>   The algorithm terminated normally. Either all constraints meet the requested tolerance, or the stepsize, delta x, is less than tol`* norm (x)`.
> 102
>   The BFGS update failed.
> 103
>   The maximum number of iterations was reached.

> An example of calling `sqp`:

>               function r = g (x)
>                 r = [ sumsq(x)-10;
>                       x(2)*x(3)-5*x(4)*x(5);
>                       x(1)^3+x(2)^3+1 ];
>               endfunction

>               function obj = phi (x)
>                 obj = exp(prod(x)) - 0.5*(x(1)^3+x(2)^3+1)^2;
>               endfunction

>               x0 = [-1.8; 1.7; 1.9; -0.8; -0.8];

>               [x, obj, info, iter, nf, lambda] = sqp (x0, @phi, @g, [])

>               x =

>                 -1.71714
>                  1.59571
>                  1.82725
>                 -0.76364
>                 -0.76364

>               obj = 0.053950
>               info = 101
>               iter = 8
>               nf = 10
>               lambda =

>                 -0.0401627
>                  0.0379578
>                 -0.0052227

> See also: qp.