Krakenos

Untitled

Dec 5th, 2020
921
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. vector<vector<double>> romber(double (*function)(double), double a, double b, int M){
  9.     double h = b - a;
  10.     vector<vector<double>> R;
  11.     R.push_back({});
  12.     double first_value = h * (function(a) + function(b)) / 2.0;
  13.     R[0].push_back(first_value);
  14.     for (int n = 0; n < M; n++){
  15.         h = h / 2.0;
  16.         double inner_sum = 0;
  17.         for (int i = 1; i < pow(2, n-1); i++){
  18.             inner_sum += function(a + (2*i - 1) * h);
  19.         }
  20.         double column_value = 1.0 / 2.0 * R[n][0] + h * inner_sum;
  21.         inner_sum = 0;
  22.         R.push_back({});
  23.         R[n+1].push_back(column_value);
  24.         for(int m = 0; m < n; m++){
  25.             double row_value = R[n+1][m] + (R[n+1][m]-R[n][m]) / (pow(4, m) - 1);
  26.             R[n+1].push_back(row_value);
  27.         }
  28.     }
  29.     //print
  30.     for (int i = 0; R.size(); i++){
  31.         for(int j = 0; R[i].size(); j++){
  32.             cout << R[i][j] << ' ';
  33.         }
  34.         cout << endl;
  35.     }
  36.     return R;
  37. }
  38.  
  39.  
  40. int main(){
  41.     vector<vector<double>> result;
  42.     romber(&sin, 0, M_PI, 4);
  43.     cout << result.size() << endl;
  44.     cout << result[1].size() << endl;
  45.    
  46.     return 0;
  47. }
RAW Paste Data