You are on page 1of 4

Romberg integration technique

General Example :
Integrate the function sin(x) on the interval [a, b] = [0, &pi]. From calculus, you know that
the answer is 2. Continue iterating until εstep < 1e-5.

Let h = b - a = π. Then

R0, 0 = T(h) = ½(sin(0) + sin(π))π = 0

Now, for i = 1, 2, ..., we calculate:

i=1

R1,0 = T(π/2) = 1.5707963267948966192

 (4 ⋅ 1.5707963267948966192 - 0)/3 = 2.0943951023931954923

i=2

R2,0 = T(π/4) = 1.8961188979370399

 (4 ⋅ 1.8961188979370399 - 1.5707963267948966)/3 = 2.0045597549844210


 (16 ⋅ 2.0045597549844210 - 2.0943951023931955)/15 = 1.9985707318238360

i=3

R3,0 = T(π/8) = 1.9742316019455508

 (4 ⋅ 1.9742316019455508 - 1.8961188979370399)/3 = 2.0002691699483878


 (16 ⋅ 2.0002691699483878 - 2.0045597549844210)/15 = 1.9999831309459856
 (64 ⋅ 1.9999831309459856 - 1.9985707318238360)/63 = 2.0000055499796705

i=4

R4,0 = T(π/16) = 1.9935703437723393

 (4 ⋅ 1.9935703437723393 - 1.9742316019455508)/3 = 2.0000165910479355


 (16 ⋅ 2.0000165910479355 - 2.0002691699483878)/15 = 1.9999997524545720
 (64 ⋅ 1.9999997524545720 - 1.9999831309459856)/63 = 2.0000000162880417
 (256⋅2.0000000162880417 - 2.0000055499796705)/255 = 1.9999999945872902
Finally, |1.9999999945872902 - 2.0000055499796705| ≈ 0.00000556, and thus we may
halt and our approximation of the integral is 1.9999999945872902

Matlab Code :
C programming code (Romberg integration ):

#include <stdio.h>
#include <math.h>
void
dump_row(size_t i, double *R){
printf("R[%2zu] = ", i);
for(size_t j = 0; j <= i; ++j){
printf("%f ", R[j]);
}printf("\n");
}double
romberg(double (*f/* function to integrate */)(double), double /*lower
limit*/ a, double /*upper limit*/ b, size_t max_steps, double /*desired
double R1[max_steps], R2[max_steps]; //buffers
double *Rp = &R1[0], *Rc = &R2[0]; //Rp is prev row,Rc is current row
double h = (b-a); //step size
Rp[0] = (f(a) + f(b))*h*.5; //first trapezoidal step
dump_row(0, Rp);
for(size_t i = 1; i < max_steps; ++i){
h /= 2.;
double c = 0;
size_t ep = 1 << (i-1); //2^(n-1)
for(size_t j = 1; j <= ep; ++j){
c += f(a+(2*j-1)*h); }
Rc[0] = h*c + .5*Rp[0]; //R(i,0)
for(size_t j = 1; j <= i; ++j){
double n_k = pow(4, j);
Rc[j] = (n_k*Rc[j-1] - Rp[j-1])/(n_k-1); //compute R(i,j)
//Dump ith column of R, R[i,i] is the best estimate so far
dump_row(i, Rc);
if(i > 1 && fabs(Rp[i-1]-Rc[i]) < acc){
return Rc[i-1]; //swap Rn and Rc as we only need the last
row double *rt = Rp;
Rp = Rc;

Rc = rt; }
return Rp[max_steps-1]; //return our best guess
}

You might also like