## Introduction to fminsearch in Matlab

fminsearch function is one of the most important function that is used in Matlab to find the minimum value of the function and it returns a scalar value where the provided inputs can be in the form of matrix or vector. It starts initially at a particular point and then executes the minimum function to the specified objective function in the input argument. It follows Nelder-Mead simplex algorithm approach. The algorithm used is a direct search method and it does not use any complex analytical gradient in it.

### Working of fminsearch in Matlab

In Matlab, fminsearch function uses a derivative-free methodology to find the minimum of the unconstrained function as mentioned in the input argument of the syntax. It is specified by f(x) where f(x) is a function where x can be of vector or matrix type and it returns a scalar quantity.

**Syntax:**

`a=fminsearch (function, a0)`

- This is used to find the minimum value that starts at an initial point (a0) to calculate the local minimum value of the function(function) mentioned in the input argument.

`a=fminsearch (function, a0, options)`

- This is used to minimize the optimization options that are mentioned in the options of the structure. The input argument “options” can be set by optimset.

`a=fminsearch (problem)`

- This is used to find the minimum of a problem where the problem mentioned in syntax is a structure.

`[a, fval] =fminsearch (__)`

- This is used to find value in fval of the given function at a given point.

`[a fval, exitflag condition] = fminsearch (__)`

- This is used when we want to give any exit condition that returns a value in the exitflag.

`[a fval, exitflag condition, output] = fminsearch (__)`

- This is used to return the structure output that contains information about the process of optimization.

The input arguments and output arguments should follow certain criteria and conditions before using the fminsearch function in Matlab. The input argument “function” can be of function handle type or any function name. It can accept inputs as a vector of the array which returns a scalar quantity. The data types that can be accepted are char, string, and function_handle. The input argument “a0” decides the initial point which can take inputs as real vector or real array. The data type that can be accepted by a0 should of type double. The input argument option is specified of the type structure that is returned by optimset. We can use this to change the values of the input by setting the field in the respective structure.

Given below are Set options that are used in Matlab:

**1. Display Option:** There are different levels of display like:

- Notify is the default level which is meant to display the output if the function mentioned does not converge.
- Final level displays the last or final output.
- Off option gives no output.
- Iter is meant to display the output if there is any iteration.

**2. FunValCheck Option:** It checks the function values are valid or not. If it is ‘on’ then there is an error when the value of the resultant function is complex or NaN. Similarly, if it is ‘off’ then there is no error.

4.5 (6,009 ratings)

View Course

**3. MaxFunEvals Option:** This gives the maximum number of evaluations that can be allowed and it should be always positive.

**4. MaxIter:** This is used to give the maximum number of iterations that are allowed and it should be always positive.

**5. PlotFcns:** This is used to plot the values graphically. We can use the default functions or can write custom plot function to plot the results.

**@optimplotx:**This is used to plot the current plot that is being executed.**@optimplotfunccount:**This is used to plot the count of the function.**@optimplotfval:**This is meant to plot the value of the function.

Another input argument ‘problem’ can be specified as type ‘structure ‘with the below fields:

- If the field name is ‘objective’, then the entry is an objective function.
- If the field name is ‘a0’, then the entry is starting point for the value x.
- If the field name is ‘solver’, then the entry is fminsearch function.
- If the field name is ‘options’, then the entry is an options structure.

The data type that is accepted by the respective input argument is of type structure.

The output argument a is the solution which can be a real array or a real vector. a and a0 size should be the same. Exitflag is another output argument that gives us the reason why the fminsearch algorithm stopped. It can take integer values like -1,0 and 1. If the value is -1 then the specified function converged. If the value is 0 then the iterations number exceeded, then the values mentioned in the MaxIter or the evaluations number exceeded then the values mentioned in the MaxFunEvals Option. If the value is 0, then it was stopped by the output function.

### Examples of fminsearch in Matlab

Please find the below examples that show the use of fminsearch function in Matlab:

#### Example #1

To use fminsearch function for the below objective function:

`f = @(a)200*(a(1) - a(1)^2)^2 + (1 - a(1))^2;`

a0 = [-1,1];

a = fminsearch(f,a0)

**Output:**

**Example #2**

To use fminsearch function for the below objective function with extra parameters:

`f = @(b,a)200*(b(2) - b(1)^2)^2 + (a-b(1))^2;`

a = 2;

fu = @(b)f(b,a);

b0 = [-1,2];

b = fminsearch(fu,b0)

**Output:**

#### Example #3

To find the minimum location and its value for the below objective function using fminsearch:

`a0 = [3,4,5];`

fu = @(a)-norm(a+a0)^2*exp(-norm(a-a0)^2 + sum(a));

[a,fval] = fminsearch(fu,a0)

**Output:**

### Conclusion

fminsearch is widely used in Matlab, so it is important to understand it’s working. There are certain things that should be looked into like fminsearch only minimizes if it contains real numbers. If the input has complex value, then it splits it into real and imaginary components before using fminsearch function.

### Recommended Articles

This is a guide to fminsearch in Matlab. Here we discuss the introduction, working, and examples along with its code implementation and outputs. You may also have a look at the following articles to learn more –