# fmincon

Solves a general nonlinear optimization problem.

### Calling Sequence

xopt = fmincon(f,x0,A,b) xopt = fmincon(f,x0,A,b,Aeq,beq) xopt = fmincon(f,x0,A,b,Aeq,beq,lb,ub) xopt = fmincon(f,x0,A,b,Aeq,beq,lb,ub,nlc) xopt = fmincon(f,x0,A,b,Aeq,beq,lb,ub,nlc,options) [xopt,fopt] = fmincon(.....) [xopt,fopt,exitflag]= fmincon(.....) [xopt,fopt,exitflag,output]= fmincon(.....) [xopt,fopt,exitflag,output,lambda]=fmincon(.....) [xopt,fopt,exitflag,output,lambda,gradient]=fmincon(.....) [xopt,fopt,exitflag,output,lambda,gradient,hessian]=fmincon(.....)

### Input Parameters

- f :
A function, representing the objective function of the problem.

- x0 :
A vector of doubles, containing the starting values of variables of size (1 X n) or (n X 1) where 'n' is the number of variables.

- A :
A matrix of doubles, containing the coefficients of linear inequality constraints of size (m X n) where 'm' is the number of linear inequality constraints.

- b :
A vector of doubles, related to 'A' and represents the linear coefficients in the linear inequality constraints of size (m X 1).

- Aeq :
A matrix of doubles, containing the coefficients of linear equality constraints of size (m1 X n) where 'm1' is the number of linear equality constraints.

- beq :
A vector of double, vector of doubles, related to 'Aeq' and represents the linear coefficients in the equality constraints of size (m1 X 1).

- lb :
A vector of doubles, containing the lower bounds of the variables of size (1 X n) or (n X 1) where 'n' is the number of variables.

- ub :
A vector of doubles, containing the upper bounds of the variables of size (1 X n) or (n X 1) where 'n' is the number of variables.

- nlc :
A function, representing the nonlinear Constraints functions(both Equality and Inequality) of the problem. It is declared in such a way that nonlinear inequality constraints (c), and the nonlinear equality constraints (ceq) are defined as separate single row vectors.

- options :
A list, containing the option for user to specify. See below for details.

### Outputs

- xopt :
A vector of doubles, containing the computed solution of the optimization problem.

- fopt :
A double, containing the value of the function at x.

- exitflag :
An integer, containing the flag which denotes the reason for termination of algorithm. See below for details.

- output :
A structure, containing the information about the optimization. See below for details.

- lambda :
A structure, containing the Lagrange multipliers of the lower bounds, upper bounds and constraints at the optimized point. See below for details.

- gradient :
A vector of doubles, containing the objective's gradient of the solution.

- hessian :
A matrix of doubles, containing the Lagrangian's hessian of the solution.

### Description

Search the minimum of a constrained optimization problem specified by:

Find the minimum of f(x) such that

fmincon calls Ipopt, an optimization library written in C++, to solve the Constrained Optimization problem.

### Options

The options allow the user to set various parameters of the Optimization problem. The syntax for the options is given by:options= list("MaxIter", [---], "CpuTime", [---], "GradObj", ---, "Hessian", ---, "GradCon", ---);

The options should be defined as type "list" and consist of the following fields:

- MaxIter : A Scalar, specifying the maximum number of iterations that the solver should take.
- CpuTime : A Scalar, specifying the maximum amount of CPU time in seconds that the solver should take.
- GradObj : A function, representing the gradient function of the Objective in vector form.
- Hessian : A function, representing the hessian function of the Lagrange in the form of a Symmetric Matrix with Input parameters as x, Objective factor and Lambda. Refer to Example 5 for definition of Lagrangian Hessian function.
- GradCon : A function, representing the gradient of the nonlinear Constraints (both Equality and Inequality) of the problem. It is declared in such a way that gradient of nonlinear inequality constraints are defined first as a separate Matrix (cg of size m2 X n or as an empty), followed by gradient of nonlinear equality constraints as a separate matrix (ceqg of size m2 X n or as an empty) where m2 & m3 are number of nonlinear inequality and equality constraints respectively.

options = list("MaxIter", [3000], "CpuTime", [600]);

The exitflag allows the user to know the status of the optimization which is returned by Ipopt. The values it can take and what they indicate is described below:

- 0 : Optimal Solution Found
- 1 : Maximum Number of Iterations Exceeded. Output may not be optimal.
- 2 : Maximum amount of CPU Time exceeded. Output may not be optimal.
- 3 : Stop at Tiny Step.
- 4 : Solved To Acceptable Level.
- 5 : Converged to a point of local infeasibility.

For more details on exitflag, see the Ipopt documentation which can be found on http://www.coin-or.org/Ipopt/documentation/

The output data structure contains detailed information about the optimization process. It is of type "struct" and contains the following fields.

- output.Iterations: The number of iterations performed.
- output.Cpu_Time : The total cpu-time taken.
- output.Objective_Evaluation: The number of Objective Evaluations performed.
- output.Dual_Infeasibility : The Dual Infeasiblity of the final soution.
- output.Message: The output message for the problem.

The lambda data structure contains the Lagrange multipliers at the end of optimization. In the current version, the values are returned only when the the solution is optimal. It has type "struct" and contains the following fields.

- lambda.lower: The Lagrange multipliers for the lower bound constraints.
- lambda.upper: The Lagrange multipliers for the upper bound constraints.
- lambda.eqlin: The Lagrange multipliers for the linear equality constraints.
- lambda.ineqlin: The Lagrange multipliers for the linear inequality constraints.
- lambda.eqnonlin: The Lagrange multipliers for the nonlinear equality constraints.
- lambda.ineqnonlin: The Lagrange multipliers for the nonlinear inequality constraints.

A few examples displaying the various functionalities of fmincon have been provided below. You will find a series problems and the appropriate code snippets to solve them.

### Example

Here we solve a simple nonlinear objective function, subjected to three linear inequality constraints.

Find x in R^2 such that it minimizes:

//Example 1: //Objective function to be minimised function y=f(x) y=x(1)^2 - x(1)*x(2)/3 + x(2)^2; endfunction //Starting point, and linear constraints. Since we haven't added any eqaulity constraints or variable bounds, we need not specify them. x0=[0 , 0]; A=[1,1 ; 1,1/4 ; 1,-1]; b=[2;1;1]; [x,fval,exitflag,output,lambda,grad,hessian] =fmincon(f, x0,A,b) |

### Example

Here we build up on the previous example by adding linear equality constraints. We add the following constraints to the problem specified above:

//Example 2: //Objective function to be minimised function y=f(x) y=x(1)^2 - x(1)*x(2)/3 + x(2)^2; endfunction //Starting point, and linear constraints. x0=[0 , 0]; A=[1,1 ; 1,1/4 ; -1,1]; b=[2;1;2]; //We specify the linear equality constraints below. Aeq = [1,-1; 2, 1]; beq = [1;2]; [x,fval,exitflag,output,lambda,grad,hessian] =fmincon(f, x0,A,b,Aeq,beq); |

### Example

In this example, we proceed to add the upper and lower bounds to the objective function.

//Example 3: //Objective function to be minimised function y=f(x) y=x(1)^2 - x(1)*x(2)/3 + x(2)^2; endfunction //Starting point, and linear constraints. x0=[0 , 0]; A=[1,1 ; 1,1/4 ; -1,1]; b=[2;1;2]; //We specify the linear equality constraints below. Aeq = [1,-1; 2, 1]; beq = [1;2]; //The upper and lower bounds for the objective function are defined in simple vectors as shown below. lb = [-1;-%inf]; ub = [%inf;1]; // [x,fval,exitflag,output,lambda,grad,hessian] =fmincon(f, x0,A,b,Aeq,beq,lb,ub); //Press ENTER to continue |

### Example

Finally, we add the nonlinear constraints to the problem. Note that there is a notable difference in the way this is done as compared to defining the linear constraints.

//Example 4: //Objective function to be minimised function y=f(x) y=x(1)^2 - x(1)*x(2)/3 + x(2)^2; endfunction //Starting point, and linear constraints. x0=[0 , 0]; A=[1,1 ; 1,1/4 ; -1,1]; b=[2;1;2]; //We specify the linear equality constraints below. Aeq = [1,-1; 2, 1]; beq = [1;2]; //The upper and lower bound for the objective function are specified below. lb = [-1;-%inf]; ub = [%inf;1]; //Nonlinear constraints are required to be defined as a single function with the inequality and equality constraints in separate vectors. function [c, ceq]=nlc(x) c=[x(1)^2-1,x(1)^2+x(2)^2-1]; ceq=[]; endfunction [x,fval,exitflag,output,lambda,grad,hessian] =fmincon(f, x0,A,b,Aeq,beq,lb,ub,nlc); //Press ENTER to continue |

### Example

Additional Functionality:

We can further enhance the functionality of fmincon by setting input options. We can pre-define the gradient of the objective function and/or the hessian of the lagrange function and thereby improve the speed of computation. This is elaborated on in example 5. We take the following problem and add simple nonlinear constraints, specify the gradients and the hessian of the Lagrange Function. We also set solver parameters using the options.

//Example 5: //Objective function to be minimised function y=f(x) y=x(1)*x(2)+x(2)*x(3); endfunction //Starting point, linear constraints and variable bounds x0=[0.1 , 0.1 , 0.1]; A=[]; b=[]; Aeq=[]; beq=[]; lb=[]; ub=[]; //Nonlinear constraints function [c, ceq]=nlc(x) c = [x(1)^2 - x(2)^2 + x(3)^2 - 2 , x(1)^2 + x(2)^2 + x(3)^2 - 10]; ceq = []; endfunction //Gradient of objective function function y=fGrad(x) y= [x(2),x(1)+x(3),x(2)]; endfunction //Hessian of the Lagrange Function, which has been pre-defined to improve solver speed. function y=lHess(x, obj, lambda) y= obj*[0,1,0;1,0,1;0,1,0] + lambda(1)*[2,0,0;0,-2,0;0,0,2] + lambda(2)*[2,0,0;0,2,0;0,0,2] endfunction //Gradient of nonlinear Constraints function [cg, ceqg]=cGrad(x) cg=[2*x(1) , -2*x(2) , 2*x(3) ; 2*x(1) , 2*x(2) , 2*x(3)]; ceqg=[]; endfunction //Options options=list("MaxIter", [1500], "CpuTime", [500], "GradObj", fGrad, "Hessian", lHess,"GradCon", cGrad); //Calling Ipopt [x,fval,exitflag,output] =fmincon(f, x0,A,b,Aeq,beq,lb,ub,nlc,options) //Press ENTER to continue |

### Example

Infeasible Problems: Find x in R^2 such that it minimizes:

//Example 6: //Infeasible objective function. function y=f(x) y=x(1)^2 - x(1)*x(2)/3 + x(2)^2; endfunction x0=[0 , 0]; A=[1,1 ; 1,1/4 ; 1,-1]; b=[2;1;1]; Aeq = [1,1]; beq = 3; [x,fval,exitflag,output,lambda,grad,hessian] =fmincon(f, x0,A,b,Aeq,beq) |

### Example

Unbounded Problems: Find x in R^2 such that it minimizes:

//Example 7: Unbounded objective function. function y=f(x) y=-(x(1)^2 - x(1)*x(2)/3 + x(2)^2); endfunction x0=[0 , 0]; A=[-1,-1 ; 1,1]; b=[-2;1]; [x,fval,exitflag,output,lambda,grad,hessian] =fmincon(f, x0,A,b); |

### Authors

- R.Vidyadhar , Vignesh Kannan