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

Minimize

x1*x1 + x2*x2

Subject to:

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

using the Solver Platform SDK with its MATLAB Procedural 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 Procedural API, you must write a MATLAB function (called an Evaluator in the terminology of the SDK) that computes values for the objective and constraints.  You can access a wide range of information about the problem and the current state of the solution in this Evaluator, and signal whether the SDK should continue or terminate the solution process early.

```function ret = EvaluateCallback(prob, refuser, evaltype)    var = SolverSDK('VarValue', prob, 'MyVars');    fcn = [0 0];    fcn(1) = var(1) + var(2); % x1 + x1 = 1    fcn(2) = var(1) * var(2); % x1 * x2 >= 0    obj = var(1)*var(1) + var(2)*var(2); % objective    SolverSDK('FcnValueSet', prob, 'MyCons', fcn);    SolverSDK('FcnValueSet', prob, 'MyObj', obj);    ret = 'Continue'; end```

You call functions to create a Problem object, and add this Evaluator to the Problem object's collection of Evaluators.  In this example, we specify constraints with "right hand sides" and "relations" (<=, =, >=).  The SDK offers several ways to specify constraints -- another way is shown in the Object-Oriented API example code.

For any type of optimization problem, you simply call SolverSDK ('SolverOptimize', prob).  All errors are reported by raising exceptions, which will cause the catch clause to be executed.

```rhs = [1 0]; rel = {['EQ'] ['GE']}; try    prob = SolverSDK('Problem');    SolverSDK('ProbTypeSet', prob, 'Minimize', 'OptNLP');    SolverSDK('VariableAdd', prob, 'MyVars', 'Decision', 2);    SolverSDK('FunctionAdd', prob, 'MyCons', 'Constraint', 2);    SolverSDK('FunctionAdd', prob, 'MyObj', 'Objective', 1);    SolverSDK('FcnRelation', prob, 'MyCons', rel, rhs);    SolverSDK('EvaluatorAdd', prob, 'EvaluateCallback',       '', 'Function');    [status, objval] = SolverSDK('SolverOptimize', prob);    x = SolverSDK('VarFinalValue', prob, 'MyVars'); catch    errstr = lasterror;    disp(errstr.message); end if prob ~= 0    SolverSDK('ProbFree', prob); end```

< Back to MATLAB API Overview

< Back to MATLAB Users Start Here