加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

辛普森法求定积分(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

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有