Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

## Create Problem Structure

### About Problem Structures

To use the GlobalSearch or MultiStart solvers, you must first create a problem structure. There are two ways to create a problem structure: using the createOptimProblem function and exporting from the Optimization app.

For information on creating inputs for the problem structure, see Inputs for Problem Structure.

### Using the createOptimProblem Function

Follow these steps to create a problem structure using the createOptimProblem function.

1. Define your objective function as a file or anonymous function. For details, see Compute Objective Functions. If your solver is lsqcurvefit or lsqnonlin, ensure the objective function returns a vector, not scalar.

2. If relevant, create your constraints, such as bounds and nonlinear constraint functions. For details, see Write Constraints.

3. Create a start point. For example, to create a three-dimensional random start point xstart:

`xstart = randn(3,1);`
4. (Optional) Create an options structure using optimoptions. For example,

`options = optimoptions(@fmincon,'Algorithm','interior-point');`
5. Enter

`problem = createOptimProblem(solver,`

where solver is the name of your local solver:

• For GlobalSearch: 'fmincon'

• For MultiStart the choices are:

• 'fmincon'

• 'fminunc'

• 'lsqcurvefit'

• 'lsqnonlin'

For help choosing, see Optimization Decision Table.

6. Set an initial point using the 'x0' parameter. If your initial point is xstart, and your solver is fmincon, your entry is now

`problem = createOptimProblem('fmincon','x0',xstart,`
7. Include the function handle for your objective function in objective:

```problem = createOptimProblem('fmincon','x0',xstart, ...
'objective',@objfun,```
8. Set bounds and other constraints as applicable.

ConstraintName
lower bounds'lb'
upper bounds'ub'
matrix Aineq for linear inequalities Aineq x ≤ bineq'Aineq'
vector bineq for linear inequalities Aineq x ≤ bineq'bineq'
matrix Aeq for linear equalities Aeq x = beq'Aeq'
vector beq for linear equalities Aeq x = beq'beq'
nonlinear constraint function'nonlcon'

9. If using the lsqcurvefit local solver, include vectors of input data and response data, named 'xdata' and 'ydata' respectively.

10. Best practice: validate the problem structure by running your solver on the structure. For example, if your local solver is fmincon:

`[x fval eflag output] = fmincon(problem);`
 Note:   Specify fmincon as the solver for GlobalSearch, even if you have no constraints. However, you cannot run fmincon on a problem without constraints. Add an artificial constraint to the problem to validate the structure:`problem.lb = -Inf(size(x0));`

#### Example: Creating a Problem Structure with createOptimProblem

This example minimizes the function from Run the Solver, subject to the constraint x1 + 2x2 ≥ 4. The objective is

sixmin = 4x2 – 2.1x4 + x6/3 + xy – 4y2 + 4y4.

Use the interior-point algorithm of fmincon, and set the start point to [2;3].

1. Write a function handle for the objective function.

```sixmin = @(x)(4*x(1)^2 - 2.1*x(1)^4 + x(1)^6/3 ...
+ x(1)*x(2) - 4*x(2)^2 + 4*x(2)^4);```
2. Write the linear constraint matrices. Change the constraint to "less than" form:

```A = [-1,-2];
b = -4;```
3. Create the local options structure to use the interior-point algorithm:

`opts = optimoptions(@fmincon,'Algorithm','interior-point');`
4. Create the problem structure with createOptimProblem:

```problem = createOptimProblem('fmincon', ...
'x0',[2;3],'objective',sixmin, ...
'Aineq',A,'bineq',b,'options',opts)```
5. The resulting structure:

```problem =
objective: @(x)(4*x(1)^2-2.1*x(1)^4+x(1)^6/3+x(1)*x(2)-4*x(2)^2+4*x(2)^4)
x0: [2x1 double]
Aineq: [-1 -2]
bineq: -4
Aeq: []
beq: []
lb: []
ub: []
nonlcon: []
solver: 'fmincon'
options: [1x1 optim.options.Fmincon]```
6. Best practice: validate the problem structure by running your solver on the structure:

`[x fval eflag output] = fmincon(problem);`
 Note:   Specify fmincon as the solver for GlobalSearch, even if you have no constraints. However, you cannot run fmincon on a problem without constraints. Add an artificial constraint to the problem to validate the structure:`problem.lb = -Inf(size(x0));`

### Exporting from the Optimization app

Follow these steps to create a problem structure using the Optimization app.

1. Define your objective function as a file or anonymous function. For details, see Compute Objective Functions. If your solver is lsqcurvefit or lsqnonlin, ensure the objective function returns a vector, not scalar.

2. If relevant, create nonlinear constraint functions. For details, see Nonlinear Constraints.

3. Create a start point. For example, to create a three-dimensional random start point xstart:

`xstart = randn(3,1);`
4. Open the Optimization app by entering optimtool at the command line, or by choosing the Optimization app from the Apps tab.

5. Choose the local Solver.

• For GlobalSearch: fmincon (default).

• For MultiStart:

• fmincon (default)

• fminunc

• lsqcurvefit

• lsqnonlin

For help choosing, see Optimization Decision Table.

6. Choose an appropriate Algorithm. For help choosing, see Choosing the Algorithm.

7. Set an initial point (Start point).

8. Include the function handle for your objective function in Objective function, and, if applicable, include your Nonlinear constraint function. For example,

9. Set bounds, linear constraints, or local Options. For details on constraints, see Writing Constraints.

10. Best practice: run the problem to verify the setup.

 Note:   You must specify fmincon as the solver for GlobalSearch, even if you have no constraints. However, you cannot run fmincon on a problem without constraints. Add an artificial constraint to the problem to verify the setup.
11. Choose File > Export to Workspace and select Export problem and options to a MATLAB structure named

#### Example: Creating a Problem Structure with the Optimization App

This example minimizes the function from Run the Solver, subject to the constraint x1 + 2x2 ≥ 4. The objective is

sixmin = 4x2 – 2.1x4 + x6/3 + xy – 4y2 + 4y4.

Use the interior-point algorithm of fmincon, and set the start point to [2;3].

1. Write a function handle for the objective function.

```sixmin = @(x)(4*x(1)^2 - 2.1*x(1)^4 + x(1)^6/3 ...
+ x(1)*x(2) - 4*x(2)^2 + 4*x(2)^4);```
2. Write the linear constraint matrices. Change the constraint to "less than" form:

```A = [-1,-2];
b = -4;```
3. Launch the Optimization app by entering optimtool at the MATLAB® command line.

4. Set the solver, algorithm, objective, start point, and constraints.

5. Best practice: run the problem to verify the setup.

The problem runs successfully.

6. Choose File > Export to Workspace and select Export problem and options to a MATLAB structure named