TRAPEZOIDAL RULE:
THEORY:
Trapezoidal Rule is a Numerical technique to find the definite integral of a function. The
function is divided into many sub-intervals and each interval is approximated by a Trapezium.
Then the area of trapeziums is calculated to find the integral which is basically the area under
the curve. The more is the number of trapeziums used, the better is the approximation.
ALGORITHM:
1. Start
2. Define function f(x)
3. Read lower limit of integration, upper limit of
integration and number of sub interval
4. Calcultae: step size = (upper limit - lower limit)/number of sub interval
5. Set: integration value = f(lower limit) + f(upper limit)
6. Set: i = 1
7. If i > number of sub interval then goto
8. Calculate: k = lower limit + i * h
9. Calculate: Integration value = Integration Value + 2* f(k)
10. Increment i by 1 i.e. i = i+1 and go to step 7
11. Calculate: Integration value = Integration value * step size/2
12. Display Integration value as required answer
13. Stop
SOURCE CODE:
#include<stdio.h>
float f(float x)
{
return (x*x*x);
}
float trap(float h, float n, float x[])
{
float f_x1 = 0, f_x2 = 0, total;
int i;
for(i = 0; i<=n; i++)
{
if(i==0 || i == n)
f_x1 += (h/2)*(f(x[i]));
else
f_x2 += (h/2)*2*(f(x[i]));
}
total = f_x1 + f_x2;
return total;
}
int main()
{
float lv,hv,h, n;
printf("Enter lower limit, higher limit and n:\n");
scanf("%f%f%f",&lv,&hv,&n);
h = (hv-lv)/n;
float x[20];
x[0] = lv;
int i;
for(i=1; i<=n; i++)
{
x[i] = x[i-1] + h;
}
printf("h = %.2f\n\n",h);
for(i=0; i<=n; i++)
{
printf("x(%d) = %.3f\n",i,x[i]);
}
printf("\n");
for(i=0; i<=n; i++)
{
printf("y(%d) = %.3f\n",i,f(x[i]));
}
float ans = trap(h,n,x);
printf("\nAns = %.3f",ans);
return 0;
OUTPUT:
DISCUSSION AND CONCLUSION:
Here, we discussed about the algorithm and source code of Trapezoidal Rule method and
we practically coded the source code of the Trapezoidal Rule method using programming
language ( C ) to test the example of solution of a problem relating Trapezoidal Rule method.