Fortran Coder

查看: 16321|回复: 16
打印 上一主题 下一主题

[流程控制] fortran循环,求助!!

[复制链接]

17

帖子

5

主题

0

精华

入门

F 币
86 元
贡献
52 点
跳转到指定楼层
#
发表于 2014-4-25 17:58:18 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
我要编一个简单的循环程序,不太会,刚学,求指导,底下是我编的一个模子program main
implicit none
real*8 A,B,C,D,ref,Z0,Zi
real*8 E,H
real*8 daltf,f,s
real*8 A2,A3,A4,As,Al,A1
real,parameter::pi=3.14159
real i
integer,parameter::Zrs=5
integer k
real*8::Zr(Zrs)=(/2.0,4.0,6.0,8.0,10.0/)
common /group1/Zi
common /group2/Z0

do k=1,Zrs
  call Get_ref(Zr(k),E(k),H(k),ref(k))
write(*,*) k,ref(k),daltf/35.0
end do
stop
end
!!!!!!!!!
Z0=50.0
Zi=-3.0
!!!!!!!!
daltf=0  !!频率的变化
do i=0,0.35,0.0001

daltf=daltf+i

end do
write(*,*) daltf
end
stop

As=0.015*(1+daltf/35.0)
A2=0.25*(1+daltf/35.0)
A3=0.0689*(1+daltf/35.0)
A4=0.419*(1+daltf/35.0)
Al=0.4808*(1+daltf/35.0)
!!A1
A1=(1.0/2.0*pi)*atan(1.0/(1.0/tan(2.0*pi*As)+1.0/tan(2.0*pi*Al)))

A=(cos(2.0*pi*A2)+sin(2.0*pi*A2)/tan(2.0*pi*A3))*cos(2.0*pi*A4)-sin(2.0*pi*A2)*sin(2.0*pi*A4)

B=((cos(2.0*pi*A2)+sin(2.0*pi*A2)/tan(2.0*pi*A3))*sin(2.0*pi*A4)+sin(2.0*pi*A2)*cos(2.0*pi*A4))*Z0

C=((-1.0/tan(2.0*pi*A1)*cos(2.0*pi*A2)+sin(2.0*pi*A2)-(1.0/tan(2.0*pi*A1)*sin(2.0*pi*A2)+cos(2.0*pi*A2))/tan(2.0*pi*A3))*cos(2.0*pi*A4)+(1.0/tan(2.0*pi*A1)*sin(2.0*pi*A2)+cos(2.0*pi*A2))*sin(2.0*pi*A4))/Z0

D=(1.0/tan(2.0*pi*A1)*cos(2.0*pi*A2)-sin(2.0*pi*A2)+(1.0/tan(2.0*pi*A1)*sin(2.0*pi*A2)+cos(2.0*pi*A2))/tan(2.0*pi*A3))*sin(2.0*pi*A4)+(1.0/tan(2.0*pi*A1)*sin(2.0*pi*A2)+cos(2.0*pi*A2))*cos(2.0*pi*A4)
!!!E的表达式
subroutine Zr_to_E(Zr,E)
implicit none
real*8 A,B,C,D,E
real*8 Zr,Z0,Zi
common /group1/Zi
common /group2/Z0

E=(A*Zr*(D-C*Zi)+C*Zr*(B+A*Zi))/(((D-C*Zi)**2+Zr**2*C**2)*Z0)

return
end
!!!H的表达式
subroutine Zr_H(Zr,H)
implicit none
real*8 A,B,C,D,Zr,Zi,Z0,H
common /group1/Zi
common /group2/Z0

H=((B+A*Zi)*(D-C*Zi)-A*C*Zr**2)/(((D-C*Zi)**2+Zr**2*C**2)*Z0)

return
end
!!!ref反射系数
subroutine Get_ref(Zr,E,H,ref)
implicit none
real*8 E,H,Zr,ref

call Zr_to_E(Zr,E)
call Zr_H(Zr,H)

ref=((E**2-1+H**2)**2+4*H**2)/(((E+1)**2+H**2)**2)
return
end


分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

17

帖子

5

主题

0

精华

入门

F 币
86 元
贡献
52 点
16#
 楼主| 发表于 2014-4-27 15:19:39 | 只看该作者
fcode 发表于 2014-4-27 15:06
这是你的算法,你比我更清楚。

我没有足够的时间阅读你的代码,并从中理解你的算法,并针对算法给你建议 ...

嗯,特感激,哇哈哈,谢谢你,祝你开心!!!

1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

15#
发表于 2014-4-27 15:06:44 | 只看该作者
ttt 发表于 2014-4-27 14:58
我有两个参数变化呢,daltf, Zr啊,我该怎么做啊

这是你的算法,你比我更清楚。

我没有足够的时间阅读你的代码,并从中理解你的算法,并针对算法给你建议。

我只能从 Fortran语法 角度给你意见。

17

帖子

5

主题

0

精华

入门

F 币
86 元
贡献
52 点
14#
 楼主| 发表于 2014-4-27 14:58:21 | 只看该作者
fcode 发表于 2014-4-27 13:11
Get_A1 就是 Dalt_To_A1 这是你的想法,编译器并不知道你的想法。你缺少 Dalt_To_A1,编译器就不让你运行。 ...

我有两个参数变化呢,daltf, Zr啊,我该怎么做啊

1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

13#
发表于 2014-4-27 13:11:23 | 只看该作者
Get_A1 就是 Dalt_To_A1 这是你的想法,编译器并不知道你的想法。你缺少 Dalt_To_A1,编译器就不让你运行。

E H REF 也一样。

17

帖子

5

主题

0

精华

入门

F 币
86 元
贡献
52 点
12#
 楼主| 发表于 2014-4-27 12:08:24 | 只看该作者
fcode 发表于 2014-4-27 11:21
没有写 DALTF_TO_A1 函数(子程序)

主程序中 E H REF 是什么?如果是数组,要定义,如果是函数(子程序) ...

主程序就是直接调用底下的子程序,你说如果是子程序要写出来,是什么意思啊,Get_A1就是你说的dalt_to_A1,我还觉得挺清晰的,好吧,谢谢你的指导,相当感激

1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

11#
发表于 2014-4-27 11:21:29 | 只看该作者
没有写 DALTF_TO_A1 函数(子程序)

主程序中 E H REF 是什么?如果是数组,要定义,如果是函数(子程序),要写出来。

我感觉你的函数命名很乱

17

帖子

5

主题

0

精华

入门

F 币
86 元
贡献
52 点
10#
 楼主| 发表于 2014-4-27 10:35:31 | 只看该作者

各种感激不尽,改完之后出现这种错误是怎么回事啊
Linking...
tu11.obj : error LNK2001: unresolved external symbol _E@4
tu11.obj : error LNK2001: unresolved external symbol _H@4
tu11.obj : error LNK2001: unresolved external symbol _REF@4
tu11.obj : error LNK2001: unresolved external symbol _DALTF_TO_A1@8
Debug/tu11.exe : fatal error LNK1120: 4 unresolved externals
Error executing link.exe.

程序是这样的

[Fortran] 纯文本查看 复制代码
program main
implicit none
real*8 A,B,C,D,ref,Z0,Zi
real*8 E,H
real*8 f,s
real*8 A2,A3,A4,As,Al,A1
real,parameter::pi=3.14159
integer,parameter::Zrs=5
integer k
real*8::Zr(Zrs)=(/2.0,4.0,6.0,8.0,10.0/)
common /group1/Zi
common /group2/Z0
real::daltf=0.0
integer i
!!!!!!!!!
Z0=50.0
Zi=-3.0
!!!!!!!!
do i=1,int(0.35/0.001)
  do k=1,Zrs
 daltf=daltf+0.001
  call Get_ref(Zr(k),E(k),H(k),ref(k))
write(*,*) k,ref(k)
write(*,*) daltf

end do
end do
stop
end
!!!!!!!As,A2,A3,A4,Al*************************
subroutine daltf_to_As(daltf,As)
implicit none
real*8 daltf,As
As=0.015*(1+daltf/35.0)
return
end

subroutine daltf_to_A2(daltf,A2)
implicit none
real*8 daltf,A2
A2=0.25*(1+daltf/35.0)
return
end

subroutine daltf_to_A3(daltf,A3)
implicit none
real*8 daltf,A3
A3=0.0689*(1+daltf/35.0)
return
end

subroutine daltf_to_A4(daltf,A4)
implicit none
real*8 daltf,A4
A4=0.419*(1+daltf/35.0)
return
end

subroutine daltf_to_Al(daltf,Al)
implicit none
real*8 daltf,Al
Al=0.4808*(1+daltf/35.0)
return
end
!!!!!!!!****************************************
!!A1,A,B,C,D************************************
subroutine Get_A1(daltf,As,Al,A1)
implicit none
real*8 As,Al,A1,daltf
real,parameter::pi=3.14159

call daltf_to_As(daltf,As)
call daltf_to_A1(daltf,Al)

A1=(1.0/2.0*pi)*atan(1.0/(1.0/tan(2.0*pi*As)+1.0/tan(2.0*pi*Al)))
return
end

subroutine Get_AB(daltf,A2,A3,A4,A,B)
implicit none
real*8 A2,A3,A4,A,B,Z0,daltf
real,parameter::pi=3.14259
common /group2/Z0

call daltf_to_A2(daltf,A2)
call daltf_to_A3(daltf,A3)
call daltf_to_A4(daltf,A4)

A=(cos(2.0*pi*A2)+sin(2.0*pi*A2)/tan(2.0*pi*A3))*cos(2.0*pi*A4)-sin(2.0*pi*A2)*sin(2.0*pi*A4)
B=((cos(2.0*pi*A2)+sin(2.0*pi*A2)/tan(2.0*pi*A3))*sin(2.0*pi*A4)+sin(2.0*pi*A2)*cos(2.0*pi*A4))*Z0
return
end

subroutine Get_CD(daltf,A1,A2,A3,A4,C,D)
implicit none
real*8 A1,A2,A3,A4,Z0,C,D,daltf,Al,As
real,parameter::pi=3.14259
common /group2/Z0

call Get_A1(daltf,As,Al,A1)
call daltf_to_A2(daltf,A2)
call daltf_to_A3(daltf,A3)
call daltf_to_A4(daltf,A4)

C=((-1.0/tan(2.0*pi*A1)*cos(2.0*pi*A2)+sin(2.0*pi*A2)-(1.0/tan(2.0*pi*A1)*sin(2.0*pi*A2)+cos(2.0*pi*A2))/tan(2.0*pi*A3))*cos(2.0*pi*A4)+(1.0/tan(2.0*pi*A1)*sin(2.0*pi*A2)+cos(2.0*pi*A2))*sin(2.0*pi*A4))/Z0

D=(1.0/tan(2.0*pi*A1)*cos(2.0*pi*A2)-sin(2.0*pi*A2)+(1.0/tan(2.0*pi*A1)*sin(2.0*pi*A2)+cos(2.0*pi*A2))/tan(2.0*pi*A3))*sin(2.0*pi*A4)+(1.0/tan(2.0*pi*A1)*sin(2.0*pi*A2)+cos(2.0*pi*A2))*cos(2.0*pi*A4)
return
end
!!!*********************************************
!!!E的表达式
subroutine Get_E(Zr,A1,A2,A3,A4,E)
implicit none
real*8 A,B,C,D,E,A1,A2,A3,A4,daltf
real*8 Zr,Z0,Zi
common /group1/Zi
common /group2/Z0

call Get_AB(daltf,A2,A3,A4,A,B)
call Get_CD(daltf,A1,A2,A3,A4,C,D)

E=(A*Zr*(D-C*Zi)+C*Zr*(B+A*Zi))/(((D-C*Zi)**2+Zr**2*C**2)*Z0)

return
end
!!!H的表达式
subroutine Get_H(Zr,A1,A2,A3,A4,H)
implicit none
real*8 A,B,C,D,Zr,Zi,Z0,H,A1,A2,A3,A4,daltf
common /group1/Zi
common /group2/Z0

call Get_AB(daltf,A2,A3,A4,A,B)
call Get_CD(daltf,A1,A2,A3,A4,C,D)

H=((B+A*Zi)*(D-C*Zi)-A*C*Zr**2)/(((D-C*Zi)**2+Zr**2*C**2)*Z0)

return
end
!!!ref反射系数
subroutine Get_ref(Zr,E,H,ref)
implicit none
real*8 E,H,Zr,ref,A1,A2,A3,A4

call Get_E(Zr,A1,A2,A3,A4,E)
call Get_H(Zr,A1,A2,A3,A4,H)

ref=((E**2-1+H**2)**2+4*H**2)/(((E+1)**2+H**2)**2)
return
end


1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

9#
发表于 2014-4-26 22:51:30 | 只看该作者
问题1:
[Fortran] 纯文本查看 复制代码
!!!!!!!!!
 Z0=50.0
 Zi=-3.0
 !!!!!!!!
 real::daltf=0.0
 integer i

换一下位置
[Fortran] 纯文本查看 复制代码
 real::daltf=0.0
 integer i
!!!!!!!!!
 Z0=50.0
 Zi=-3.0
 !!!!!!!!

Fortran 里的声明语句必须在所有执行语句前面

问题2:
Get_A1 函数有4个参数,但调用时只给了3个


17

帖子

5

主题

0

精华

入门

F 币
86 元
贡献
52 点
8#
 楼主| 发表于 2014-4-26 22:34:43 | 只看该作者
chuxf 发表于 2014-4-25 19:53
1.我建议你写成一个函数,把daltf当做参数传递进去,然后循环调用这个函数既可。
2.这样写不行。要这样循环 ...

那个啥,我改完后有几个错误还是解决不了,麻烦你给指导指导,谢谢
[Fortran] 纯文本查看 复制代码
program main
implicit none
real*8 A,B,C,D,ref,Z0,Zi
real*8 E,H
real*8 f,s
real*8 A2,A3,A4,As,Al,A1
real,parameter::pi=3.14159
integer,parameter::Zrs=5
integer k
real*8::Zr(Zrs)=(/2.0,4.0,6.0,8.0,10.0/)
common /group1/Zi
common /group2/Z0
!!!!!!!!!
Z0=50.0
Zi=-3.0
!!!!!!!!
real::daltf=0.0
integer i
do i=1,int(0.35/0.001)
  do k=1,Zrs
daltf=daltf+0.001
  call Get_ref(Zr(k),E(k),H(k),ref(k))
write(*,*) k,ref(k)
write(*,*) daltf

end do
end do
stop
end
!!!!!!!As,A2,A3,A4,Al*************************
subroutine daltf_to_As(daltf,As)
implicit none
real*8 daltf,As
As=0.015*(1+daltf/35.0)
return
end

subroutine daltf_to_A2(daltf,A2)
implicit none
real*8 daltf,A2
A2=0.25*(1+daltf/35.0)
return
end

subroutine daltf_to_A3(daltf,A3)
implicit none
real*8 daltf,A3
A3=0.0689*(1+daltf/35.0)
return
end

subroutine daltf_to_A4(daltf,A4)
implicit none
real*8 daltf,A4
A4=0.419*(1+daltf/35.0)
return
end

subroutine daltf_to_Al(daltf,Al)
implicit none
real*8 daltf,Al
Al=0.4808*(1+daltf/35.0)
return
end
!!!!!!!!****************************************
!!A1,A,B,C,D************************************
subroutine Get_A1(daltf,As,Al,A1)
implicit none
real*8 As,Al,A1,daltf
real,parameter::pi=3.14159

call daltf_to_As(daltf,As)
call daltf_to_A1(daltf,Al)

A1=(1.0/2.0*pi)*atan(1.0/(1.0/tan(2.0*pi*As)+1.0/tan(2.0*pi*Al)))
return
end

subroutine Get_AB(daltf,A2,A3,A4,A,B)
implicit none
real*8 A2,A3,A4,A,B,Z0,daltf
real,parameter::pi=3.14259
common /group2/Z0

call daltf_to_A2(daltf,A2)
call daltf_to_A3(daltf,A3)
call daltf_to_A4(daltf,A4)

A=(cos(2.0*pi*A2)+sin(2.0*pi*A2)/tan(2.0*pi*A3))*cos(2.0*pi*A4)-sin(2.0*pi*A2)*sin(2.0*pi*A4)
B=((cos(2.0*pi*A2)+sin(2.0*pi*A2)/tan(2.0*pi*A3))*sin(2.0*pi*A4)+sin(2.0*pi*A2)*cos(2.0*pi*A4))*Z0
return
end

subroutine Get_CD(daltf,A1,A2,A3,A4,C,D)
implicit none
real*8 A1,A2,A3,A4,Z0,C,D,daltf,Al,As
real,parameter::pi=3.14259
common /group2/Z0

call Get_A1(As,Al,A1)
call daltf_to_A2(daltf,A2)
call daltf_to_A3(daltf,A3)
call daltf_to_A4(daltf,A4)

C=((-1.0/tan(2.0*pi*A1)*cos(2.0*pi*A2)+sin(2.0*pi*A2)-(1.0/tan(2.0*pi*A1)*sin(2.0*pi*A2)+cos(2.0*pi*A2))/tan(2.0*pi*A3))*cos(2.0*pi*A4)+(1.0/tan(2.0*pi*A1)*sin(2.0*pi*A2)+cos(2.0*pi*A2))*sin(2.0*pi*A4))/Z0

D=(1.0/tan(2.0*pi*A1)*cos(2.0*pi*A2)-sin(2.0*pi*A2)+(1.0/tan(2.0*pi*A1)*sin(2.0*pi*A2)+cos(2.0*pi*A2))/tan(2.0*pi*A3))*sin(2.0*pi*A4)+(1.0/tan(2.0*pi*A1)*sin(2.0*pi*A2)+cos(2.0*pi*A2))*cos(2.0*pi*A4)
return
end
!!!*********************************************
!!!E的表达式
subroutine Get_E(Zr,A1,A2,A3,A4,E)
implicit none
real*8 A,B,C,D,E,A1,A2,A3,A4
real*8 Zr,Z0,Zi
common /group1/Zi
common /group2/Z0

call Get_AB(A2,A3,A4,A,B)
call Get_CD(A1,A2,A3,A4,C,D)

E=(A*Zr*(D-C*Zi)+C*Zr*(B+A*Zi))/(((D-C*Zi)**2+Zr**2*C**2)*Z0)

return
end
!!!H的表达式
subroutine Get_H(Zr,A1,A2,A3,A4,H)
implicit none
real*8 A,B,C,D,Zr,Zi,Z0,H,A1,A2,A3,A4
common /group1/Zi
common /group2/Z0

call Get_AB(A2,A3,A4,A,B)
call Get_CD(A1,A2,A3,A4,C,D)

H=((B+A*Zi)*(D-C*Zi)-A*C*Zr**2)/(((D-C*Zi)**2+Zr**2*C**2)*Z0)

return
end
!!!ref反射系数
subroutine Get_ref(Zr,E,H,ref)
implicit none
real*8 E,H,Zr,ref,A1,A2,A3,A4

call Get_E(Zr,A1,A2,A3,A4,E)
call Get_H(Zr,A1,A2,A3,A4,H)

ref=((E**2-1+H**2)**2+4*H**2)/(((E+1)**2+H**2)**2)
return
end

以下是错误提示
-------------------Configuration: tu11 - Win32 Debug--------------------
Compiling Fortran...
C:\Program Files\Microsoft Visual Studio\MyProjects\tu11\tu11.f90
C:\Program Files\Microsoft Visual Studio\MyProjects\tu11\tu11.f90(17) : Error: A specification statement cannot appear in the executable section.
real::daltf=0.0
^
C:\Program Files\Microsoft Visual Studio\MyProjects\tu11\tu11.f90(19) : Error: A specification statement cannot appear in the executable section.
integer i
^
C:\Program Files\Microsoft Visual Studio\MyProjects\tu11\tu11.f90(20) : Error: This name does not have a type, and must have an explicit type.   [I]
do i=1,int(0.35/0.001)
---^
C:\Program Files\Microsoft Visual Studio\MyProjects\tu11\tu11.f90(20) : Error: An INTEGER or REAL data type is required in this context.   [I]
do i=1,int(0.35/0.001)
---^
C:\Program Files\Microsoft Visual Studio\MyProjects\tu11\tu11.f90(22) : Error: This name does not have a type, and must have an explicit type.   [DALTF]
daltf=daltf+0.001
-^
Error executing df.exe.

tu11.obj - 5 error(s), 0 warning(s)

17

帖子

5

主题

0

精华

入门

F 币
86 元
贡献
52 点
7#
 楼主| 发表于 2014-4-25 19:59:17 | 只看该作者
chuxf 发表于 2014-4-25 19:53
1.我建议你写成一个函数,把daltf当做参数传递进去,然后循环调用这个函数既可。
2.这样写不行。要这样循环 ...

好,我试试,谢谢你,哇哈哈
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-9 04:33

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表