Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
535 B
1
Indexable
Never
function lagrangePolynomial(xValues, yValues) {
  let n = xValues.length;
  let polynomial = "";
  
  for (let i = 0; i < n; i++) {
    let term = `${yValues[i]}`;
    for (let j = 0; j < n; j++) {
      if (i !== j) {
        let numerator = `(x - ${xValues[j]})`;
        let denominator = `(${xValues[i]} - ${xValues[j]})`;
        term = `${term} * ${numerator}/${denominator}`;
      }
    }
    polynomial = `${polynomial} + ${term}`;
  }
  
  return polynomial.substring(3); // usunięcie początkowego "+" 
}