Mathematics Previous page   Next Page

Output Functions

An output function is a function that an optimization function calls at each iteration of its algorithm. Typically, you might use an output function to generate graphical output, record the history of the data the algorithm generates, or halt the algorithm based on the data at the current iteration. You can create an output function as an M-file function, a subfunction, or a nested function.

You can use the OutputFcn option with the following MATLAB optimization functions:

This section covers the following topics:

Creating and Using an Output Function

The following is a simple example of an output function that plots the points generated by an optimization function.

You can use this output function to plot the points generated by fminsearch in solving the optimization problem

To do so,

  1. Create an M-file containing the preceding code and save it as outfun.m in a directory on the MATLAB path.
  2. Enter the command
  1. to set the value of the Outputfcn field of the options structure to a function handle to outfun.

  1. Enter the following commands:

This returns the solution

and displays the following plot of the points generated by fminsearch:

plot of the points generated by fminsearch

Structure of the Output Function

The function definition line of the output function has the following form:

where

The optimization function passes the values of the input arguments to outfun at each iteration.

Example of a Nested Output Function

The example in Creating and Using an Output Function does not require the output function to preserve data from one iteration to the next. When this is the case, you can write the output function as an M-file and call the optimization function directly from the command line. However, if you want your output function to record data from one iteration to the next, you should write a single M-file that does the following:

In the following example, the M-file also contains the objective function as a subfunction, although you could also write the objective function as a separate M-file or as an anonymous function.

Since the nested function has access to variables in the M-file function that contains it, this method enables the output function to preserve variables from one iteration to the next.

The following example uses an output function to record the points generated by fminsearch in solving the optimization problem

and returns the sequence of points as a matrix called history.

To run the example, do the following steps:

  1. Open a new M-file in the MATLAB editor.
  2. Copy and paste the following code into the M-file.
  3. Save the file as myproblem.m in a directory on the MATLAB path.
  4. At the MATLAB prompt, enter

The function fminsearch returns x, the optimal point, and fval, the value of the objective function at x.

In addition, the output function myoutput returns the matrix history, which contains the points generated by the algorithm at each iteration, to the MATLAB workspace. The first four rows of history are

The final row of points is the same as the optimal point, x.

Fields in optimValues

The following table lists the fields of the optimValues structure that are provided by all three optimization functions, fminbnd, fminsearch, and fzero. The function fzero also provides additional fields that are described in its reference page.

The "Command-Line Display Headings" column of the table lists the headings, corresponding to the optimValues fields that are displayed at the command line when you set the Display parameter of options to 'iter'.

optimValues Field (optimValues.field)
Description
Command-Line Display Heading
funcount
Cumulative number of function evaluations.

Func-count

fval
Function value at current point.

min f(x)

iteration
Iteration number -- starts at 0.

Iteration

procedure
Procedure messages

Procedure

States of the Algorithm

The following table lists the possible values for state:

State
Description
'init'
The algorithm is in the initial state before the first iteration.
'interrupt'
The algorithm is performing an iteration. In this state, the output function can interrupt the current iteration of the optimization. You might want the output function to do this to improve the efficiency of the computations. When state is set to 'interrupt', the values of x and optimValues are the same as at the last call to the output function, in which state is set to 'iter'.
'iter'
The algorithm is at the end of an iteration.
'done'
The algorithm is in the final state after the last iteration.

The following code illustrates how the output function might use the value of state to decide which tasks to perform at the current iteration.

Stop Flag

The output argument stop is a flag that is true or false. The flag tells the optimization function whether the optimization should quit or continue. The following examples show typical ways to use the stop flag.

Stopping an Optimization Based on Data in optimValues.   The output function can stop an optimization at any iteration based on the current data in optimValues. For example, the following code sets stop to true if the objective function value is less than 5:

Stopping an Optimization Based on GUI Input.   If you design a GUI to perform optimizations, you can make the output function stop an optimization when a user clicks a Stop button on the GUI. The following code shows how to do this, assuming that the Stop button callback stores the value true in the optimstop field of a handles structure called hObject stored in appdata.


Previous page  Setting Minimization Options Finding Zeros of Functions Next page

© 1994-2005 The MathWorks, Inc.