The Octave package called Optim can be downloaded easily fromĀ https://octave.sourceforge.io/optim/. The following screenshot indicates the version of the package. Note this was the version on March 1, 2018:
![](assets/12f1c67c-ab77-4577-8e30-514378445c1b.png)
After downloading and installing the package (see Chapter 6, Managing Packages, for more detail on how to install an Octave package), we could use the following command lines to see some functions:
pkg load optim pkg describe -verbose optim
The following screenshot shows the output. To save space, only the top part is shown:
![](assets/64690671-0c98-4d99-96d7-80e0ff173fbd.png)
To find the usage of a specific function, we could use the help() function:
help fminsearch
The related output is shown in the following screenshot:
![](assets/8fc27527-8e61-4480-9469-f7e67e526e76.png)
From the preceding information, we know that the fminsearch() function would minimize our objective function with a given set of initial values with a set of options. Next, we have two examples. For the first example, the objective function is given here:
![](assets/2ccf33df-35c1-4ee9-bcd6-5356f5558164.png)
The code and related output is shown here:
>> fun = @(x)50*(x(1)^2-x(2))^2 + (x(1)-3)^2;
>> x0 = [0,0];
>> x = fminsearch(fun,x0)
x
3.0000 9.0000
For the next example, its objective function is given here:
![](assets/1c46796a-e103-451a-a0ee-9b96ae9dbea3.png)
To see each step, we have the following code:
OPTIONS = optimset('Display','iter');
function f = fun2(x)
f = 0;
for k = -5:5
f = f + exp(-(x(1)-x(2))^2 - 2*x(1)^2)*cos(x(2))*sin(2*x(2));
end
endfunction
x0 = [0.5,-0.5];
[x,fval] = fminsearch(@fun2,x0,OPTIONS)
The first line is for the settings for the optimization procedure. The word Display means to display all the intermediate steps while iter means integration. The output is shown here. To save space, we show the top and bottom parts. The top part is given first:
![](assets/958e501c-c887-4b5f-bdeb-77015d74aad1.png)
Here is the bottom part:
![](assets/9766b85b-2bab-4a8a-be3e-5ef606d2ab73.png)