title: Method of the golden ratio
body: <p>I need to find the maximum of the function between the specific ratio. The code below show "Method of the golden ratio", which could find the maximum of the funciton. The problem is when I use a exp() function in [0.,10.] the result is about 10, but it should be about 20k. Do you know where is the problem? Have you got some other methods to find the maximum of the function?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  using namespace std;
  double goldenRatioMethodMax(double(*p_pFunction)(double), double a, double b)
  {
    double k = (sqrt(5.) - 1.) / 2.;
    double xL = b - k * (b - a);
    double xR = a + k * (b - a);
    while (b - a &gt; EPSILON)
    {
      if (p_pFunction(xL) &gt; p_pFunction(xR))
      {
        b = xR;
        xR = xL;
        xL = b - k*(b - a);
      }
      else
      {
        a = xL;
        xL = xR;
        xR = a + k * (b - a);
      }
    }
    return (a + b) / 2.;
  }
  int main(int argc, char **argv)
  {
     cout &lt;&lt; goldenRatioMethodMax(exp, 0.,10.);//the answer is about 10 but it should be    about 20k 
     return 0;
  }
  </code></pre>