主题:程序错误提示,求解答
Belinda124
[专家分:0] 发布于 2010-12-13 23:37:00
PROGRAM interpolation_errors
IMPLICIT NONE
REAL :: x,q,k2=0.0,k6=0.0,k8=0.0,P2=0.0,P6=0.0,P8=0.0,fun_f
INTEGER :: i,j,k,m
REAL :: A(0:2,0:2),B(0:6,0:2),C(0:8,0:2)
DO i=0,8
IF(i==2) THEN
q=(10.0/2.0)
A(0,0)=-5
A(0,1)=fun_f(A(0,0))
A(0:2,0:2)=1
DO j=1,2
IF(j<=2) THEN
A(j,0)=(A(j-1,0)+q)
A(j,1)=fun_f(A(j,0))
ENDIF
DO k=0,2
IF(k/=j) THEN
A(k,2)=(A(k,2)*(x-A(j,0))/(A(k,0)-A(j,0)))
ELSE
A(k,2)=(A(k,2)*(x-A(0,0))/(A(k,0)-A(0,0)))
ENDIF
ENDDO
DO m=0,2
k2=(k2+A(m,1)*A(m,2))
P2=(k2*(1.0/(1.0+x**2)))
ENDDO
ENDDO
ENDIF
IF(i==6) THEN
q=(10.0/6.0)
B(0,0)=-5
B(0,1)=fun_f(B(0,0))
B(0:6,0:2)=1
DO j=0,6
IF(j<=6) THEN
B(j,0)=(B(j-1,0)+q)
B(j,1)=fun_f(B(j,0))
ENDIF
DO k=0,6
IF(k<=6) THEN
IF(k/=j) THEN
B(k,2)=(B(K,2)*(x-B(j,0))/(B(k,0)-B(j,0)))
ELSE
B(k,2)=(B(k,2)*(x-B(0,0))/(B(k,0)-B(0,0)))
ENDIF
ENDIF
ENDDO
DO m=1,6
IF(m<=6) THEN
k6=(k6+B(m,1)*B(m,2))
P6=(k6*(1.0/(1.0+x**2)))
ENDIF
ENDDO
ENDDO
ENDIF
IF(i==8) THEN
q=(10.0/8.0)
C(0,0)=-5
C(0,1)=fun_f(C(0,0))
C(0:6,0:2)=1
DO j=1,8
IF(j<=8) THEN
C(j,0)=(C(j-1,0)+q)
C(j,1)=fun_f(C(j,0))
ENDIF
DO k=1,8
IF(k<=8) THEN
IF(k/=j) THEN
C(k,2)=(C(K,2)*(x-C(j,0))/(C(k,0)-C(j,0)))
ELSE
C(k,2)=(C(k,2)*(x-C(0,0))/(C(k,0)-C(0,0)))
ENDIF
ENDIF
ENDDO
DO m=1,8
IF(m<=8) THEN
k8=(k8+C(m,1)*C(m,2))
P8=(k8*(1.0/(1.0+x**2)))
ENDIF
ENDDO
ENDDO
ENDIF
ENDDO
END PROGRAM interpolation_errors
Function fun_f(a)
IMPLICIT NONE
REAL :: fun_f(a)
REAL,INTENT(IN) :: a
fun_f(a)=(1.0/(1.0+a**2))
RETURN
END Function fun_f
回复列表 (共10个回复)
沙发
yeg001 [专家分:14390] 发布于 2010-12-14 00:09:00
1. 是帮你排错不是帮你看代码, 你说的错误提示呢?
2. 你都开了一个帖了, 何必再开一个浪费版面呢?
板凳
Belinda124 [专家分:0] 发布于 2010-12-14 01:14:00
Prog_n.f90:5.17:
INTEGER :: fun_f(a)
1
Error: Expression at (1) must be of INTEGER type,found REAL
Prog_n.f90:6.6:
fun_f(a)=(1.0/(1.0+a**2))
1
Warning: Extension: REAL array index at(1)
3 楼
臭石头雪球 [专家分:23030] 发布于 2010-12-14 05:41:00
很明显的错误啊。。
fun_f(a) 这是一个数组,数组的大小只能是整数,比如100个元素,或者200个元素。数组大小不能是实数,比如数组有100.3个元素??这不符合逻辑
4 楼
Belinda124 [专家分:0] 发布于 2010-12-14 18:35:00
谢谢
以下是我修改的程序,仍然运行不出来,请帮忙改下,
由于电脑及其古老,错误提示实在拷不出来
Function fun_f(VALUE(n))
IMPLICIT NONE
REAL,INTENT(IN),DIMENSION(9) :: VALUE
REAL,DIMENSION(9) :: fun_f(VALUE)
fun_f(VALUE(n))=(1.0/(1.0+VALUE(n)**2))
RETURN
END Function fun_f
PROGRAM interpolation_errors
IMPLICIT NONE
REAL :: x,q,k2=0.0,k6=0.0,k8=0.0,P2=0.0,P6=0.0,P8=0.0
INTEGER :: i,j,k,m
REAL :: A(0:2,0:2),B(0:6,0:2),C(0:8,0:2),fun_f
fun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(1.0/(1.0+x**2)))
ENDIF
ENDDO
ENDDO
ENDIF
ENDDO
END PROGRAM interpolation_errors
5 楼
臭石头雪球 [专家分:23030] 发布于 2010-12-14 19:19:00
楼主的计算机不能输入汉字么?或是楼主不会说中文?
那也请用楼主的语言来描述一下,OK??
我们的回答对你是否有帮助,你是否做了什么修改??
请说话好吗?
6 楼
yeg001 [专家分:14390] 发布于 2010-12-14 19:28:00
楼主, "Function fun_f(VALUE(n))" 不是这样定义一个函数的.
你之前的
Function fun_f(a)
IMPLICIT NONE
REAL :: fun_f
REAL,INTENT(IN) :: a
fun_f=(1.0/(1.0+a**2))
RETURN
END Function fun_f
就是了.
主程序那里声明一下fun_f 是extenal外部函数的. 就不要再定义一个fun_f变量.
分清什么是数组, 什么是函数.
7 楼
dongyuanxun [专家分:7180] 发布于 2010-12-14 19:45:00
pfan上的编码很乱 一会儿gbk 一会儿unicode
8 楼
Belinda124 [专家分:0] 发布于 2010-12-14 21:24:00
I always keep inputting chiese character, but after submitting, I don't know why all of them become irrecognizable characters which maybe you see on this page. So I am forced to input english instead now.
Anyhow, I am willing to restate what I am confused is how to define the externel function with a matrix variable in order to realize the value transimission from the external function to the main function.
Could you be kind to help me to correct this function? and I am willing to present a relative high score if everything goes well.Many thanks.
9 楼
yeg001 [专家分:14390] 发布于 2010-12-14 21:45:00
我不明白你第二次帖的代码子函数多了数组定义, 根据主函数的代码应该不是数组, 简单修改了一下.
Function fun_f(temp)
IMPLICIT NONE
REAL, INTENT(IN) :: temp
REAL :: fun_f
fun_f=(1.0/(1.0+temp**2))
RETURN
END Function fun_f
PROGRAM interpolation_errors
IMPLICIT NONE
REAL :: x,q,k2=0.0,k6=0.0,k8=0.0,P2=0.0,P6=0.0,P8=0.0
INTEGER :: i,j,k,m
REAL :: A(0:2,0:2),B(0:6,0:2),C(0:8,0:2)
REAL, external :: fun_f
DO i=2,8
IF(i==2) THEN
q=(10.0/2.0)
A(0,0)=-5.0
A(0,1)=fun_f(A(0,1))
A(0:2,0:2)=1.0
DO j=1,2
IF(j<=2) THEN
A(j,0)=(A(j-1,0)+q)
A(j,1)=fun_f(A(j,0))
ENDIF
DO k=0,2
IF(k/=j) THEN
A(k,2)=(A(k,2)*(x-A(j,0))/(A(k,0)-A(j,0)))
ELSE
A(k,2)=(A(k,2)*(x-A(0,0))/(A(k,0)-A(0,0)))
ENDIF
ENDDO
DO m=0,2
k2=(k2+A(m,1)*A(m,2))
P2=(k2*(1.0/(1.0+x**2)))
ENDDO
ENDDO
ENDIF
IF(i==6) THEN
q=(10.0/6.0)
B(0,0)=-5.0
B(0,1)=fun_f(B(0,0))
B(0:6,0:2)=1.0
DO j=0,6
IF(j<=6) THEN
B(j,0)=(B(j-1,0)+q)
B(j,1)=fun_f(B(j,0))
ENDIF
DO k=0,6
IF(k<=6) THEN
IF(k/=j) THEN
B(k,2)=(B(K,2)*(x-B(j,0))/(B(k,0)-B(j,0)))
ELSE
B(k,2)=(B(k,2)*(x-B(0,0))/(B(k,0)-B(0,0)))
ENDIF
ENDIF
ENDDO
DO m=1,6
IF(m<=6) THEN
k6=(k6+B(m,1)*B(m,2))
P6=(k6*(1.0/(1.0+x**2)))
ENDIF
ENDDO
ENDDO
ENDIF
IF(i==8) THEN
q=(10.0/8.0)
C(0,0)=-5.0
C(0,1)=fun_f(C(0,0))
C(0:6,0:2)=1.0
DO j=1,8
IF(j<=8) THEN
C(j,0)=(C(j-1,0)+q)
C(j,1)=fun_f(C(j,0))
ENDIF
DO k=1,8
IF(k<=8) THEN
IF(k/=j) THEN
C(k,2)=(C(K,2)*(x-C(j,0))/(C(k,0)-C(j,0)))
ELSE
C(k,2)=(C(k,2)*(x-C(0,0))/(C(k,0)-C(0,0)))
ENDIF
ENDIF
ENDDO
DO m=1,8
IF(m<=8) THEN
k8=(k8+C(m,1)*C(m,2))
P8=(k8*(1.0/(1.0+x**2)))
ENDIF
ENDDO
ENDDO
ENDIF
ENDDO
END PROGRAM interpolation_errors
编译是通过了, 不过运行出现数组越界, 出现在52行(我修改过的代码)
DO j=0,6
IF(j<=6) THEN
[color=800000]B(j,0)=(B(j-1,0)+q)[/color]
B(j,1)=fun_f(B(j,0))
ENDIF
可以想像, 当j=0时, 等号右边B(j-1,0)为B(-1,0), 显然你定义的数组没有这一项.
其他靠你自己搞了.
10 楼
Belinda124 [专家分:0] 发布于 2010-12-18 00:09:00
Thank you.
the following is the updated programme, which is still a little shy of the quota.
that is:
1. I want to define the independent variable x at the range of [-5,5], but why the output range of x is in the range of [1,11]?
2. why do the values of longitudinal coordinates P2,P6 and P8 always keep same?
Firstly, attached is the programme:
Function fun_f(temp)
IMPLICIT NONE
REAL, INTENT(IN) :: temp
REAL :: fun_f
fun_f=(1.0/(1.0+temp**2))
RETURN
END Function fun_f
PROGRAM interpolation_errors
IMPLICIT NONE
REAL :: x,q,k2=0.0,k6=0.0,k8=0.0,P2=0.0,P6=0.0,P8=0.0
INTEGER :: i,j,k,l,m,n
REAL :: A(0:2,0:2),B(0:6,0:2),C(0:8,0:2)
REAL, external :: fun_f
DO i=2,8
IF(i==2) THEN
q=(10.0/2.0)
A(0,0)=-5.0
A(0,1)=fun_f(A(0,0))
A(0:2,0:2)=1.0
DO j=1,2
A(j,0)=(A(j-1,0)+q)
A(j,1)=fun_f(A(j,0))
ENDDO
DO k=0,2
DO n=0,2
IF(k/=n) THEN
A(k,2)=(A(k,2)*(x-A(n,0))/(A(k,0)-A(n,0)))
ELSE
CYCLE
ENDIF
ENDDO
ENDDO
DO m=0,2
k2=(k2+A(m,1)*A(m,2)) !compute the value of pai(x)
ENDDO
P2=(k2/(1.0+x**2)) !compute the value of pai(x)*f(x)
OPEN(10,file='test0.dat')
DO l=0,2
WRITE(10,*)A(l,0),P2
ENDDO
CLOSE(10)
ENDIF
IF(i==6) THEN
q=(10.0/6.0)
B(0,0)=-5.0
B(0,1)=fun_f(B(0,0))
B(0:6,0:2)=1.0
DO j=0,6
B(j,0)=(B(j-1,0)+q)
B(j,1)=fun_f(B(j,0))
ENDDO
DO k=0,6
DO n=0,6
IF(k/=n) THEN
B(k,2)=(B(K,2)*(x-B(n,0))/(B(k,0)-B(n,0)))
ELSE
CYCLE
ENDIF
ENDDO
ENDDO
DO m=0,6
k6=(k6+B(m,1)*B(m,2))
ENDDO
P6=(k6/(1.0+x**2))
OPEN(10,file='test1.dat')
DO l=0,6
WRITE(10,*)B(l,0),P6
ENDDO
CLOSE(10)
ENDIF
IF(i==8) THEN
q=(10.0/8.0)
C(0,0)=-5.0
C(0,1)=fun_f(C(0,0))
C(0:6,0:2)=1.0
DO j=1,8
C(j,0)=(C(j-1,0)+q)
C(j,1)=fun_f(C(j,0))
ENDDO
DO k=1,8
DO n=1,8
IF(k/=n) THEN
C(k,2)=(C(K,2)*(x-C(n,0))/(C(k,0)-C(n,0)))
ELSE
CYCLE
ENDIF
ENDDO
ENDDO
DO m=0,8
k8=(k8+C(m,1)*C(m,2))
ENDDO
P8=(k8/(1.0+x**2))
OPEN(10,file='test2.dat')
DO l=0,8
WRITE(10,*)C(l,0),P8
ENDDO
CLOSE(10)
ENDIF
ENDDO
END PROGRAM interpolation_errors
And secondly, the output values of "dat" files
!i=2
1.0000000 1.3090918
6.0000000 1.3090918
11.000000 1.3090918
!i=6
1.6666666 0.93107301
3.3333333 0.93107301
5.0000000 0.93107301
6.6666665 0.93107301
8.3333330 0.93107301
10.000000 0.93107301
11.666667 0.93107301
!i=8
1.0000000 1.5395851
2.2500000 1.5395851
3.5000000 1.5395851
4.7500000 1.5395851
6.0000000 1.5395851
7.2500000 1.5395851
8.5000000 1.5395851
9.7500000 1.5395851
11.000000 1.5395851
Looking forward to your reply...
我来回复