/tags/cvs_final/octave-forge/main/optim/inst/fminbnd.m

# · MATLAB · 70 lines · 66 code · 4 blank · 0 comment · 4 complexity · 7e35d9bc4540bb3c58c841c70c4321b8 MD5 · raw file

  1. ## Copyright (C) 2000 Ben Sapp. All rights reserved.
  2. ## Modification by Andreas Helms
  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. ## -*- texinfo -*-
  13. ## @deftypefn {Function File} {[@var{x},@var{v}] =} fminbnd(@var{f},@var{lb},@var{ub},@var{[options]},@var{P1},@var{P2}, ...)
  14. ##
  15. ## Find the minimizer @var{x} of a scalar function and the corresponding
  16. ## value @var{v} 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 options
  27. ## Vector with control parameters (For compatibily with MATLAB, not used
  28. ## here)
  29. ## @item P1,P2, ...
  30. ## Optional parameter for function @var{f}
  31. ##
  32. ## @end table
  33. ## @end deftypefn
  34. ## 2001-09-24 Andreas Helms <helms@astro.physik.uni-potsdam.de>
  35. ## * modified for use with functions of more than one parameter
  36. ## 2007-08-09 Marco Caliari <mcaliari@math.unipd.it>
  37. ## * modified in order to get optionally the value of the function
  38. function [min,val] = fminbnd(_func,lb,ub, options, varargin)
  39. delta = 1e-17;
  40. gr = (sqrt(5)-1)/2;
  41. width = (ub-lb);
  42. out = [ lb:(width/3):ub ];
  43. out(2) = out(4)-gr*width;
  44. out(3) = out(1)+gr*width;
  45. upper = feval(_func,out(3), varargin{:});
  46. lower = feval(_func,out(2), varargin{:});
  47. while((out(3)-out(2)) > delta) #this will not work for symmetric funcs
  48. if(upper > lower)
  49. out(4) = out(3);
  50. out(3) = out(2);
  51. width = out(4)-out(1);
  52. out(2) = out(4)-gr*width;
  53. upper = lower;
  54. lower = feval(_func,out(2), varargin{:});
  55. else
  56. out(1) = out(2);
  57. out(2) = out(3);
  58. width = out(4)-out(1);
  59. out(3) = out(1)+width*gr;
  60. lower = upper;
  61. upper = feval(_func,out(3), varargin{:});
  62. endif
  63. endwhile
  64. min = out(2);
  65. val = feval(_func,out(2), varargin{:});
  66. endfunction