GO TO statements- Fortran to Matlab

筅森魡賤 提交于 2021-02-05 12:15:18

问题


I have been trying really hard to convert this grid search code from Fortran to Matlab, however I am not able to properly incorporate the GO TO statements (I am trying to use while loops but I think I need something else for them end the search). Any help would be greatly appreciated.

vmax    = -1.0E+15           
amax_G  = -1

askipR  = REAL(ac_max - ac_min)/REAL(intA)

askip   = CEILING(askipR)


DO acc0 = 1,intA+1  

acc = ac_min + askipR * (acc0-1)  

    cons = ( x1 - grida2(acc) ) / onetauc

    IF (cons<0.0) GOTO 102

    vtemp = utilR(cons) + one_sv * utilB(acc)

    IF (vtemp>vmax) THEN      
        vmax = vtemp
        amax_G = acc
    ELSE
        GOTO 102
    ENDIF

ENDDO ! acc0


102 continue

IF (askip<2) GO TO 109

askip = askip/2

IF (amax_G>ac_min) THEN  

    acc = amax_G - askip
    cons = ( x1 - grida2(acc) ) / onetauc

    IF (cons<0.0) PRINT *,'WARNING: NEGATIVE CONS @ ASEARCH_Rx'

    vtemp = utilR(cons) + one_sv * utilB(acc)

    IF (vtemp>vmax) THEN
        vmax   = vtemp
        amax_G = acc
        GOTO 102
    ENDIF

ENDIF 


IF (amax_G < ac_max) THEN  

    acc  = amax_G + askip
    cons = ( x1 - grida2(acc) ) / onetauc

    IF (cons<0.0) GO TO 102

    vtemp = utilR(cons) + one_sv * utilB(acc)

    IF (vtemp>vmax) THEN
        vmax = vtemp
        amax_G = acc
    ENDIF

ENDIF 

GOTO 102

109 CONTINUE

vfunR(jc,ac,sc)   = vmax    ! jc=Nj

afunR_G(jc,ac,sc) = amax_G  ! jc=Nj

回答1:


You have two types of control-flow implemented with your GOTO statement:

Type 1: breaking out of a loop

The GOTO statement is used to break out of the DO-loop:

    DO acc0 = 1,intA+1  
        ! some code
        IF (cons<0.0) GOTO 102
        ! some more code
    END DO
102 continue

As you notice, if cons < 0.0 the GOTO statement states to move to label 102 which is located just outside of the DO-loop. In matlab is nothing more than a simple break in a for-loop:

for acc0=1:intA+1
  % some matlab code
  if (cons < 0.0)
    break
  end
  % some more matlab code
end

Type 2: creating a loop

While the loop is not explicitly written, the following code creates something that can be translated as a while loop:

    ! This is the start of the loop
102 continue
    ! This is the condition to exit the loop <<==
    IF (askip<2) GO TO 109
    ! stuff
    ! This is a condition to restart the loop    <<==
    IF (vtemp>vmax) THEN
         vmax   = vtemp
         amax_G = acc
         GOTO 102
    ENDIF
    ! stuff
    ! This is another condition to restart the loop   <<==
    IF (cons<0.0) GO TO 102
    ! stuff
    ! This is the end of the loop, asked to restart it  <<==
    GOTO 102
    ! This is outside of the loop
109 CONTINUE

In the end, you translate this as:

while (askip >=2)
   % stuff
   if (vtemp > vmax)
        vmax = vtemp
        amax_G = acc
        continue
   end
   % stuff
   if (cons < 0.0)
      continue
   end
   % stuff
end


来源:https://stackoverflow.com/questions/55572284/go-to-statements-fortran-to-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!