This file is indexed.

/usr/share/doc/octave-optim/optim-mini-howto-2.lyx is in octave-optim 1.0.17-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
#LyX 1.1 created this file. For more info see http://www.lyx.org/
\lyxformat 218
\textclass article
\begin_preamble
\usepackage{dsfont}

% No date please
\date{}
\fontfamily{cmss}
\selectfont
\end_preamble
\language english
\inputencoding auto
\fontscheme helvet
\graphics default
\paperfontsize default
\spacing single 
\papersize Default
\paperpackage a4
\use_geometry 0
\use_amsmath 0
\paperorientation portrait
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\defskip medskip
\quotes_language english
\quotes_times 2
\papercolumns 1
\papersides 1
\paperpagestyle default

\layout Standard


\latex latex 
% This is for 
\backslash 
LyX
\layout Standard


\begin_inset FormulaMacro 
\newcommand{\bfrac}[2]{\frac{#1 }{#2 }}

\end_inset 


\layout Standard


\begin_inset FormulaMacro 
\newcommand{\nth}{^{\textrm{th}}}

\end_inset 


\layout Standard


\begin_inset FormulaMacro 
\newcommand{\R}{R}

\end_inset 


\layout Standard


\begin_inset FormulaMacro 
\newcommand{\N}{N}

\end_inset 


\layout Standard


\begin_inset FormulaMacro 
\newcommand{\Z}{Z}

\end_inset 


\layout Standard


\begin_inset FormulaMacro 
\newcommand{\tra}{^{T}}

\end_inset 


\layout Standard


\begin_inset FormulaMacro 
\newcommand{\xx}{\mathbf{x}}

\end_inset 


\layout Standard


\latex latex 
% This is for 
\backslash 
LaTeX
\layout Standard


\latex latex 

\backslash 
fontfamily{cmss}
\backslash 
selectfont
\layout Standard


\latex latex 

\backslash 
renewcommand{
\backslash 
R}{{
\backslash 
mathds{R}}} 
\layout Standard


\latex latex 

\backslash 
renewcommand{
\backslash 
N}{{
\backslash 
mathds{N}}}
\layout Standard


\latex latex 

\backslash 
renewcommand{
\backslash 
Z}{{
\backslash 
mathds{Z}}}
\layout Standard


\latex latex 

\backslash 
renewcommand{
\backslash 
tra}{^{
\backslash 
top}}
\layout Standard


\latex latex 

\backslash 
renewcommand{
\backslash 
bfrac}[2]{
\backslash 
frac{{
\backslash 
textstyle #1 }}{{
\backslash 
textstyle #2 }}}
\layout Title

Mini-HOWTO on using Octave for Unconstrained Nonlinear Optimization
\begin_float footnote 
\layout Standard

Author : Etienne Grossmann 
\family typewriter 
<etienne@isr.ist.utl.pt>
\family default 
 (soon replaced by 
\begin_inset Quotes eld
\end_inset 

Octave-Forge developers
\begin_inset Quotes erd
\end_inset 

?).
 This document is free documentation; you can redistribute it and/or modify
 it under the terms of the GNU Free Documentation License as published by
 the Free Software Foundation.
\newline 
.\SpecialChar ~
\SpecialChar ~
\SpecialChar ~
This is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.
\end_float 
\layout Comment

Keywords: nonlinear optimization, octave, tutorial, Nelder-Mead, Conjugate
 Gradient, Levenberg-Marquardt
\layout Standard

Nonlinear optimization problems are very common and when a solution cannot
 be found analytically, one usually tries to find it numerically.
 This document shows how to perform unconstrained nonlinear minimization
 using the Octave language for numerical computation.
 We assume to be so lucky as to have an initial guess from which to start
 an iterative method, and so impatient as to avoid as much as possible going
 into the details of the algorithm.
 In the following examples, we consider multivariable problems, but the
 single variable case is solved in exactly the same way.
\layout Standard

All the algorithms used below return numerical approximations of 
\emph on 
local minima
\emph default 
 of the optimized function.
 In the following examples, we minimize a function with a single minimum
 (Figure\SpecialChar ~

\begin_inset LatexCommand \ref{fig:function}

\end_inset 

), which is relatively easily found.
 In practice, success of optimization algorithms greatly depend on the optimized
 function and on the starting point.
\layout Section*


\begin_inset ERT
collapsed true

\layout Standard


\latex latex 

\backslash 
fontfamily{cmss} 
\backslash 
selectfont 
\end_inset 

A simple example
\layout Standard

\begin_float fig 
\layout Standard
\align center 

\latex latex 

\backslash 
raisebox{6mm}{
\begin_inset Figure size 178 160
file figures/2D_slice-3.eps2
width 3 30
flags 11

\end_inset 

}\SpecialChar ~

\begin_inset Figure size 297 193
file figures/optim_tutorial_slice.eps
width 3 50
flags 9

\end_inset 


\layout Caption


\begin_inset LatexCommand \label{fig:function}

\end_inset 

 2D and 1D slices of the function that is minimized throughout this tutorial.
 Although not obvious at first sight, it has a unique minimum.
\end_float 
\layout Standard

We will use a call of the type
\layout LyX-Code

[x_best, best_value, niter] = minimize (func, x_init)
\layout Standard

to find the minimum of 
\begin_inset Formula \[
\begin{array}{cccc}
f\, : & \left( x_{1},.x_{2},x_{3}\right) \in \R ^{3} & \longrightarrow  & \left( x_{1}-1\right) ^{2}/9+\left( x_{3}-1\right) ^{2}/9+\left( x_{3}-1\right) ^{2}/9\\
 &  &  & -\cos \left( x_{1}-1\right) -\cos \left( x_{2}-1\right) -\cos \left( x_{3}-1\right) .
\end{array}\]

\end_inset 


\layout Standard

The following commands should find a local minimum of 
\begin_inset Formula \( f() \)
\end_inset 

, using the Nelder-Mead (aka 
\begin_inset Quotes eld
\end_inset 

downhill simplex
\begin_inset Quotes erd
\end_inset 

) algorithm and starting from a randomly chosen point 
\family typewriter 
x0
\family default 
\SpecialChar ~
:
\layout LyX-Code

function cost = foo (xx)
\layout LyX-Code

  xx--;  
\layout LyX-Code

  cost = sum (-cos(xx)+xx.^2/9);
\layout LyX-Code

endfunction
\layout LyX-Code

x0 = [-1, 3, -2];
\layout LyX-Code

[x,v,n] = minimize ("foo", x0)
\layout Standard

The output should look like\SpecialChar ~
:
\layout LyX-Code

x =
\layout LyX-Code

  1.00000 1.00000 1.00000
\layout LyX-Code

\layout LyX-Code

v = -3.0000
\layout LyX-Code

n = 248
\layout Standard

This means that a minimum has been found in 
\begin_inset Formula \( \left( 1,1,1\right)  \)
\end_inset 

 and that the value at that point is 
\begin_inset Formula \( -3 \)
\end_inset 

.
 This is correct, since all the points of the form 
\begin_inset Formula \( x_{1}=1+2i\pi ,\, x_{2}=1+2j\pi ,\, x_{3}=1+2k\pi  \)
\end_inset 

, for some 
\begin_inset Formula \( i,j,k\in \N  \)
\end_inset 

, minimize 
\begin_inset Formula \( f() \)
\end_inset 

.
 The number of function evaluations, 248, is also returned.
 Note that this number depends on the starting point.
 You will most likely obtain different numbers if you change 
\family typewriter 
x0
\family default 
.
\layout Standard

The Nelder-Mead algorithm is quite robust, but unfortunately it is not very
 efficient.
 For high-dimensional problems, its execution time may become prohibitive.
\layout Section*


\begin_inset ERT
collapsed true

\layout Standard


\latex latex 

\backslash 
fontfamily{cmss} 
\backslash 
selectfont 
\end_inset 

Using the first differential
\layout Standard

Fortunately, when a function, like 
\begin_inset Formula \( f() \)
\end_inset 

 above, is differentiable, more efficient optimization algorithms can be
 used.
 If 
\family typewriter 
minimize()
\family default 
 is given the differential of the optimized function, using the 
\family typewriter 
"df"
\family default 
 option, it will use a conjugate gradient method.
\layout LyX-Code

## Function returning partial derivatives
\layout LyX-Code

function dc = diffoo (x)
\layout LyX-Code

    x = x(:)' - 1;
\layout LyX-Code

    dc = sin (x) + 2*x/9;
\layout LyX-Code

endfunction
\layout LyX-Code

[x, v, n] = minimize ("foo", x0, "df", "diffoo")
\layout Standard

This produces the output\SpecialChar ~
:
\layout LyX-Code

x =
\layout LyX-Code

  1.00000 1.00000 1.00000
\layout LyX-Code

v = -3 
\layout LyX-Code

n =
\layout LyX-Code

  108 6
\layout Standard

The same minimum has been found, but only 108 function evaluations were
 needed, together with 6 evaluations of the differential.
 Here, 
\family typewriter 
diffoo()
\family default 
 takes the same argument as 
\family typewriter 
foo()
\family default 
 and returns the partial derivatives of 
\begin_inset Formula \( f() \)
\end_inset 

 with respect to the corresponding variables.
 It doesn't matter if it returns a row or column vector or a matrix, as
 long as the 
\begin_inset Formula \( i\nth  \)
\end_inset 

 element of 
\family typewriter 
diffoo(x)
\family default 
 is the partial derivative of 
\begin_inset Formula \( f() \)
\end_inset 

 with respect to 
\begin_inset Formula \( x_{i} \)
\end_inset 

 .
\layout Section*


\begin_inset ERT
collapsed true

\layout Standard


\latex latex 

\backslash 
fontfamily{cmss} 
\backslash 
selectfont 
\end_inset 

Using numerical approximations of the first differential
\layout Standard

Sometimes, the minimized function is differentiable, but actually writing
 down its differential is more work than one would like.
 Numerical differentiation offers a solution which is less efficient in
 terms of computation cost, but easy to implement.
 The 
\family typewriter 
"ndiff"
\family default 
 option of 
\family typewriter 
minimize()
\family default 
 uses numerical differentiation to execute exactly the same algorithm as
 in the previous example.
 However, because numerical approximation of the differentia is used, the
 outpud may differ slightly\SpecialChar ~
:
\layout LyX-Code

[x, v, n] = minimize ("foo", x0, "ndiff")
\layout Standard

wich yields\SpecialChar ~
:
\layout LyX-Code

x =
\layout LyX-Code

  1.00000 1.00000 1.00000
\layout LyX-Code

v = -3 
\layout LyX-Code

n =
\layout LyX-Code

  78 6
\layout Standard

Note that each time the differential is numerically approximated, 
\family typewriter 
foo()
\family default 
 is called 6 times (twice per input element), so that 
\family typewriter 
foo()
\family default 
 is evaluated a total of (78+6*6=) 114 times in this example.
\layout Section*


\begin_inset ERT
collapsed true

\layout Standard


\latex latex 

\backslash 
fontfamily{cmss} 
\backslash 
selectfont 
\end_inset 

Using the first and second differentials
\layout Standard

When the function is twice differentiable and one knows how to compute its
 first and second differentials, still more efficient algorithms can be
 used (in our case, a variant of Levenberg-Marquardt).
 The option 
\family typewriter 
"d2f"
\family default 
 allows to specify a function that returns the value of the function, the
 first and second differentials of the minimized function.
 Entering the commands\SpecialChar ~
: 
\layout LyX-Code

function [c, dc, d2c] = d2foo (x)
\layout LyX-Code

    c = foo(x);
\layout LyX-Code

    dc = diffoo(x);
\layout LyX-Code

    d2c = diag (cos (x(:)-1) + 2/9);
\layout LyX-Code

end
\layout LyX-Code

[x,v,n] = minimize ("foo", x0, "d2f", "d2foo") 
\layout Standard

produces the output\SpecialChar ~
:
\layout LyX-Code

x =
\layout LyX-Code

  1.0000 1.0000 1.0000
\layout LyX-Code

v = -3
\layout LyX-Code

n =
\layout LyX-Code

  34 5
\layout Standard

This time, 34 function evaluations, and 5 evaluations of 
\family typewriter 
d2foo()
\family default 
 were needed.
\layout Section*


\begin_inset ERT
collapsed true

\layout Standard


\latex latex 

\backslash 
fontfamily{cmss} 
\backslash 
selectfont 
\end_inset 

Summary
\layout Standard

We have just seen the most basic ways of solving nonlinear unconstrained
 optimization problems.
 The online help system of Octave (try e.g.
 
\begin_inset Quotes eld
\end_inset 


\family typewriter 
help minimize
\family default 

\begin_inset Quotes erd
\end_inset 

) will yield information on other issues, such as 
\emph on 
passing extra arguments
\emph default 
 to the minimized function, 
\emph on 
controling the termination
\emph default 
 of the optimization process, choosing the algorithm etc.
\layout LyX-Code

\the_end