Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cassert>
- #include <cmath>
- using namespace std;
- double lagrangeInterp(double x[], double y[], int n, double xL) {
- double sum = 0, mult;
- for (int i = 0; i < n; i++) {
- mult = 1;
- for (int j = 0; j < n; j++) {
- if (i != j) {
- assert(fabs(x[i] - x[j]) > 1e-10 && "Сannot be divided by zero!");
- mult *= (xL - x[j]) / (x[i] - x[j]);
- }
- }
- sum += mult * y[i];
- }
- return sum;
- }
- int main() {
- int n;
- cout << "Enter count of points: ";
- cin >> n;
- double x[n], y[n], xL;
- cout << "Enter x array: ";
- for (int i = 0; i < n; i++) {
- cin >> x[i];
- }
- cout << "Enter y array: ";
- for (int i = 0; i < n; i++) {
- cin >> y[i];
- }
- cout << "Enter x to find f(x): ";
- cin >> xL;
- cout << "f(" << xL << ") = " << lagrangeInterp(x, y, n, xL) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement