You are on page 1of 1

1 /*

2
ROMBERG'S INTEGRATION PROGRAM
3
written by Gulsen Taskin
4
May 27 2005
5
6 */
7 #include<stdio.h>
8 #include<math.h>
9 /*Simpson's function is selected as exp(-y)*sin(y)*/
10 long double f (long double y)
11 {
12
return(exp(-y)*sin(y));
13 }
14 /* SIMPSON'S INTEGRATION FUNCTION */
15 double simpson(double a, double b, int n)
16 {
17
long double integral,x,h;
18
long double part,coeff,m;
19
int i;
20
m = f(3.333333);
21
part = (b-a) / (long double) n;
22
h = part / (long double) 3.0;
23
24
integral = 0;
25
x = 0;
26
27
for (i=0;i<n;i++)
28
{
29
integral = integral+f(x)+3.0*f(x+h)+3.0*f(x+2*h)+f(x+3.0*h);
30
x = x + 3.0*h ;
31
}
32
coeff = 3.0*h / 8.0 ;
33
integral = coeff * integral ;
34
return integral;
35 }
36 /* C MAIN FUNCTION
*/
37 int main()
38 {
39
int n;
40
double a, b;
41
double romberg;
42
a=0.0;
43
b=10.0;
44
n=10;
45
46 /*ROMBERG INTEGRATION
*/
47
romberg = (1.0/3.0)*simpson(a,b,n)-2.0*simpson(a,b,2*n)
+(8.0/3.0)*simpson(a,b,4*n);
48
printf("FUNCTION\n");
49
printf("f(y) -> exp(-y)*sin(y)\n");
50
printf("Romberg Integration [%lf-%lf]= %f\n",a,b,romberg);
51
return 0;
52 }

syntax highlighted by Code2HTML, v. 0.9.1

You might also like