我试图了解以下代码中的索引是如何工作的。
i=1
j=NTAB ! setting up for binary search
DO
k=(i+j)/2 ! integer division
IF (h < htab(k)) THEN
j=k
ELSE
i=k
END IF
IF (j <= i+1) EXIT
END DO
我觉得上面的代码片段应该是一种循环,但是我不明白它是如何工作的。它以i = 1和j = 8开始,导致k = 4.5,导致在htab数组中第4.5个索引中进行引用,但这对我没有任何意义。我正在尝试将此程序片段转换为Matlab,但我不知道该怎么做。有人可以告诉我这段代码在做什么,还是将一小部分代码翻译成Matlab或Python?
下面是上面的摘录来自的程序,因此您可以在DO命令中查看引用。
!+
SUBROUTINE Atmosphere(alt, sigma, delta, theta)
! -------------------------------------------------------------------------
! PURPOSE - Compute the properties of the 1976 standard atmosphere to 86 km.
! AUTHOR - Ralph Carmichael, Public Domain Aeronautical Software
! NOTE - If alt > 86, the values returned will not be correct, but they will
! not be too far removed from the correct values for density.
! The reference document does not use the terms pressure and temperature
! above 86 km.
IMPLICIT NONE
!============================================================================
! A R G U M E N T S |
!============================================================================
REAL,INTENT(IN):: alt ! geometric altitude, km.
REAL,INTENT(OUT):: sigma ! density/sea-level standard density
REAL,INTENT(OUT):: delta ! pressure/sea-level standard pressure
REAL,INTENT(OUT):: theta ! temperature/sea-level standard temperature
!============================================================================
! L O C A L C O N S T A N T S |
!============================================================================
REAL,PARAMETER:: REARTH = 6369.0 ! radius of the Earth (km)
REAL,PARAMETER:: GMR = 34.163195 ! hydrostatic constant
INTEGER,PARAMETER:: NTAB=8 ! number of entries in the defining tables
!============================================================================
! L O C A L V A R I A B L E S |
!============================================================================
INTEGER:: i,j,k ! counters
REAL:: h ! geopotential altitude (km)
REAL:: tgrad, tbase ! temperature gradient and base temp of this layer
REAL:: tlocal ! local temperature
REAL:: deltah ! height above base of this layer
!============================================================================
! L O C A L A R R A Y S ( 1 9 7 6 S T D. A T M O S P H E R E ) |
!============================================================================
REAL,DIMENSION(NTAB),PARAMETER:: htab= &
(/0.0, 11.0, 20.0, 32.0, 47.0, 51.0, 71.0, 84.852/)
REAL,DIMENSION(NTAB),PARAMETER:: ttab= &
(/288.15, 216.65, 216.65, 228.65, 270.65, 270.65, 214.65, 186.946/)
REAL,DIMENSION(NTAB),PARAMETER:: ptab= &
(/1.0, 2.233611E-1, 5.403295E-2, 8.5666784E-3, 1.0945601E-3, &
6.6063531E-4, 3.9046834E-5, 3.68501E-6/)
REAL,DIMENSION(NTAB),PARAMETER:: gtab= &
(/-6.5, 0.0, 1.0, 2.8, 0.0, -2.8, -2.0, 0.0/)
!----------------------------------------------------------------------------
h=alt*REARTH/(alt+REARTH) ! convert geometric to geopotential altitude
i=1
j=NTAB ! setting up for binary search
DO
k=(i+j)/2 ! integer division
IF (h < htab(k)) THEN
j=k
ELSE
i=k
END IF
IF (j <= i+1) EXIT
END DO
tgrad=gtab(i) ! i will be in 1...NTAB-1
tbase=ttab(i)
deltah=h-htab(i)
tlocal=tbase+tgrad*deltah
theta=tlocal/ttab(1) ! temperature ratio
IF (tgrad == 0.0) THEN ! pressure ratio
delta=ptab(i)*EXP(-GMR*deltah/tbase)
ELSE
delta=ptab(i)*(tbase/tlocal)**(GMR/tgrad)
END IF
sigma=delta/theta ! density ratio
RETURN
END Subroutine Atmosphere ! -----------------------------------------------
答案 0 :(得分:2)
正如评论所说,它正在执行二进制搜索。它假定htab中的数据已排序,并且正在寻找与h相同的元素的索引。 i和j是上下限,并且从中间(i + j)/ 2开始。如果该元素大于h,则将当前索引设置为上限并重复循环。否则,它设置该索引的下限并重复循环。如果范围是单个元素(如果没有这样的元素,则可能不是h),IF测试将退出循环。