Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ASSIGNMENT 7
TO COMPUTE THE NUMERICAL VALUE OF A DEFINITE INTEGRAL
BY TRAPIZOIDAL RULE
STATEMENT:
The numerical integration is a process of finding the numerical value of a definite integral.
Trapezoidal rule is two point quadrature, i.e., n=1. Therefore there are only two functional values
y0=f(x0)=f(a) and y1=f(xn)=f(b), where h=(b−a)/n, thus the second and higher order differences are
not possible.
Let,
b
I= ∫a f(x) dx [ a= lower limit and b= upper limit of the integral ].
h b−a
I = 2 [y0 + yn + 2 × (y1 + y2 + y3 + ⋯ + yn−1 )] where, h = n
Here in algorithm we consider h is the step length, n is the total number of sub-interval. m and t
stores the initial and last value respectively.
ALGORITHM:
1
INPUT: A definite integral I = ∫0 (4x − 3x 2 ) dx
PROCESS:
Step 2.1: Take the number of intervals in an integer type variable ‘n’. Lower limit of the
integral is stored in a ‘float’ type variable ‘a’ (i.e. set a0.0) and upper limit of the integral is
Step 2.6: For i=0 to i< n-1 repeat Step 2.7 to Step 2.10
Step 2.13: Print the ans i.e. the float type variable ‘s’
Step 3: Stop.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,i;
float a=0.0,b=1.0,t,m,z=0.0,y=0.0,h,s,sum=0.0 ;
29
scanf("%d",&n);
h=(float)(b-a)/n;
m=F(a);
t=F(b);
printf("\t--------------------------");
for(i=0;i<(n-1);i++)
{
z=z+h;
y=F(z);
sum=sum+y;
printf("\n\t%f\t%f\n",z,y);
}
sum=2*sum;
s=(h/2)*(sum+m+t);
getch();
}
30
OUTPUT:
DISCUSSION:
The trapezoidal rule is one of a family of formulas for numerical integration called Newton–Cotes
formulas, of which the midpoint rule is similar to the trapezoid rule. Simpson's rule is another member
of the same family, and in general has faster convergence than the trapezoidal rule for functions which
are twice continuously differentiable, though not in all specific cases. However for various classes of
rougher functions (ones with weaker smoothness conditions), the trapezoidal rule has faster
convergence in general than Simpson's rule.
31
Moreover, the trapezoidal rule tends to become extremely accurate when periodic functions are
integrated over their periods, which can be analyzed in various ways.
For non-periodic functions, however, methods with unequally spaced points such as Gaussian
quadrature and Clenshaw–Curtis quadrature are generally far more accurate; Clenshaw–Curtis
quadrature can be viewed as a change of variables to express arbitrary integrals in terms of periodic
integrals, at which point the trapezoidal rule can be applied accurately.
The error of the composite trapezoidal rule is the difference between the value of the integral and the
numerical result