0% found this document useful (0 votes)
27 views2 pages

Solution To Problem Set 5 Question No. 1 (A) : Program

This program uses the trapezoidal rule to evaluate integrals numerically. It takes user input for the lower and upper bounds of integration a and b. It first calculates the integral using n and n+1 intervals, storing the results in I1 and I2. It then recursively increases n and recalculates I1 and I2 until the difference between them is less than 0.000001, at which point I2 is printed as the final integral value. The trap function implements the trapezoidal rule calculation for a given number of intervals m.

Uploaded by

pontas97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Solution To Problem Set 5 Question No. 1 (A) : Program

This program uses the trapezoidal rule to evaluate integrals numerically. It takes user input for the lower and upper bounds of integration a and b. It first calculates the integral using n and n+1 intervals, storing the results in I1 and I2. It then recursively increases n and recalculates I1 and I2 until the difference between them is less than 0.000001, at which point I2 is printed as the final integral value. The trap function implements the trapezoidal rule calculation for a given number of intervals m.

Uploaded by

pontas97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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