Mercurial > forge
comparison main/optim/fminbnd.m @ 0:6b33357c7561 octave-forge
Initial revision
author | pkienzle |
---|---|
date | Wed, 10 Oct 2001 19:54:49 +0000 |
parents | |
children | 997ba9e81037 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:6b33357c7561 |
---|---|
1 ## Copyright (C) 2000 Ben Sapp. All rights reserved. | |
2 ## | |
3 ## This program is free software; you can redistribute it and/or modify it | |
4 ## under the terms of the GNU General Public License as published by the | |
5 ## Free Software Foundation; either version 2, or (at your option) any | |
6 ## later version. | |
7 ## | |
8 ## This is distributed in the hope that it will be useful, but WITHOUT | |
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
11 ## for more details. | |
12 | |
13 ## -*- texinfo -*- | |
14 ## @deftypefn {Function File} {[@var{x}] =} fminbnd(@var{f},@var{lb},@var{ub},[@var{tol}]) | |
15 ## | |
16 ## Find the minimum of a scalar function with the Golden Search method. | |
17 ## | |
18 ## @strong{Inputs} | |
19 ## @table @var | |
20 ## @item f | |
21 ## A string contining the name of the function to minimiz | |
22 ## @item lb | |
23 ## Value to use as an initial lower bound on @var{x}. | |
24 ## @item ub | |
25 ## Value to use as an initial upper bound on @var{x}. | |
26 ## @item tol | |
27 ## Tolerence you would like to have. The default value is @var{tol} = | |
28 ## 10e-6 | |
29 ## @end table | |
30 ## @end deftypefn | |
31 | |
32 function min = fminbnd(_func,lb,ub) | |
33 delta = 1e-17; | |
34 gr = (sqrt(5)-1)/2; | |
35 width = (ub-lb); | |
36 out = lb:(width/3):ub; | |
37 out(2) = out(4)-gr*width; | |
38 out(3) = out(1)+gr*width; | |
39 upper = feval(_func,out(3)); | |
40 lower = feval(_func,out(2)); | |
41 while((out(3)-out(2)) > delta) #this will not work for symetric funcs | |
42 if(upper > lower) | |
43 out(4) = out(3); | |
44 out(3) = out(2); | |
45 width = out(4)-out(1); | |
46 out(2) = out(4)-gr*width; | |
47 upper = lower; | |
48 lower = feval(_func,out(2)); | |
49 else | |
50 out(1) = out(2); | |
51 out(2) = out(3); | |
52 width = out(4)-out(1); | |
53 out(3) = out(1)+width*gr; | |
54 lower = upper; | |
55 upper = feval(_func,out(3)); | |
56 endif | |
57 endwhile | |
58 min = out(2); | |
59 endfunction |