辛普森法求定积分(fortran版)
(2009-06-06 15:50:08)
标签:
程序教育 |
分类: 数值分析 |
!Simpson method for integral
module INTEGRAL
implicit none
contains
real function integ(a,b,m)!subfunction
implicit none
real a,b,l
integer m,i
real datas(m)
real,external::func
if(mod(m,2)==0) then
write(*,*) 'm is an even number,please enter an odd number'
end if
forall(i=1:m)!build an array
datas(i)=a+(i-1)*l/(m-1)
end forall
l=b-a
integ=0
!!!!!!!!enter to the key code segment
integ=datas(1)+datas(m)
do i=2,m-1
if(mod(i,2)==0) then
integ=integ+4.0*f(datas(i))
else
integ=integ+2.0*f(datas(i))
end if
end do
integ=integ*l/(m-1)/3.0
return
end function
real function f(x) !the function to be solved
implicit none
real x
f=x+sin(x)
return
end function
end module
!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!main progam
program main
use INTEGRAL
implicit none
real a,b!a is the lower limit of integral
!b is the upper limit of integral
integer m!m is the number of the dot to be given,and m must be a odd
write(*,*) 'please enter a'
read(*,*) a
write(*,*) 'please enter b (b>a)'
read(*,*) b
write(*,*) 'please enter m'
read(*,*) m
write(*,*) integ(a,b,m)
stop
end