MATLAB® Toolbox Tutorial

This page illustrates how you can solve the following constrained nonlinear optimization problem:


   x1*x1 + x2*x2

Subject to:

   x1 + x2 = 1
   x1 * x2 >= 0
   0 <= x1, x2 <= 1

using the Solver Platform SDK with its MATLAB Optimization Toolbox compatible API.  Click the links below to see example MATLAB code written using the SDK's other APIs.

To solve a constrained nonlinear optimization problem using the Optimization Toolbox API, you must write two MATLAB functions -- one to compute values for the objective (we'll call this objfun), and one to compute values for the constraints (we'll call this confun) -- and then call the fmincon function, passing the addresses of these two functions as arguments.  Each MATLAB function is generally placed in a separate M-file.This function computes a value for the objective, given current trial values for the problem variables as chosen by the optimizer:

function f = objfun(x)
   f = x(1) * x(1) + x(2) * x(2);

This function computes values for the constraints, given current trial values for the problem variables.  You must separate the constraints into inequalities (which we'll call c) and equalities (which we'll call ceq).  fmincon expects constraints of the form c <= 0 and ceq = 0.  Our first constraint is x1 + x2 = 1, so we'll have to subtract 1 to form (x1 + x2 - 1) = 0.  Our second constraint is x1 * x2 >= 0, so we must multiply by -1 to form -x1 * x2 <= 0.

function [c, ceq] = confun(x)
   ceq = [x(1) + x(2) - 1];
   c = [-x(1) * x(2)];

Below is the code to call fmincon, passing vectors for the lower and upper bounds on the variables, and the addresses of the two functions above.  You must pass the arguments in the correct order, and supply the empty matrix [] for the arguments you are not using.

function Example4
   lb = [0; 0];
   ub = [1; 1];
   x0 = [1; 1];
   [x,obj,exitflag] =

After calling fmincon, you must test the exitflag value to determine whether any error occurred.  If the optimization was successful, you can display or otherwise work with obj, the final value of the objective, and the vector x, the final values of the decision variables.

< Back to MATLAB API Overview

< Back to MATLAB Users Start Here