Advertisement
shadeyourself

interpolation

Oct 11th, 2021
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4.  
  5. double f(double x) {
  6.     return sin(x) / x;
  7. }
  8. double L(double *x, int n, double a) {
  9.     double rez = 0, l;
  10.     int i, j;
  11.     for (i = 0; i < n; i++) {
  12.         l = 1;
  13.         for (j = 0; j < n; j++) {
  14.             if (i != j) {
  15.                 l = l * (a - x[j]);
  16.                 l = l / (x[i] - x[j]);
  17.             }
  18.         }
  19.         rez = rez + f(x[i]) * l;
  20.     }
  21.     return rez;
  22. }
  23.  
  24. int main() {
  25.     double *x, tochka;
  26.     int n;
  27.     printf("enter n (kolichestvo peremennih v kotorih izvestno znachenie): ");
  28.     scanf("%d", &n);
  29.     x = (double*)malloc(sizeof(double) * n);
  30.     if (x == NULL) {
  31.         printf("malloc x error\n");
  32.         return -1;
  33.     }
  34.     for (int i = 0; i < n; i++) {
  35.         printf("%d\nenter xi :", i);
  36.         scanf("%lf", &x[i]);
  37.     }
  38.     printf("enter x v kotorom hotim y: ");
  39.     scanf("%lf", &tochka);
  40.     printf("lagranj: %lf\n", L(x, n, tochka));
  41.     printf("the real value in tochka: %lf\n", f(tochka));
  42.     free(x);
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement