1.矩形法求定积分
program definiteintegralbyrectangular
implicit none
real,parameter::pi=3.1415926
integer,parameter::N=100!调整精度
real lowbound,upbound,len,x,sum,f
lowbound = 0 !输入下界
upbound = pi!输入上界
len = (upbound - lowbound)/N
sum = 0.0
x = lowbound
do while(x<upbound)
f = cos(x)-x**2 !输入要积分的函数
sum = sum + f * len
x = x + len
end do
write(*,*)'The final result = ',sum
pause
end
2.梯形法求定积分
program definiteintegralbyrectangular
implicit none
real,parameter::pi=3.1415926
integer,parameter::N=100!调整精度
real lowbound,upbound,len,x,sum,f1,f2
lowbound = 0 !输入下界
upbound = pi!输入上界
len = (upbound - lowbound)/N
sum = 0.0
x = lowbound
do while(x<upbound)
f1 = cos(x)-x**2 !输入要积分的函数
f2 = cos(x+len)-(x+len)**2
sum = sum + (f1+f2)*len/2
x = x + len
end do
write(*,*)'The final result = ',sum
pause
end
3.辛普森法
program definiteintegralbyrectangular
implicit none
real,parameter::pi=3.1415926
integer,parameter::N=100!调整精度
real lowbound,upbound,len,x,sum,f1,f2,f3
lowbound = 0 !输入下界
upbound = pi!输入上界
len = (upbound - lowbound)/N
sum = 0.0
x = lowbound
do while(x<upbound)
f1 = cos(x)-x**2 !输入要积分的函数
f2 = (cos(x+len/2)-(x+len/2)**2)*4
f3 = cos(x+len)-(x+len)**2
sum = sum + (f1+f2+f3)*len/6
x = x + len
end do
write(*,*)'The final result = ',sum
pause
end