主题:求助
各位高手,小弟我写了一个求根程序,老是出错,可是我所有的情况都讨论了啊,太郁闷了!
错误为:
run-time error M6201: MATH
-sqrt:domain error
以下是我的程序
=============================
PROGRAM SLOVE
DIMENSION X(10)
EXTERNAL F
C DO Q=0.0,5.0
CALL DDHRT(3.0,4.0,0.1,1.0E-06,X,10,M,F)
WRITE(*,10) M
10 FORMAT(1X,'M=',I2)
DO 20 I=1,M
20 WRITE(*,30) I,X(I)
30 FORMAT(1X,'X(',I2,')=',E15.6)
C END DO
END
FUNCTION F(X,Q)
REAL H1, H2, Q, X
H1=SQRT(X+2.0)
H2=SQRT(X-2.0)
H3=SQRT(-X-2.0)
H4=SQRT(2.0-X)
PI=3.141592635897
F=2.0*cos(H1*PI)*cos(H2*PI)-(H1/H2+H2/H1)*sin(H1*PI)*sin(H2*PI)
X -2.0
F2=2.0*cosh(H3*PI)*cosh(H4*PI)-(H3/H4+H4/H3)*sinh(H3*PI)
X *sinh(H4*PI)-2.0
F3=2.0*cos(H1*PI)*cosh(H4*PI)-(H1/H4+H4/H1)*sin(H1*PI)*sinh(H4*PI)
X -2.0
F4=2.0*cos(H4*PI)-2.0
IF (X-2.0.GT.0.0) THEN
F=F1
ELSE IF (X+2.0.LT.0.0) THEN
F=F2
ELSE IF (ABS(X)-2.0.LT.0.0) THEN
F=F3
ELSE IF (ABS(X)-2.0.EQ.0.0) THEN
F=F4
END IF
RETURN
END
SUBROUTINE DDHRT(A,B,H,EPS,X,N,M,F)
DIMENSION X(N)
M=0
Z=A
Y=F(Z)
10 IF ((Z.GT.B+H/2.0).OR.(M.EQ.N)) RETURN
IF (ABS(Y).LT.EPS) THEN
M=M+1
X(M)=Z
Z=Z+H/2.0
Y=F(Z)
GOTO 10
END IF
Z1=Z+H
Y1=F(Z1)
IF (ABS(Y1).LT.EPS) THEN
M=M+1
X(M)=Z1
Z=Z1+H/2.0
Y=F(Z)
GOTO 10
END IF
IF (Y*Y1.GT.0.0) THEN
Y=Y1
Z=Z1
GOTO 10
END IF
20 IF (ABS(Z1-Z).LT.EPS) THEN
M=M+1
X(M)=(Z1+Z)/2.0
Z=Z1+H/2.0
Y=F(Z)
GOTO 10
END IF
Z0=(Z1+Z)/2.0
Y0=F(Z0)
IF (ABS(Y0).LT.EPS) THEN
M=M+1
X(M)=Z0
Z=Z0+H/2.0
Y=F(Z)
GOTO 10
END IF
IF (Y*Y0.LT.0.0) THEN
Z1=Z0
Y1=Y0
ELSE
Z=Z0
Y=Y0
END IF
GOTO 20
END
错误为:
run-time error M6201: MATH
-sqrt:domain error
以下是我的程序
=============================
PROGRAM SLOVE
DIMENSION X(10)
EXTERNAL F
C DO Q=0.0,5.0
CALL DDHRT(3.0,4.0,0.1,1.0E-06,X,10,M,F)
WRITE(*,10) M
10 FORMAT(1X,'M=',I2)
DO 20 I=1,M
20 WRITE(*,30) I,X(I)
30 FORMAT(1X,'X(',I2,')=',E15.6)
C END DO
END
FUNCTION F(X,Q)
REAL H1, H2, Q, X
H1=SQRT(X+2.0)
H2=SQRT(X-2.0)
H3=SQRT(-X-2.0)
H4=SQRT(2.0-X)
PI=3.141592635897
F=2.0*cos(H1*PI)*cos(H2*PI)-(H1/H2+H2/H1)*sin(H1*PI)*sin(H2*PI)
X -2.0
F2=2.0*cosh(H3*PI)*cosh(H4*PI)-(H3/H4+H4/H3)*sinh(H3*PI)
X *sinh(H4*PI)-2.0
F3=2.0*cos(H1*PI)*cosh(H4*PI)-(H1/H4+H4/H1)*sin(H1*PI)*sinh(H4*PI)
X -2.0
F4=2.0*cos(H4*PI)-2.0
IF (X-2.0.GT.0.0) THEN
F=F1
ELSE IF (X+2.0.LT.0.0) THEN
F=F2
ELSE IF (ABS(X)-2.0.LT.0.0) THEN
F=F3
ELSE IF (ABS(X)-2.0.EQ.0.0) THEN
F=F4
END IF
RETURN
END
SUBROUTINE DDHRT(A,B,H,EPS,X,N,M,F)
DIMENSION X(N)
M=0
Z=A
Y=F(Z)
10 IF ((Z.GT.B+H/2.0).OR.(M.EQ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