Program Driver file for trapezd.c routine
/* This is a C program to integrate f(x) in the interval a<= x <= b
* using the composite trapezoidal method described in class. */
/* Illustrates the use of NRC */
/* See page 293 of ABC for passing functions as arguements */
#include <stdio.h>
#include <math.h>
#include "nr.h"
#include "nrutil.h"
float func(float);
float trapzd( float (*func)(float), float low_lim, float up_lim, int ntr);
int main(void)
{
float integral,low_lim,up_lim;
int i,ntr;
/*
* Read the lower and upper limits of the integral, and the number of
* trapezoids to be used = ntr
*/
printf("Enter the lower and upper bounds:\n");
scanf("%f%f",&low_lim,&up_lim);
printf("Value of n(>0): number of trapezoids used is 2^n+1.\n");
scanf("%d",&ntr);
/* */
for(i=1; i<=ntr+1; i++) integral = trapzd(func,low_lim,up_lim,i);
/* */
printf("Integral is %e\n",integral);
}
float func(float x)
{
float pi;
pi = 4.0*atan(1.0);
return exp(-x*x/2.0)/sqrt(2.0*pi);
}