File: gsl-ref_33.html

package info (click to toggle)
gsl-ref-html 1.6-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,504 kB
  • ctags: 3,558
  • sloc: makefile: 36
file content (589 lines) | stat: -rw-r--r-- 17,888 bytes parent folder | download
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
<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.54+ (gsl)
     from ../gsl-ref.texi -->

<TITLE>GNU Scientific Library -- Reference Manual - One dimensional Minimization</TITLE>
<!-- <LINK rel="stylesheet" title="Default Style Sheet" href="/css/texinfo.css" type="text/css"> -->
<link href="gsl-ref_34.html" rel=Next>
<link href="gsl-ref_32.html" rel=Previous>
<link href="gsl-ref_toc.html" rel=ToC>

</HEAD>
<BODY>
<p>Go to the <A HREF="gsl-ref_1.html">first</A>, <A HREF="gsl-ref_32.html">previous</A>, <A HREF="gsl-ref_34.html">next</A>, <A HREF="gsl-ref_50.html">last</A> section, <A HREF="gsl-ref_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC440" HREF="gsl-ref_toc.html#TOC440">One dimensional Minimization</A></H1>
<P>
<A NAME="IDX2113"></A>
<A NAME="IDX2114"></A>
<A NAME="IDX2115"></A>
<A NAME="IDX2116"></A>
<A NAME="IDX2117"></A>

</P>
<P>
This chapter describes routines for finding minima of arbitrary
one-dimensional functions.  The library provides low level components
for a variety of iterative minimizers and convergence tests.  These can be
combined by the user to achieve the desired solution, with full access
to the intermediate steps of the algorithms.  Each class of methods uses
the same framework, so that you can switch between minimizers at runtime
without needing to recompile your program.  Each instance of a minimizer
keeps track of its own state, allowing the minimizers to be used in
multi-threaded programs.

</P>
<P>
The header file <TT>'gsl_min.h'</TT> contains prototypes for the
minimization functions and related declarations.  To use the minimization
algorithms to find the maximum of a function simply invert its sign.

</P>



<H2><A NAME="SEC441" HREF="gsl-ref_toc.html#TOC441">Overview</A></H2>
<P>
<A NAME="IDX2118"></A>

</P>
<P>
The minimization algorithms begin with a bounded region known to contain
a minimum.  The region is described by a lower bound a and an
upper bound b, with an estimate of the location of the minimum
x.

</P>

<P>
The value of the function at x must be less than the value of the
function at the ends of the interval,

</P>

<PRE class="example">
f(a) &#62; f(x) &#60; f(b)
</PRE>

<P>
This condition guarantees that a minimum is contained somewhere within
the interval.  On each iteration a new point x' is selected using
one of the available algorithms.  If the new point is a better estimate
of the minimum, f(x') &#60; f(x), then the current estimate of the
minimum x is updated.  The new point also allows the size of the
bounded interval to be reduced, by choosing the most compact set of
points which satisfies the constraint f(a) &#62; f(x) &#60; f(b).  The
interval is reduced until it encloses the true minimum to a desired
tolerance.  This provides a best estimate of the location of the minimum
and a rigorous error estimate.

</P>
<P>
Several bracketing algorithms are available within a single framework.
The user provides a high-level driver for the algorithm, and the
library provides the individual functions necessary for each of the
steps.  There are three main phases of the iteration.  The steps are,

</P>

<UL class="itemize">
<LI>

initialize minimizer state, <VAR>s</VAR>, for algorithm <VAR>T</VAR>

<LI>

update <VAR>s</VAR> using the iteration <VAR>T</VAR>

<LI>

test <VAR>s</VAR> for convergence, and repeat iteration if necessary
</UL>

<P>
The state for the minimizers is held in a <CODE>gsl_min_fminimizer</CODE>
struct.  The updating procedure uses only function evaluations (not
derivatives).

</P>


<H2><A NAME="SEC442" HREF="gsl-ref_toc.html#TOC442">Caveats</A></H2>
<P>
<A NAME="IDX2119"></A>

</P>
<P>
Note that minimization functions can only search for one minimum at a
time.  When there are several minima in the search area, the first
minimum to be found will be returned; however it is difficult to predict
which of the minima this will be. <EM>In most cases, no error will be
reported if you try to find a minimum in an area where there is more
than one.</EM>

</P>
<P>
With all minimization algorithms it can be difficult to determine the
location of the minimum to full numerical precision.  The behavior of the
function in the region of the minimum x^* can be approximated by
a Taylor expansion,

</P>

<PRE class="example">
y = f(x^*) + (1/2) f"(x^*) (x - x^*)^2
</PRE>

<P>
and the second term of this expansion can be lost when added to the
first term at finite precision.  This magnifies the error in locating
x^*, making it proportional to \sqrt \epsilon (where
\epsilon is the relative accuracy of the floating point numbers).
For functions with higher order minima, such as x^4, the
magnification of the error is correspondingly worse.  The best that can
be achieved is to converge to the limit of numerical accuracy in the
function values, rather than the location of the minimum itself.

</P>


<H2><A NAME="SEC443" HREF="gsl-ref_toc.html#TOC443">Initializing the Minimizer</A></H2>

<P>
<DL>
<DT><U>Function:</U> gsl_min_fminimizer * <B>gsl_min_fminimizer_alloc</B> <I>(const gsl_min_fminimizer_type * <VAR>T</VAR>)</I>
<DD><A NAME="IDX2120"></A>
This function returns a pointer to a newly allocated instance of a
minimizer of type <VAR>T</VAR>.  For example, the following code
creates an instance of a golden section minimizer,

</P>

<PRE class="example">
const gsl_min_fminimizer_type * T 
  = gsl_min_fminimizer_goldensection;
gsl_min_fminimizer * s 
  = gsl_min_fminimizer_alloc (T);
</PRE>

<P>
If there is insufficient memory to create the minimizer then the function
returns a null pointer and the error handler is invoked with an error
code of <CODE>GSL_ENOMEM</CODE>.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> int <B>gsl_min_fminimizer_set</B> <I>(gsl_min_fminimizer * <VAR>s</VAR>, gsl_function * <VAR>f</VAR>, double <VAR>x_minimum</VAR>, double <VAR>x_lower</VAR>, double <VAR>x_upper</VAR>)</I>
<DD><A NAME="IDX2121"></A>
This function sets, or resets, an existing minimizer <VAR>s</VAR> to use the
function <VAR>f</VAR> and the initial search interval [<VAR>x_lower</VAR>,
<VAR>x_upper</VAR>], with a guess for the location of the minimum
<VAR>x_minimum</VAR>.

</P>
<P>
If the interval given does not contain a minimum, then the function
returns an error code of <CODE>GSL_FAILURE</CODE>.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> int <B>gsl_min_fminimizer_set_with_values</B> <I>(gsl_min_fminimizer * <VAR>s</VAR>, gsl_function * <VAR>f</VAR>, double <VAR>x_minimum</VAR>, double <VAR>f_minimum</VAR>, double <VAR>x_lower</VAR>, double <VAR>f_lower</VAR>, double <VAR>x_upper</VAR>, double <VAR>f_upper</VAR>)</I>
<DD><A NAME="IDX2122"></A>
This function is equivalent to <CODE>gsl_min_fminimizer_set</CODE> but uses
the values <VAR>f_minimum</VAR>, <VAR>f_lower</VAR> and <VAR>f_upper</VAR> instead of
computing <CODE>f(x_minimum)</CODE>, <CODE>f(x_lower)</CODE> and <CODE>f(x_upper)</CODE>.
</DL>

</P>

<P>
<DL>
<DT><U>Function:</U> void <B>gsl_min_fminimizer_free</B> <I>(gsl_min_fminimizer * <VAR>s</VAR>)</I>
<DD><A NAME="IDX2123"></A>
This function frees all the memory associated with the minimizer
<VAR>s</VAR>.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> const char * <B>gsl_min_fminimizer_name</B> <I>(const gsl_min_fminimizer * <VAR>s</VAR>)</I>
<DD><A NAME="IDX2124"></A>
This function returns a pointer to the name of the minimizer.  For example,

</P>

<PRE class="example">
printf ("s is a '%s' minimizer\n",
        gsl_min_fminimizer_name (s));
</PRE>

<P>
would print something like <CODE>s is a 'brent' minimizer</CODE>.
</DL>

</P>


<H2><A NAME="SEC444" HREF="gsl-ref_toc.html#TOC444">Providing the function to minimize</A></H2>
<P>
<A NAME="IDX2125"></A>

</P>
<P>
You must provide a continuous function of one variable for the
minimizers to operate on.  In order to allow for general parameters the
functions are defined by a <CODE>gsl_function</CODE> data type
(see section <A HREF="gsl-ref_32.html#SEC432">Providing the function to solve</A>).

</P>


<H2><A NAME="SEC445" HREF="gsl-ref_toc.html#TOC445">Iteration</A></H2>

<P>
The following functions drive the iteration of each algorithm.  Each
function performs one iteration to update the state of any minimizer of the
corresponding type.  The same functions work for all minimizers so that
different methods can be substituted at runtime without modifications to
the code.

</P>
<P>
<DL>
<DT><U>Function:</U> int <B>gsl_min_fminimizer_iterate</B> <I>(gsl_min_fminimizer * <VAR>s</VAR>)</I>
<DD><A NAME="IDX2126"></A>
This function performs a single iteration of the minimizer <VAR>s</VAR>.  If the
iteration encounters an unexpected problem then an error code will be
returned,

</P>
<DL COMPACT>

<DT><CODE>GSL_EBADFUNC</CODE>
<DD>
the iteration encountered a singular point where the function evaluated
to <CODE>Inf</CODE> or <CODE>NaN</CODE>.

<DT><CODE>GSL_FAILURE</CODE>
<DD>
the algorithm could not improve the current best approximation or
bounding interval.
</DL>
</DL>

<P>
The minimizer maintains a current best estimate of the position of the
minimum at all times, and the current interval bounding the minimum.
This information can be accessed with the following auxiliary functions,

</P>
<P>
<DL>
<DT><U>Function:</U> double <B>gsl_min_fminimizer_x_minimum</B> <I>(const gsl_min_fminimizer * <VAR>s</VAR>)</I>
<DD><A NAME="IDX2127"></A>
This function returns the current estimate of the position of the
minimum for the minimizer <VAR>s</VAR>.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> double <B>gsl_min_fminimizer_x_upper</B> <I>(const gsl_min_fminimizer * <VAR>s</VAR>)</I>
<DD><A NAME="IDX2128"></A>
<DT><U>Function:</U> double <B>gsl_min_fminimizer_x_lower</B> <I>(const gsl_min_fminimizer * <VAR>s</VAR>)</I>
<DD><A NAME="IDX2129"></A>
These functions return the current upper and lower bound of the interval
for the minimizer <VAR>s</VAR>.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> double <B>gsl_min_fminimizer_f_minimum</B> <I>(const gsl_min_fminimizer *<VAR>s</VAR>)</I>
<DD><A NAME="IDX2130"></A>
<DT><U>Function:</U> double <B>gsl_min_fminimizer_f_upper</B> <I>(const gsl_min_fminimizer *<VAR>s</VAR>)</I>
<DD><A NAME="IDX2131"></A>
<DT><U>Function:</U> double <B>gsl_min_fminimizer_f_lower</B> <I>(const gsl_min_fminimizer *<VAR>s</VAR>)</I>
<DD><A NAME="IDX2132"></A>
These functions return the value of the function at the current estimate
of the minimum and at the upper and lower bounds of interval for the
minimizer <VAR>s</VAR>.
</DL>

</P>


<H2><A NAME="SEC446" HREF="gsl-ref_toc.html#TOC446">Stopping Parameters</A></H2>
<P>
<A NAME="IDX2133"></A>

</P>
<P>
A minimization procedure should stop when one of the following
conditions is true:

</P>

<UL class="itemize">
<LI>

A minimum has been found to within the user-specified precision.

<LI>

A user-specified maximum number of iterations has been reached.

<LI>

An error has occurred.
</UL>

<P>
The handling of these conditions is under user control.  The function
below allows the user to test the precision of the current result.

</P>
<P>
<DL>
<DT><U>Function:</U> int <B>gsl_min_test_interval</B> <I>(double <VAR>x_lower</VAR>, double <VAR>x_upper</VAR>, double <VAR>epsabs</VAR>, double <VAR>epsrel</VAR>)</I>
<DD><A NAME="IDX2134"></A>
This function tests for the convergence of the interval [<VAR>x_lower</VAR>,
<VAR>x_upper</VAR>] with absolute error <VAR>epsabs</VAR> and relative error
<VAR>epsrel</VAR>.  The test returns <CODE>GSL_SUCCESS</CODE> if the following
condition is achieved,

</P>

<PRE class="example">
|a - b| &#60; epsabs + epsrel min(|a|,|b|) 
</PRE>

<P>
when the interval x = [a,b] does not include the origin.  If the
interval includes the origin then \min(|a|,|b|) is replaced by
zero (which is the minimum value of |x| over the interval).  This
ensures that the relative error is accurately estimated for minima close
to the origin.

</P>
<P>
This condition on the interval also implies that any estimate of the
minimum x_m in the interval satisfies the same condition with respect
to the true minimum x_m^*,

</P>

<PRE class="example">
|x_m - x_m^*| &#60; epsabs + epsrel x_m^*
</PRE>

<P>
assuming that the true minimum x_m^* is contained within the interval.
</DL>

</P>



<H2><A NAME="SEC447" HREF="gsl-ref_toc.html#TOC447">Minimization Algorithms</A></H2>

<P>
The minimization algorithms described in this section require an initial
interval which is guaranteed to contain a minimum -- if a and
b are the endpoints of the interval and x is an estimate
of the minimum then f(a) &#62; f(x) &#60; f(b).  This ensures that the
function has at least one minimum somewhere in the interval.  If a valid
initial interval is used then these algorithm cannot fail, provided the
function is well-behaved.

</P>
<P>
<DL>
<DT><U>Minimizer:</U> <B>gsl_min_fminimizer_goldensection</B>
<DD><A NAME="IDX2135"></A>

</P>
<P>
<A NAME="IDX2136"></A>
<A NAME="IDX2137"></A>

</P>
<P>
The <I>golden section algorithm</I> is the simplest method of bracketing
the minimum of a function.  It is the slowest algorithm provided by the
library, with linear convergence.

</P>
<P>
On each iteration, the algorithm first compares the subintervals from
the endpoints to the current minimum.  The larger subinterval is divided
in a golden section (using the famous ratio (3-\sqrt 5)/2 =
0.3189660...) and the value of the function at this new point is
calculated.  The new value is used with the constraint f(a') &#62;
f(x') &#60; f(b') to a select new interval containing the minimum, by
discarding the least useful point.  This procedure can be continued
indefinitely until the interval is sufficiently small.  Choosing the
golden section as the bisection ratio can be shown to provide the
fastest convergence for this type of algorithm.

</P>
</DL>

<P>
<DL>
<DT><U>Minimizer:</U> <B>gsl_min_fminimizer_brent</B>
<DD><A NAME="IDX2138"></A>
<A NAME="IDX2139"></A>
<A NAME="IDX2140"></A>

</P>
<P>
The <I>Brent minimization algorithm</I> combines a parabolic
interpolation with the golden section algorithm.  This produces a fast
algorithm which is still robust.

</P>
<P>
The outline of the algorithm can be summarized as follows: on each
iteration Brent's method approximates the function using an
interpolating parabola through three existing points.  The minimum of the
parabola is taken as a guess for the minimum.  If it lies within the
bounds of the current interval then the interpolating point is accepted,
and used to generate a smaller interval.  If the interpolating point is
not accepted then the algorithm falls back to an ordinary golden section
step.  The full details of Brent's method include some additional checks
to improve convergence.
</DL>

</P>



<H2><A NAME="SEC448" HREF="gsl-ref_toc.html#TOC448">Examples</A></H2>

<P>
The following program uses the Brent algorithm to find the minimum of
the function f(x) = \cos(x) + 1, which occurs at x = \pi.
The starting interval is (0,6), with an initial guess for the
minimum of 2.

</P>

<PRE class="example">
#include &#60;stdio.h&#62;
#include &#60;gsl/gsl_errno.h&#62;
#include &#60;gsl/gsl_math.h&#62;
#include &#60;gsl/gsl_min.h&#62;

double fn1 (double x, void * params)
{
  return cos(x) + 1.0;
}

int
main (void)
{
  int status;
  int iter = 0, max_iter = 100;
  const gsl_min_fminimizer_type *T;
  gsl_min_fminimizer *s;
  double m = 2.0, m_expected = M_PI;
  double a = 0.0, b = 6.0;
  gsl_function F;

  F.function = &#38;fn1;
  F.params = 0;

  T = gsl_min_fminimizer_brent;
  s = gsl_min_fminimizer_alloc (T);
  gsl_min_fminimizer_set (s, &#38;F, m, a, b);

  printf ("using %s method\n",
          gsl_min_fminimizer_name (s));

  printf ("%5s [%9s, %9s] %9s %10s %9s\n",
          "iter", "lower", "upper", "min",
          "err", "err(est)");

  printf ("%5d [%.7f, %.7f] %.7f %+.7f %.7f\n",
          iter, a, b,
          m, m - m_expected, b - a);

  do
    {
      iter++;
      status = gsl_min_fminimizer_iterate (s);

      m = gsl_min_fminimizer_x_minimum (s);
      a = gsl_min_fminimizer_x_lower (s);
      b = gsl_min_fminimizer_x_upper (s);

      status 
        = gsl_min_test_interval (a, b, 0.001, 0.0);

      if (status == GSL_SUCCESS)
        printf ("Converged:\n");

      printf ("%5d [%.7f, %.7f] "
              "%.7f %.7f %+.7f %.7f\n",
              iter, a, b,
              m, m_expected, m - m_expected, b - a);
    }
  while (status == GSL_CONTINUE &#38;&#38; iter &#60; max_iter);

  return status;
}
</PRE>

<P>
Here are the results of the minimization procedure.

</P>

<PRE class="smallexample">
bash$ ./a.out 
    0 [0.0000000, 6.0000000] 2.0000000 -1.1415927 6.0000000
    1 [2.0000000, 6.0000000] 3.2758640 +0.1342713 4.0000000
    2 [2.0000000, 3.2831929] 3.2758640 +0.1342713 1.2831929
    3 [2.8689068, 3.2831929] 3.2758640 +0.1342713 0.4142862
    4 [2.8689068, 3.2831929] 3.2758640 +0.1342713 0.4142862
    5 [2.8689068, 3.2758640] 3.1460585 +0.0044658 0.4069572
    6 [3.1346075, 3.2758640] 3.1460585 +0.0044658 0.1412565
    7 [3.1346075, 3.1874620] 3.1460585 +0.0044658 0.0528545
    8 [3.1346075, 3.1460585] 3.1460585 +0.0044658 0.0114510
    9 [3.1346075, 3.1460585] 3.1424060 +0.0008133 0.0114510
   10 [3.1346075, 3.1424060] 3.1415885 -0.0000041 0.0077985
Converged:                            
   11 [3.1415885, 3.1424060] 3.1415927 -0.0000000 0.0008175
</PRE>



<H2><A NAME="SEC449" HREF="gsl-ref_toc.html#TOC449">References and Further Reading</A></H2>

<P>
Further information on Brent's algorithm is available in the following
book,

</P>

<UL class="itemize">
<LI>

Richard Brent, <CITE>Algorithms for minimization without derivatives</CITE>,
Prentice-Hall (1973), republished by Dover in paperback (2002), ISBN
0-486-41998-3.
</UL>

<P><HR><P>
<p>Go to the <A HREF="gsl-ref_1.html">first</A>, <A HREF="gsl-ref_32.html">previous</A>, <A HREF="gsl-ref_34.html">next</A>, <A HREF="gsl-ref_50.html">last</A> section, <A HREF="gsl-ref_toc.html">table of contents</A>.
</BODY>
</HTML>