FORTRAN 90

advertisement
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 + 2x , . . . , xn = a + nx = 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
ϯ
Download