FORTRAN 90 Lecturer : Rafel Hekmat Hameed University of Babylon Subject : Fortran 90 College of Engineering Year : Second B.Sc. Mechanical Engineering Dep. Numerical Integration Numerical integration is the study of how the numerical value of an integral can be found. Also called quadrature, which refers to finding a square whose area is the same as the area under a curve, it is one of the classical topics of numerical analysis. Trapezoidal Rule The trapezoidal rule is a numerical method that approximates the value of a definite integral. We consider the definite integral ୠ න ሺሻ ୟ We assume that f(x) is continuous on [a, b] and we divide [a, b] into n subintervals of equal length ο ൌ െ using the n + 1 points x0 = a , x1 = a + x , x2 = a + 2x , . . . , xn = a + nx = b. We can compute the value of f(x) at these points. y0 = f(x0) , y1 = f(x1) , y2 = f(x2) , . . . , yn = f(xn) We approximate the integral by using n trapezoids formed by using straight line segments between the points (xi−1, yi−1) and (xi, yi) for 1 i n as shown in the figure below. ϭ The area of a trapezoid is obtained by adding the area of a rectangle and a triangle. ሺ ଵ ሻο ͳ ൌ ο ሺଵ െ ሻο ൌ ʹ ʹ By adding the area of the n trapezoids, we obtain the approximation න ݂ሺݔሻ݀ ݔൎ ሺݕ ݕଵ ሻο ݔሺݕଵ ݕଶ ሻοݔ ሺݕିଵ ݕ ሻοݔ ڮ ʹ ʹ ʹ which simplifies to the trapezoidal rule formula. න ݂ሺݔሻ݀ ݔൎ οݔ ሺ ݕ ʹݕଵ ʹݕଶ ڮ ʹݕିଵ ݕ ሻ ʹ Example Use the trapezoidal rule with n = 8 to estimate ହ න ඥͳ ݔଶ ݀ݔ ଵ For n = 8, we have x = (5−1)/8 = 0.5. We compute the values of y0, y1, y2, . . . , y8. x 1 1.5 2 2.5 3 3.5 4 4.5 5 ࢟ ൌ ඥ ࢞ ξʹ ξ͵Ǥʹͷ ξͷ ξǤʹͷ ξͳͲ ξͳ͵Ǥʹͷ ξͳ ξʹͳǤʹͷ ξʹ Ϯ ହ ଵ ξͳ ݔଶ ݀ ݔൎ ͳʹǤ program trapezoidal_rule implicit none integer:: i real::a,b,n,dx,sum,f,x f(x)=sqrt(1+x**2) read(*,*) a,b,n dx=(b-a)/n sum=f(a)+f(b) do i=1,n-1 x=a+(i*dx) sum=sum+2.0*f(x) enddo sum=sum*dx/2.0 print*, "for n =",n ," ", "Integral =", sum end ϯ