title: JS exponential curve fit
body: <p>I'm trying to make a simple function to do an exponential curve fit given a dataset.  I've written the following:</p>
  
  <pre><code>  function expCurveFit(d){
      var sum_x2 = 0, sum_lny = 0, sum_x = 0, sum_xlny = 0, n = d.length;
      for(var i=0;i&lt;d.length;i++){
        var x = d[i][0];
        var y = d[i][1];
        sum_x2 += x^2, sum_lny += Math.log(y), sum_x += x, sum_xlny += x*Math.log(y);
      }
      var a = ((sum_lny*sum_x2)-(sum_x*sum_xlny))/((n*sum_x2)-sum_x^2);
      var b = ((n*sum_xlny)-(sum_x*sum_lny))/((n*sum_x2)-sum_x^2);
      return [a,b];
    }
  </code></pre>
  
  <p>but a and b don't seem to be correct.  I'm working from the basic formula at <a href="http://mathworld.wolfram.com/LeastSquaresFittingExponential.html" rel="nofollow">Wolfram</a>.  The inputted array is in the form <code>[[x1,y1],[x2,y2]...[xn,yn]]</code>.  Anyone have working suggestions, plug-ins, libraries, or revised functions?</p>