Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- using namespace std;
- vector<vector<double>> romber(double (*function)(double), double a, double b, int M){
- double h = b - a;
- vector<vector<double>> R (M+2, vector<double> (M+2, 0));
- R[1][1] = h * (function(a) + function(b)) / 2.0;
- for (int n = 1; n <= M; n++){
- h = h / 2.0;
- double inner_sum = 0;
- for (int i = 1; i <= pow(2, n-1); i++){
- inner_sum += function(a + (2*i - 1) * h);
- }
- R[n+1][1]= 1.0 / 2.0 * R[n][1] + h * inner_sum;
- for(int m = 1; m <= n; m++){
- R[n+1][m+1] = R[n+1][m] + (R[n+1][m]-R[n][m]) / (pow(4, m) - 1);
- }
- }
- //print
- for (int i = 1; i<R.size(); i++){
- for(int j = 1; j<R[i].size(); j++){
- cout << fixed << R[i][j] << ' ';
- }
- cout << endl;
- }
- return R;
- }
- int main(){
- vector<vector<double>> result;
- romber(&sin, 0, M_PI, 4);
- return 0;
- }
RAW Paste Data