Sei sulla pagina 1di 5

27

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 ].

So, by the Trapezoidal rule-

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

OUTPUT: The numerical value of the definite integral.

PROCESS:

Step 1: The equation ( i.e. 4x−3x2 here) is defined by ‘macro’ F(x)

Step 2: Create the function “void main()”

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 a0.0) and upper limit of the integral is

stored in a float type variable ‘b’ (i.e. set b1.0)

Step 2.2: Set h(float)(b-a)/n

Step 2.3: Set mF(a)


28

Step 2.4: Set tF(b)


Step 2.5: Print the value of the integral ‘m’ for the lower limit ‘a’ and print the the value of

the integral ‘t’ for the upper limit.

Step 2.6: For i=0 to i< n-1 repeat Step 2.7 to Step 2.10

Step 2.7: Set zz+h

Step 2.8: Set yF(z)

Step 2.9: Set sumsum+y

Step 2.10: Print the float values z and y

[End of Step 2.6 ‘For’ loop]

Step 2.11: Set sum2*sum

Step 2.12: Set s(h/2) *(sum+m+t)

Step 2.13: Print the ans i.e. the float type variable ‘s’

(correct upto three decimal places)

[End of the function “void main( )”]

Step 3: Stop.

PROGRAM CODE:

#include<stdio.h>

#include<conio.h>

#define F(x) (4*x)-(3*x*x)

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

printf(" Enter interval: ");

scanf("%d",&n);
h=(float)(b-a)/n;

m=F(a);

t=F(b);

printf("\n\n Value of the integral for the lower limit= %f",m);

printf("\n\n Value of the integral for the upper limit= %f",t);

printf("\n\n Intermediate values:");

printf("\n\t x\t\t y=f(x)\n");

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);

printf("\n\n Ans= %0.3f (corrected upto 3 decimal places)",s);

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

Potrebbero piacerti anche