Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. //using namespace std;
  5.  
  6. double NewtonInterpolation(int NumberOfSamplingPoints, float *x, float *f, double PointToCalculate, bool check);
  7.  
  8. int main()
  9. {
  10.     int NumberOfSamplingPoints;
  11.     float *x, *f;
  12.     double PointToCalculate;
  13.     bool check;
  14.    
  15.     while (true) {
  16.         check = true;
  17.         system("cls");
  18.         std::cout << "Enter no of sample points: ";
  19.         std::cin >> NumberOfSamplingPoints;
  20.         std::cout << "Enter All x with corresponding f(x): (" << NumberOfSamplingPoints << ") " << std::endl;
  21.  
  22.         x = new float[NumberOfSamplingPoints];
  23.         f = new float[NumberOfSamplingPoints];
  24.         for (int i = 0; i < NumberOfSamplingPoints; i++)
  25.             std::cin >> x[i] >> f[i];
  26.         //std::cout << "Enter x for calculation: ";
  27.         //std::cin >> PointToCalculate;
  28.         for (double i = 0; i < 12.5; i++) {
  29.             PointToCalculate = i / 2;
  30.             std::cout << "The result for x = " << PointToCalculate << " is: " << NewtonInterpolation(NumberOfSamplingPoints, x, f, PointToCalculate, check) << std::endl;
  31.             check = false;
  32.         }
  33.         system("pause");
  34.     }
  35.     std::cin.get();
  36.     std::cin.get();
  37.     delete[] x, f;
  38.     return 0;
  39. }
  40.  
  41. double NewtonInterpolation(int NumberOfSamplingPoints, float *x, float *f, double PointToCalculate, bool check) {
  42.     int i, j;
  43.     double sum = 0, mult;
  44.     if (check == true) {
  45.         std::cout << "Ilorazy: " << std::endl;
  46.         for (j = 0; j < NumberOfSamplingPoints - 1; j++)
  47.         {
  48.             std::cout << "------" << j+1 << "------" << std::endl;
  49.             for (i = NumberOfSamplingPoints - 1; i > j; i--) {
  50.                 f[i] = (f[i] - f[i - 1]) / (x[i] - x[i - j - 1]);
  51.                 std::cout << "(" << f[i] << ")" << std::endl;
  52.             }
  53.         }
  54.     }
  55.     for (i = NumberOfSamplingPoints - 1; i >= 0; i--)
  56.     {
  57.         mult = 1;
  58.         for (j = 0; i>j; j++)
  59.             mult *= (PointToCalculate - x[j]);
  60.  
  61.         mult *= f[j];
  62.         sum += mult;
  63.     }
  64.     return sum;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement