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.
Simpson’s 1/3 Rule
Simpson’s rule is a numerical method that approximates the value of a
definite integral by using quadratic polynomials.
Let’s first derive a formula for the area under a parabola of equation
y = ax2 + bx + c passing through the three points: (−h, y0), (0, y1), (h, y2).
୦
ൌ න ሺƒš ଶ ൅ „š ൅ …ሻ†š
ି୦
య
మ
ଷ
ଶ
୦
ൌ ሺƒ ୶ ൅ „ ୶ ൅ …šሻቚ
ϭ
ି୦
ൌ
ʹƒŠଷ
൅ ʹ…Š
͵
Š
ൌ ሺʹƒŠଶ ൅ ͸…ሻ
͵
Since the points (−h, y0), (0, y1), (h, y2) are on the parabola, they satisfy
y = ax2 + bx + c. Therefore,
›଴ ൌ ƒŠଶ െ „Š ൅ …
›ଵ ൌ …
›ଶ ൌ ƒŠଶ ൅ „Š ൅ …
Observe that
y0 + 4y1 + y2 = (ah2 − bh + c) + 4c + (ah2 + bh + c) = 2 ah2 + 6 c. Therefore,
the area under the parabola is
οš
Š
ሺ› ൅ Ͷ›ଵ ൅ ›ଶ ሻ
ൌ ሺ›଴ ൅ Ͷ›ଵ ൅ ›ଶ ሻ ൌ
͵ ଴
͵
We consider the definite integral
ୠ
න ˆሺšሻ †š
ୟ
We assume that f(x) is continuous on [a, b] and we divide [a, b] into an even
number n of subintervals of equal length
„െƒ
οš ൌ

Using the n+1 points
x0 = a , x1 = a + x , x2 = a + 2x , . . . , xn = a + nx = b.
Compute the value of f(x) at these points.
y0 = f(x0), y1 = f(x1), y2 = f(x2), . . . , yn = f(xn).
Ϯ
We can estimate the integral by adding the areas under the parabolic arcs
through three successive points.
ୠ
න ˆሺšሻ†š ൎ
ୟ
οš
οš
οš
ሺ›଴ ൅ Ͷ›ଵ ൅ ›ଶ ሻ ൅ ሺ›ଵ ൅ Ͷ›ଶ ൅ ›ଷ ሻ ൅ ‫ ڮ‬൅ ሺ›୬ିଶ ൅ Ͷ›୬ିଵ ൅ ›୬ ሻ
͵
͵
͵
By simplifying, we obtain Simpson’s rule formula.
ୠ
න ˆሺšሻ†š ൎ
ୟ
οš
ሺ› ൅ Ͷ›ଵ ൅ ʹ›ଶ ൅ Ͷ›ଷ ൅ ʹ‫ݕ‬ସ ൅ ‫ ڮ‬൅ Ͷ‫ݕ‬௡ିଵ ൅ ‫ݕ‬௡ ሻ
͵ ଴
ସ
Example. Use Simpson’s rule with n = 6 to estimate ‫׬‬ଵ ξͳ ൅ ‫ ݔ‬ଷ
For n = 6, we have x = (4−1)/ 6 = 0.5. We compute the values of y0, y1, y2, . .
. , y6.
x
1
1.5
2
2.5
3
3.5
4
࢟ ൌ ඥ૚ ൅ ࢞૜
ξʹ
ξͶǤ͵͹ͷ
͵
ξͳ͸Ǥ͸ʹͷ
ξʹͺ
ξͶ͵Ǥͺ͹ͷ
ξ͸ͷ
Therefore,
ସ
Ǥͷ
ሺξʹ ൅ ͶξͶǤ͵͹ͷ ൅ ʹሺ͵ሻ ൅ Ͷξͳ͸Ǥ͸ʹͷ ൅ ʹξʹͺ ൅ ͶξͶ͵Ǥͺ͹ͷ ൅ ξ͸ͷ
͵
ൎ ͳʹǤͺ͹ͳ
න ඥͳ ൅ ‫ ݔ‬ଷ ݀‫ ݔ‬ൎ
ଵ
program simpson_rule
implicit none
integer:: i
;
real::a , b , n , dx , sum , f , x
f(x)=sqrt (1+x**3
read(*,*) a,b,n
;
dx=(b-a)/n
sum=f(a)+f(b)
do i=1,n-1
;
x=a+(i*dx)
If (i/2*2.ne.i) then
sum=sum+4*f(x)
else
;
endif ;
sum=sum+2*f(x)
enddo
sum=sum*dx/3.0
print*, "for n =",n ,"
",
"Integral =", sum
end
ϯ
Download