Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
464 B
1
Indexable
Never
function interpolacja(x, y) {
  let n = x.length;
  let A = new Array(n).fill(0).map(() => new Array(n).fill(0));
  for (let i = 0; i < n; i++) {
    A[i][0] = 1;
    for (let j = 1; j < n; j++) {
      A[i][j] = A[i][j-1] * x[i];
    }
  }
  let c = numeric.solve(A, y);
  c = c.reverse();
  let p_str = `${c[0].toFixed(2)} ∙ x^${n-1}`;
  for (let i = 1; i < n; i++) {
    p_str += ` + ${c[i].toFixed(2)} ∙ x^${n-i-1}`;
  }
  return p_str;
}