Krakenos

Untitled

Dec 5th, 2020
849
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 (M+1, vector<double> (M+1, 0));
  11.     R[0][0] = h * (function(a) + function(b)) / 2.0;
  12.     for (int n = 0; n < M; n++){
  13.         h = h / 2.0;
  14.         double inner_sum = 0;
  15.         for (int i = 1; i <= pow(2, n); i++){
  16.             inner_sum += function(a + (2*i - 1) * h);
  17.         }
  18.         R[n+1][0]= 1.0 / 2.0 * R[n][0] + h * inner_sum;
  19.         for(int m = 0; m < n; m++){
  20.             R[n+1][m+1] = R[n+1][m] + (R[n+1][m]-R[n][m]) / (pow(4, m+1) - 1);
  21.         }
  22.     }
  23.     //print
  24.     for (int i = 0; i<R.size(); i++){
  25.         for(int j = 0; j<R[i].size(); j++){
  26.             cout << fixed << R[i][j] << ' ';
  27.         }
  28.         cout << endl;
  29.     }
  30.     return R;
  31. }
  32.  
  33.  
  34. int main(){
  35.     vector<vector<double>> result;
  36.     romber(&sin, 0, M_PI, 4);
  37.     return 0;
  38. }
RAW Paste Data