fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <cmath>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. void newton_backward_interpolation() {
  10. int n = 4;
  11. double x_vals[] = {0.0, 1.0, 2.0, 3.0};
  12. double y_vals[] = {1.0, 0.0, 1.0, 10.0};
  13. double a = 0.5;
  14.  
  15. vector<vector<double>> diff_table(n, vector<double>(n));
  16.  
  17. for (int i = 0; i < n; ++i) {
  18. diff_table[i][0] = y_vals[i];
  19. }
  20.  
  21. for (int j = 1; j < n; ++j) {
  22. for (int i = 0; i < n - j; ++i) {
  23. diff_table[i][j] = diff_table[i + 1][j - 1] - diff_table[i][j - 1];
  24. }
  25. }
  26.  
  27. cout << setprecision(6) << fixed;
  28. cout << "--- Newton's Backward Interpolation ---" << endl;
  29. cout << "Input table (x, y):" << endl;
  30. for (int i = 0; i < n; ++i) {
  31. cout << "(" << x_vals[i] << ", " << y_vals[i] << ") ";
  32. }
  33. cout << "\nPoint of Interpolation (a): " << a << endl;
  34. cout << string(60, '-') << endl;
  35.  
  36. cout << "Output: The backward difference table is (same as forward, but values used start at the bottom):" << endl;
  37. cout << setw(10) << "x" << setw(10) << "y";
  38. for (int j = 1; j < n; ++j) {
  39. cout << setw(10) << "Nabla^" << j << "y";
  40. }
  41. cout << endl;
  42. cout << string(10 + 10 * n, '-') << endl;
  43.  
  44. for (int i = 0; i < n; ++i) {
  45. cout << setw(10) << x_vals[i] << setw(10) << diff_table[i][0];
  46. for (int j = 1; j < n - i; ++j) {
  47. cout << setw(10) << diff_table[i][j];
  48. }
  49. cout << endl;
  50. }
  51. cout << string(10 + 10 * n, '-') << endl;
  52.  
  53. double h = x_vals[1] - x_vals[0];
  54. double u = (a - x_vals[n - 1]) / h;
  55. double sum = diff_table[n - 1][0];
  56. double p = 1.0;
  57.  
  58. for (int j = 1; j < n; ++j) {
  59. double backward_diff = diff_table[n - 1 - j][j];
  60. p = p * (u + j - 1) / j;
  61. sum = sum + p * backward_diff;
  62. }
  63.  
  64. cout << "\nThe value of y at x=" << a << " is " << round(sum * 1000.0) / 1000.0 << endl;
  65. cout << "Calculated value: " << sum << endl;
  66. }
  67.  
  68. int main() {
  69. newton_backward_interpolation();
  70. return 0;
  71. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
--- Newton's Backward Interpolation ---
Input table (x, y):
(0.000000, 1.000000) (1.000000, 0.000000) (2.000000, 1.000000) (3.000000, 10.000000) 
Point of Interpolation (a): 0.500000
------------------------------------------------------------
Output: The backward difference table is (same as forward, but values used start at the bottom):
         x         y    Nabla^1y    Nabla^2y    Nabla^3y
--------------------------------------------------
  0.000000  1.000000 -1.000000  2.000000  6.000000
  1.000000  0.000000  1.000000  8.000000
  2.000000  1.000000  9.000000
  3.000000 10.000000
--------------------------------------------------

The value of y at x=0.500000 is 0.625000
Calculated value: 0.625000