1 /* integration by simpson rule */
2 /* M. Vallieres, Spring 2001 */
6 /* function to integrate */
9 double x1, x2, x3, func;
14 func = exp(-x1*x1)-1.5*exp(-x2*x2)+exp(-x3*x3);
18 /* simpson rule implementation */
19 double simpson_int ( double a, double b, int N, double (*f)(double) )
21 double x, dx, integral;
26 printf( "\n N must be odd for the Simpson rule \n\n" );
29 dx = (b-a)/(double)(N-1);
30 integral = f(a) + f(b);
32 for ( ix=1 ; ix<N-1 ; ix++ )
35 integral = integral + (double)factor*f(x);
38 integral = integral * dx / 3.0;
47 /* integration parameters */
48 a = 0.0; /* integration interval */
53 /* # of grid points */
54 printf( "\n Input # grid points (0 to end): " );
61 /* calculate integral */
62 area = simpson_int ( a, b, N, fun );
64 printf( " Integral = %15.12f (Simpson using %d grid points) \n", area, N );