| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Genetic Algorithm and Direct Search Toolbox |
| Contents | Index |
| Learn more about Genetic Algorithm and Direct Search Toolbox |
| On this page… |
|---|
To use the genetic algorithm at the command line, call the genetic algorithm function ga with the syntax
[x fval] = ga(@fitnessfun, nvars, options)
where
@fitnessfun is a handle to the fitness function.
nvars is the number of independent variables for the fitness function.
options is a structure containing options for the genetic algorithm. If you do not pass in this argument, ga uses its default options.
The results are given by
x — Point at which the final value is attained
fval — Final value of the fitness function
Using the function ga is convenient if you want to
Return results directly to the MATLAB workspace
Run the genetic algorithm multiple times with different options, by calling ga from an M-file
Using the Genetic Algorithm from the Command Line provides a detailed description of using the function ga and creating the options structure.
To open the Optimization Tool, enter
optimtool('ga')
at the command line, or enter optimtool and then choose ga from the Solver menu.

You can also start the tool from the MATLAB Start menu as pictured:

To use the Optimization Tool, you must first enter the following information:
Fitness function — The objective function you want to minimize. Enter the fitness function in the form @fitnessfun, where fitnessfun.m is an M-file that computes the fitness function. Writing Files for Functions You Want to Optimize explains how write this M-file. The @ sign creates a function handle to fitnessfun.
Number of variables — The length of the input vector to the fitness function. For the function my_fun described in Writing Files for Functions You Want to Optimize, you would enter 2.
You can enter constraints or a nonlinear constraint function for the problem in the Constraints pane. If the problem is unconstrained, leave these fields blank.
To run the genetic algorithm, click the Start button. The tool displays the results of the optimization in the Run solver and view results pane.
You can change the options for the genetic algorithm in the Options pane. To view the options in one of the categories listed in the pane, click the + sign next to it.
For more information,
See the Optimization Tool chapter in the Optimization Toolbox User's Guide.
See Example — Rastrigin's Function for an example of using the tool.
![]() | What Is the Genetic Algorithm? | Example — Rastrigin's Function | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |