-
Notifications
You must be signed in to change notification settings - Fork 0
/
approx.cpp
54 lines (50 loc) · 1.51 KB
/
approx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "approx.h"
// http://en.wikipedia.org/wiki/Trapezoidal_rule
double trap(int *data, int a, int b, int n)
{
double mult = static_cast<double>(b-a)/(2*n);
int step = (b-a)/(n);
int sum = 0;
for (int i=0; i<b; i+=step)
{
sum += data[i] + data[i+step];
}
return mult*sum;
}
// general case romberg's method for
// approximating definite integrals
// I did not write this function, I merely modified it
// from its original, at:
// http://en.wikipedia.org/wiki/Romberg%27s_method
// quad is a quadrature method (estimation of numerical integration)
// basic examples include simpson's rule and the trapezoid rule.
// more advanced examples include Guassian quadrature, and Clenshaw–Curtis quadrature
int romberg(int *data, int n, double (*quad)(int*, int, int, int))
{
double s[n];
int i,k;
double var ;
for (i = 1; i< n; i++)
s[i] = 1;
k = 1;
while((1 << k+1) < n)
{
for (i=1; i <=k; i++)
{
if (i==1)
{
var = s[i];
s[i] = quad(data, 0, n, (1 << k-1)); // sub-routine approximation
} // integrated from 0 and 1
/* pow() is the number of subdivisions*/
else
{
s[k]= ( (1 << 2*(i-1))*s[i-1]-var )/((1 << 2*(i-1)) - 1);
var = s[i];
s[i]= s[k];
}
}
k++;
}
return s[n-1];
}