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.

Solution of Ordinary Differential Equations
Rung-kutta Method
The Runge-Kutta method is the most widely used method of solving
differential equations with numerical methods.
Obviously, a more general (and more common) case involves ODEs that
depend on both x and y,
†›
ൌ ˆሺšǡ ›ሻ
†š
The most popular RK methods are fourth order. The following is the
most commonly used form, and we therefore call it the classical fourth-order
RK method:
k1 = h f (xi , yi)
k2 = h f (xi + h/2 , yi + k1 / 2)
k3 = h f (xi + h/2 , yi +k2 / 2)
k4 = h f (xi + h , yi + k3)
yi+1 = yi + k1 / 6 + k2 / 3 + k3 / 3 + k4 / 6
The fourth-order Runge-Kutta method requires four evaluations of the
right hand side per step h.
Example
Solve the following differential equation by using Runge - Kutta
method:
ϭ
݀‫ݕ‬
ൌ‫ݔ‬൅‫ݕ‬
݀‫ݔ‬
With initial y(0)=1, 0 ≤ x ≤ 0.1, assume h=0.1
K1=0.1[0+1]=0.10000
K2=0.1[.05+1.05]=0.11000
K3=0.1[.05+1.055]=0.11050
K4=0.1[0.1+1.11050]=0.12105
y(0.1) = 0.1 + 1/6 [0.1 + 2(0.11) + 2(0.11050) + 0.1210] = 1.11034
a fortran 90 program to solve ordinary differential equation by runge-kutta
method
program runge_kutta
implicit none
real,dimension(10)::x,y
integer::i
real::f,k1,k2,k3,k4,h,xo,yo
read(*,*) xo,yo,h
x(1)=xo
y(1)=yo
write(*,7)"x(1)=",xo,"y(1)=",yo
7format(2x,a,f9.6,2x,a,f9.6)
i=2
do
k1=h*f(x(i-1),y(i-1))
k2=h*f(x(i-1)+h/2,y(i-1)+k1/2)
k3=h*f(x(i-1)+h/2,y(i-1)+k2/2)
k4=h*f(x(i-1)+h,y(i-1)+k3)
y(i)=y(i-1)+1/6.0*(k1+2*k2+2*k3+k4)
x(i)=x(1)+h*(i-1)
Ϯ
if(x(i).gt.0.1) exit
write(*,17)"x(",i,")=",x(i),"y(",i,")=",y(i
17format (2x,a,i2,a,2x,f9.6,2x,a,i2,a,2x,f9.6
i=i+1
enddo
end
real function f(x,y)
implicit none
real::x,y
f=x+y
end
ϯ
Download