To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

integration.cpp 523 Bytes
Newer Older
Erick Turricelli's avatar
Erick Turricelli committed
1
2
3
4
5
#include <iostream>
#include <cmath>
#include <cassert>
#define _USE_MATH_DEFINES

6
double integration(const double a, const double b, const int N, const double (*my_func)(double x)) {
Erick Turricelli's avatar
Erick Turricelli committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    assert(N>0);
    assert(b > a);
    double delta = (b-a)/N;
    double result = my_func(a);

    for (int i = 1; i< N; i++) {
        if (i%2==1) {
            result += 4*my_func(a+i*delta);
        }
        else {
            result += 2*my_func(a+i*delta);
        }
    }
    result += my_func(b);
    return result*delta/3.0;
}