FORTRAN 90 L e c

advertisement
FORTRAN 90
Lecturer : Rafel Hekmat Hameed
University of Babylon
Subject : Fortran 90
College of Engineering
Year : Second B.Sc.
Mechanical Engineering Dep.
N
Nuum
meerriiccaall S
Soolluuttiioonn ooff FFiirrsstt O
Orrddeerr D
Diiffffeerreennttiiaall E
Eqquuaattiioonn
Euler Method
The Euler methods are simple methods of solving first-order ODE,
particularly suitable for quick programming because of their great simplicity,
although their accuracy is not high.
Consider:
y' (x) = f(x, y)
;
y (x0) = y0
(1)
Let:
xi = x0 + i h ;
i = 0, 1,…. , n
yi = y (xi)
Yi
true solution evaluated at points xi
the solution to be calculated numerically.
Replace
y'(x)=(Yi+1-Yi)/h
Then Eq. (1) gets replaced with
Yi+1 = Yi + h f(xi , Yi)
EXAMPLE
Find a numerical solution to some first-order differential equation with
initial y(0) = 1, for 0  x  3.
5 dy/dx - y2 = -x2
dy/dx=1/5 (y2-x2)
The initial data is y(0) = 1 , assume h=.5 . So
x0 = 0 and y0 = 1.
x1 = x0 + h = 0 +.5=.5,
and
y1= y0 + h · f (x0, y0) = y0 +.1(y02 - x02)
= 1 +.1(12 − 02)=1.1
ϭ
x2 = x1 + h = .5 +.5=1
y2= y1 + h · f (x1, y1) = 1.1+.1 (1.12 − .52)=1.196
.
.
.
i
0
1
2
3
4
5
6
xi
0
.5
1
1.5
2
2.5
3
yi
1
1.1000
1.1960
1.2390
1.1676
.9039
0.3606
program Euler_method
implicit none
real, dimension (20)::x,y
integer::i
real :: h,xo,yo,f
read(*,*) xo,yo,h
x(1)=xo
y(1)=yo
print*,"for x=",x(1) , "y=",y(1)
i=2
do
x(i)=x(1)+(i-1)*h
y(i)=y(i-1)+h*f(x(i-1),y(i-1))
if (x(i).gt.3.0) exit
print*,"for x=",x(i) , "y=",y(i
i=i+1
enddo
end
real function f(x,y)
implicit none
real ::x,y
f=1/5.0*(y**2-x**2
end
Ϯ
Download