主题:如何找错误
lqmj
[专家分:40] 发布于 2010-10-14 01:27:00
我是初学者,尝试编写一个简单问题的程序,一种写法是没有用到SUBRUTINE编出,另一种写法是用到subroutine,call ,但是二者计算结果相差甚远,我估计可能是用了subroutine的程序出错了,但试了几次也不知错在哪,恳请高手指点
板凳
lqmj [专家分:40] 发布于 2010-10-14 01:53:00
program dinit
!one dimension
intrinsic atan
real*8 x,namda,f,s,a,b,phi,phiba,xtry,ftry,dff,gapploc,aphi,dfirst
x = 100.0d0
namda = 1.0d-4
shift = 1.39174521
F = atan(X-shift)
s = sqrt(x*x+f*f)
b = f/s-1
a = x/s-1
phi = x+F-s
phiba = b*phi
Xtry= x+namda*phiba
ftry = atan(xtry - shift)
dff =ftry-f
Gapploc = dff/namda
aphi = a*phi
Gapp = aphi+Gapploc
dfirst = -gapp
print *,'x=',x
print *,'F=',f
print *,'Xtry=',Xtry
print *,'ftry=',ftry
print *,'DFF=',dff
print *,'a=',a
print *,'b=',b
print *,'Gapploc=',Gapploc
print *,'aphi=',aphi
print *,'dfirst=',dfirst
print *,'namda=',namda
END
另一个含有subroutine的程序为
program firststep
external initpoint,Gapproximate, direction
parameter n=1
real*8 x(n)
real*8 a,dfirst(n)
real*8 gapprox
parameter namda = 1.0d-4
call initpoint(N)
call Gapproximate(N,numf,namda,Gapprox,a)
call direction(N,dfirst,numf,namda)
print *,'dfirst=',dfirst
print *,'namda=',namda
print *,'numf=',numf
print *,'a=',a
end
subroutine initpoint(N)
real*8 x(n)
do I=1,N
X(I) = 100.0D0
endDO
END SUBROUTINE
subroutine Gapproximate(N,numf,namda,Gapprox,a)
integer I,N,numf
real*8 x(n),F(n),a(n),b(n),dff(n),phi(n),phiba(n),Gapprox(n),Gapproxloc(n),Xtry(n),Ftry(n)
real*8 sqxf
intrinsic SQRT
external fcycle
numf = 0
call fcycle(n,x,f)
numf = numf+1
do I = 1,N
sqxf = SQRT(X(I)*X(I)+F(I)*F(I))
a(I) = X(I)/sqxf-1
b(I) = F(I)/sqxf-1
phi(I) = X(I)+F(I)-sqxf
phiba(I) = b(I)*phi(I)
Xtry(I) = X(I)+namda*phiba(I)
ENDDO
call fcycle(N,Xtry,Ftry)
!numf denote the times of function evaluation
numf = numf+1
DO I = 1,N
Gapproxloc(I) = (Ftry(I)-F(I))/namda
Gapprox(I) = a(I)*phi(I)+Gapproxloc(I)
dff(I) = Ftry(I)-F(I)
enddo
end subroutine
subroutine direction(N,dfirst,numf,namda)
integer I,N
real*8 belta,theta,Gbefnorm
real*8 Gnamda(n),Gbef(n),y(n),d(n),dfirst(n)
external Gapproximate
call Gapproximate(N,numf,namda,Gbef,a)
DO I = 1,N
dfirst(I) = -Gbef(I)
ENDDO
end subroutine
subroutine fcycle(n,X,f)
!one dimension
intrinsic atan
real*8 F, X,shift
shift = 1.39174521
F = atan(X-shift)
RETURN
END