이 튜토리얼에서는 라그랑주 보간 공식의 결과를 찾는 프로그램을 작성할 것입니다.
프로그램에 대한 로직을 작성하지 않았습니다. 수식을 코드로 변환하기만 하면 됩니다. 코드를 봅시다.
예시
#include<bits/stdc++.h>
using namespace std;
struct Data {
int x, y;
};
double interpolate(Data function[], int xi, int n) {
double result = 0;
for (int i = 0; i < n; i++) {
double term = function[i].y;
for (int j = 0; j < n; j++) {
if (j != i) {
term = term * (xi - function[j].x) / double(function[i].x - function[j].x);
}
}
result += term;
}
return result;
}
int main() {
Data function[] = {{0,3}, {1,2}, {6,9}, {10,17}};
cout << interpolate(function, 3, 5) << endl;
return 0;
} 출력
위의 코드를 실행하면 다음과 같은 결과를 얻을 수 있습니다.
3
결론
튜토리얼에서 질문이 있는 경우 댓글 섹션에 언급하세요.