Facebook
From Big Macaw, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 253
  1. #include "stdafx.h"
  2. #include "iostream"
  3. #include "fstream"
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. struct Data
  9. {
  10.         double x, y;
  11. };
  12.  
  13. //Data przeslij(string a)
  14. //{
  15. //      fstream plik;
  16. //      plik.open(a, ios::in);
  17. //      while (!plik.eof())
  18. //      {
  19. //
  20. //      }
  21. //}
  22.  
  23. double interpolate(Data f[], double xi, int n)
  24. {
  25.         double result = 0;
  26.         for (int i = 0; i<n; i++)
  27.         {
  28.                 double term = f[i].y;
  29.                 for (int j = 0; j<n; j++)
  30.                 {
  31.                         if (j != i)
  32.                                 term = term*(xi - f[j].x) / double(f[i].x - f[j].x);
  33.                 }
  34.                 result += term;
  35.         }
  36.         return result;
  37. }
  38.  
  39. int main()
  40. {
  41.         Data f[] = { { -2,5 },{ -1,-2 },{ 0,4 },{ 1,-7 },{ 2,2 } };
  42.         cout << "Value of f(0) is : " << interpolate(f, -0.5, 5);
  43.         system("pause");
  44.         return 0;
  45. }
  46.