我正在尝试实现一段代码,通过使用Simpson's Rule
.
!(file:///D:/1-%20TUD/Semester%201/Numerical%20Methods%20BIWO-04/Lectures/simpson's%20rule.JPG)
我已经使用 MatchCAD 进行了尝试,并且得到了正确的结果函数:f(x)= x**5+(x-2)*sin(x)+(x-1)
program simpsons
implicit none
real a, b, h
real integ, fa, fb
integer i, m
write(*,*) 'enter the lower boundary'
read(*,*) a
write(*,*) 'enter the upper boundary'
read(*,*) b
do while(a.ge.b)
write(*,*) 'reenter the lower boundary'
read(*,*) a
write(*,*) 'reenter the upper boundary'
read(*,*) b
enddo
write(*,*) 'enter the intervals number'
read(*,*) m
h=(b-a)/2.0
fa=a**5.0+(a-2.0)*sin(a)+(a-1.0)
fb=b**5.0+(b-2.0)*sin(b)+(b-1.0)
integ=0
do i=1, m/2
integ=integ+4*((a+(2*i-1)*h)**5.0+((a+(2*i-1)*h)-2.0)*sin((a+(2*i-1)*h))+ ((a+(2*i-1)*h)-1.0))
if (i.le.((m/2)-1)) then
integ=integ+2*((a+2*i*h)**5.0+((a+2*i*h)-2.0)*sin((a+2*i*h))+((a+2*i*h)-1.0))
endif
enddo
integ=(fa+fb+integ)*(h/3.0)
write(*,*) 'integration = ', integ
end
当我输入a=-1
并且我应该b=1
得到m=20
Integration=-1.398
当我输入a=-1
并且我应该b=1
得到m=40
Integration=-1.398
但不知何故我得到了整合=7015869.0