주어진 데이터 포인트의 개별 세트 범위 내에서 새로운 데이터 포인트를 구성하기 위해 보간 기술이 사용됩니다. 라그랑주 보간 기법이 그 중 하나입니다. 주어진 데이터 포인트가 고르게 분포되지 않은 경우 이 보간 방법을 사용하여 솔루션을 찾을 수 있습니다. Lagrange 보간법의 경우 이 방정식을 따라야 합니다.

입력 및 출력
Input:
List of x and f(x) values. find f(3.25)
x: {0,1,2,3,4,5,6}
f(x): {0,1,8,27,64,125,216}
Output:
Result after Lagrange interpolation f(3.25) = 34.3281 알고리즘
largrangeInterpolation(x: array, fx: array, x1)
입력 - 이전에 알려진 데이터를 가져오기 위한 x 배열 및 fx 배열 및 포인트 x1.
출력: f(x1)의 값입니다.
Begin res := 0 and tempSum := 0 for i := 1 to n, do tempProd := 1 for j := 1 to n, do if i ≠ j, then tempProf := tempProd * (x1 – x[j])/(x[i] – x[j]) done tempPord := tempProd * fx[i] res := res + tempProd done return res End
예
#include<iostream>
#define N 6
using namespace std;
double lagrange(double x[], double fx[], double x1) {
double res = 0, tempSum = 0;
for(int i = 1; i<=N; i++) {
double tempProd = 1; //for each iteration initialize temp product
for(int j = 1; j<=N; j++) {
if(i != j) { //if i = j, then denominator will be 0
tempProd *= (x1 - x[j])/(x[i] - x[j]); //multiply each term using formula
}
}
tempProd *= fx[i]; //multiply f(xi)
res += tempProd;
}
return res;
}
main() {
double x[N+1] = {0,1,2,3,4,5,6};
double y[N+1] = {0,1,8,27,64,125,216};
double x1 = 3.25;
cout << "Result after lagrange interpolation f("<<x1<<") = " << lagrange(x, y, x1);
} 출력
Result after lagrange interpolation f(3.25) = 34.3281