You are on page 1of 2

Solution to Problem Set 5 Question No.

1(a) :

Program:

/* Program to evaluate integrals using the trapezoidal rule 1 */

#include<stdio.h>
#include<math.h>

void main()
{

int n=1;
float a,b,I1,I2,f(float),trap(float,float,int);

clrscr();

printf("Enter the value of a : ");


scanf("%f",&a);
printf("Enter the value of b : ");
scanf("%f",&b);

I1=trap(a,b,n);
I2=trap(a,b,n+1);

while(fabs(I1-I2)>0.000001)
{
n=n+1;
I1=trap(a,b,n);
I2=trap(a,b,n+1);
}

printf("The integral is %f",I2);


getch();

float f(float z)
{
return 1/z;
}

float trap(float x,float y,int m)


{
int i;
float I=f(x)+f(y),h=(y-x)/m;
for(i=1;i<=m-1;i++)
{
I=I+2*f(x+(i*h));
}
I=h/2*(I);
return I;
}

You might also like