title: How to calculate coefficients of polynomial using Lagrange interpolation
body: <p>I need to calculate coefficients of polynomial using Lagrange <a href="http://en.wikipedia.org/wiki/Polynomial_interpolation" rel="noreferrer">interpolation polynomial</a>, as my homework, I decide to do this in Javascript.</p>
  
  <p>here is definition of Lagrange polynomial (L(x))</p>
  
  <p><img src="https://upload.wikimedia.org/math/f/b/f/fbf1a1a198c3f3c6dc043ef8e18f8835.png" alt="enter image description here"></p>
  
  <p>Lagrange basis polynomials are defined as follows</p>
  
  <p><img src="https://upload.wikimedia.org/math/1/e/4/1e439bae00050b2e5a6045e9b3677072.png" alt="enter image description here"></p>
  
  <p>Calculate y value for specific X (W(x) function) is simple but I need to calculate coefficients of polynomial (array of [a0, a1, ..., an]) I need to do this to n&lt;=10 but it will be nice to have arbitrary n, then I can put that function into horner function and draw that polynomial.</p>
  
  <p><img src="https://i.stack.imgur.com/dvdv4.png" alt="enter image description here"></p>
  
  <p>I have function that calculate denominator in first equation</p>
  
  <pre><code>function denominator(i, points) {
     var result = 1;
     var x_i = points[i].x;
     for (var j=points.length; j--;) {
        if (i != j) {
          result *= x_i - points[j].x;
        }
     }
     return result;
  }
  </code></pre>
  
  <p>and function that return y using horner method (I also have drawing function using canvas)</p>
  
  <pre><code>function horner(array, x_scale, y_scale) {
     function recur(x, i, array) {
        if (i == 0) {
           return x*array[0];
        } else {
           return array[i] + x*recur(x, --i, array);
        }
     }
     return function(x) {
        return recur(x*x_scale, array.length-1, array)*y_scale;
     };
  }
  </code></pre>
  
  <p>anybody know algorithm to do this, or idea how to calculate those coefficients</p>