我把二分法也改好了,但输出的结果好像错了,请问是哪里出了问题。
[Fortran] 纯文本查看 复制代码 module first
implicit none
real,parameter::zero=1E-15
integer,parameter::n=7
contains
real*8 function bisect(a,b)
implicit none
real*8::a,b,c,fa,fb,fc
bisect=0d0
do
c=(a+b)/2d0
fa=func(a)
fb=func(b)
fc=func(c)
if(fa*fc<0)then
b=c
else
a=c
end if
if((b-a)<zero)exit
end do
bisect=c
end function
real*8 function func(x)
implicit none
real*8::x
integer::i
real*8::fun(n)
fun(1)=x
fun(2)=1.5*x*x-0.5
do i=3,n
fun(i)=((2*n-1)*x*fun(i-1)-(n-1)*fun(i-2))/n
enddo
func=fun(n)
return
end function
end module first
module second
use first
implicit none
contains
subroutine fn0(fn)
implicit none
integer::i,j
real*8 :: fn(:)
real*8,allocatable :: k(:)
real*8::p,q,m
m=-1.0001_8
j=1
allocate(k(size(fn)))
k=0.0_8
do i=1,1999
p=func(m)
q=func(m+0.001_8)
if(p*q<zero)then
write(*,*)'j=',j,'m=',m
k(j)=m
j=j+1
endif
m=m+0.001_8
end do
do i=1,j
fn(i)=bisect(k(i),k(i)+0.001_8)
end do
end subroutine
end module second
program GSLD
use first
use second
implicit none
integer::i
real*8,allocatable::fn(:)
allocate(fn(200))
call fn0(fn)
do i=1,n
write(*,*)i,fn(i)
enddo
pause
endprogram GSLD
|