L e c t

advertisement
FORTRAN 90
Lecturer : Rafel Hekmat Hameed
University of Babylon
Subject : Fortran 90
College of Engineering
Year : Second B.Sc.
Mechanical Engineering Dep.
Numerical differentiation
Numerical differentiation is the process of finding the numerical value of a
derivative of a given function at a given point,
point there
here are many applications where
derivatives need to be computed numerically.
First derivatives
The most accurate method for approximating the first derivative of f can be
seen in the next diagram
Here we approximate as follows
݂ሺҧ ܽሻ ൎ slope of short broken line=
ҧ
This is called a central difference approximation to ݂ሺܽሻ
ϭ
Second derivatives
Ӗ
A central difference approximation to the second derivative ݂ሺܽሻis
Example
Use a central difference method to approximate the first and ssecond derivative
of cos(x) at x = ð/3.,, assume the value of h =0.01
݂ሺ‫ݔ‬ሻ ൌ …‘•ሺ‫ݔ‬ሻ
݂ ሺ‫ ݔ‬൅ ݄ሻ െ ݂ሺ‫ ݔ‬െ ݄ሻ
݂ሺҧ ‫ݔ‬ሻ ൌ
ʹ݄
ߨ
ߨ
ܿ‫ ݏ݋‬ቀ ൅ ͲǤͲͳቁ െ ܿ‫ݏ݋‬ሺ
ܿ
െ ͲǤͲͳሻ
ͲǤͶͻͳ͵ͳͶͺͻ െ ͲǤͷͲͺ͸͵ͷͳͳ
ͳ
͵
͵
݂ሺҧ ‫ݔ‬ሻ ൌ
ൌ
ൌ െͲǤͺ͸͸ͲͳͲͻ͹
ʹ ൈ ͲǤǤͲͳ
ͲǤͲʹ
݂ ሺ‫ ݔ‬൅ ݄ሻ െ ʹ݂ ሺ‫ ݔ‬ሻ ൅ ݂ሺ‫ ݔ‬െ ݄ሻ
݄ଶ
ߨ
ߨ
ߨ
ܿ‫ ݏ݋‬ቀ ൅ ͲǤͲͳቁ െ ʹܿ‫ݏ݋‬
ʹ
ቀ ቁ ൅ ܿ‫ݏ݋‬ሺ െ ͲǤͲͳሻ
͵
͵
͵
݂ሺӖ ‫ݔ‬ሻ ൌ
ൌ െͲǤͷͲͲͲͲʹ͵
ͲǤͲͳଶ
݂ሺӖ ‫ݔ‬ሻ ൌ
A fortran 90 program
m to find the first and second derivative by central
difference approximation
program central_difference_method
implicit none
real::h,a,first,second,f1,f2
read(*,*)h,a
f1=first(a,h)
f2=second(a,h)
write(*,9)"for x=",a,"first derevitive=",f1
write(*,9)"for x=",a,"second derevitive=",f2
9 format(3x,a,f7.3,2x,a,2x,f7.3)
end
real function first(a,h)
implicit none
Ϯ
real::a,h
first=(cos(a+h)-cos(a-h))/(2*h)
end
real function second(a,h)
implicit none
real::a,h
second=(cos(a+h)-(2*cos(a))+cos(a-h))/(h**2)
end
If we used an internal function to calculate the first and second derivative the
program will be
program central_difference_method
implicit none
real::h,a,first,second,f1,f2,x,f
f(x)=cos(x)
read(*,*)h,a
first=(f(a+h)-f(a-h))/(2*h)
second=(f(a+h)-(2*f(a))+f(a-h))/(h**2)
f1=first
f2=second
write(*,9)"for x=",a,"first derevitive=",f1
write(*,9)"for x=",a,"second derevitive=",f2
9 format(3x,a,f7.3,2x,a,2x,f7.3)
end
ϯ
Download