c++ - Method of Divided Differences -


i have problem in method of divided differences code. 2 problems follows:

  1. it working correctly if enter 1 or 2 data points, if enter 3 or more data points result wrong
  2. i want form of output :

    how many data points entered?: 5 enter x00: 1.0 enter y00: 0.7651977 enter x01: 1.3 enter y01: 0.6200860 enter x02: 1.6 enter y02: 0.4554022 enter x03: 1.9 enter y03: 0.2818186 enter x04: 2.2 enter y04: 0.1103623 interpolating polynomial is: p(x) =     0.7651977   - 0.4837057(x - 1.0)   - 0.1087339(x - 1.0)(x - 1.3)   + 0.0658784(x - 1.0)(x - 1.3)(x - 1.6)   + 0.0018251(x - 1.0)(x - 1.3)(x - 1.6)(x - 1.9)  press key continue... 

here code:

#include <iostream> #include <vector> #include <stdexcept> using namespace std;  double func(const std::vector< double > & a, const std::vector< double > & b, int i, int j){     if ((i < 0) || (j < 0) || (i >= a.size()) || (j >= b.size()) || (i < j)) {         return 0; // ignore invalid arguments.     }     else if (i == j){         return b[i];     }     else if (a[i] == a[j]) {         return 0; // avoid division 0.     }     else if (i - j == 1){         return (b[i] - b[j]) / (a[i] - a[j]);     }     else         return (func(a, b, i, j - 1) - func(a, b, - 1, j)) / (a[i] - a[j]); }  int main() {     int x;      cout << "how many data points entered?:";     cin >> x;     std::vector< double > a;     std::vector< double > b;     (int c = 0; c < x; c++){         double v;         cout << "enter x0" << c << ":";         cin >> v;         a.push_back(v);         cout << "enter y0" << c << ":";         cin >> v;         b.push_back(v);     }       std::cout << std::endl;     (int = 0; < x; ++i)         (int j = 0; j < x; j = j + 2){             try {             double value = func(a, b, i, j);              std::cout << "p(x): " << value << "(x-" << a[i] << ")" << std::endl;             }             catch (...) {                 std::cout << "func( " << << ", " << j << " ) = invalid " << std::endl;             }         }     return 0; } 

note: "i'm beginner in c++ , i'm weak in english language if not understand problems please tell me explain thank all"


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -