# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

## fminunc Unconstrained Minimization

Consider the problem of finding a set of values [x1, x2] that solves

 $\underset{x}{\mathrm{min}}f\left(x\right)={e}^{{x}_{1}}\left(4{x}_{1}^{2}+2{x}_{2}^{2}+4{x}_{1}{x}_{2}+2{x}_{2}+1\right).$ (6-15)

To solve this two-dimensional problem, write a file that returns the function value. Then, invoke the unconstrained minimization routine `fminunc`.

### Step 1: Write a file objfun.m.

This code ships with the toolbox. To view, enter `type objfun`:

```function f = objfun(x) f = exp(x(1)) * (4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1);```

### Step 2: Set options.

Set options to use the `'quasi-newton'` algorithm. Set options because the `'trust-region'` algorithm requires that the objective function include a gradient. If you do not set the options, then, depending on your MATLAB® version, `fminunc` can issue a warning.

`options = optimoptions(@fminunc,'Algorithm','quasi-newton');`

### Step 3: Invoke fminunc using the options.

```x0 = [-1,1]; % Starting guess [x,fval,exitflag,output] = fminunc(@objfun,x0,options);```

This produces the following output:

```Local minimum found. Optimization completed because the size of the gradient is less than the default value of the optimality tolerance.```

View the results, including the first-order optimality measure in the `output` structure.

```x,fval,exitflag,output.firstorderopt x = 0.5000 -1.0000 fval = 3.6609e-16 exitflag = 1 ans = 7.3704e-08```

The `exitflag` tells whether the algorithm converged. `exitflag = 1` means a local minimum was found. The meanings of exitflags are given in function reference pages.

The `output` structure gives more details about the optimization. For `fminunc`, it includes the number of iterations in `iterations`, the number of function evaluations in `funcCount`, the final step-size in `stepsize`, a measure of first-order optimality (which in this unconstrained case is the infinity norm of the gradient at the solution) in `firstorderopt`, the type of algorithm used in `algorithm`, and the exit message (the reason the algorithm stopped).