The pause and return commands of fortran 90 to matlab
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi,
I am trying to translate a fortran 90 code into Matlab. I do it solely for my own practice and in order to understand a code provided by academic journal. The particular section, is taken from the well known book of "numerical recipes 77", I slightly modified (simplified the code), to the following form:
PROGRAM PAUSEE
IMPLICIT NONE
real*8:: saf1=10, saf2=-15, tol, r
r = zbrent(saf1,saf2, 10.0215, 20.03256, 0.00003)
print*, 'this is what we get'
print*, r
contains
FUNCTION zbrent (saf1,saf2,x1,x2,tol)
real(kind=1), intent(in) :: x1, x2, tol
integer, parameter:: itmax=300
real*8, parameter:: epsz=3.e-8
integer:: iter
real*8:: a,b,c,d,e,fa,fb,fc,p,q,r,s,tol1,xm, zbrent, saf1,saf2
a=x1
b=x2
fa=saf1
fb=saf2
if(fa*fb>0.) pause 'root must be bracketed for zbrent'
c=b
fc=fb
do iter=1,itmax
if (fb*fc>0.) then
c=a
fc=fa
d=b-a
e=d
end if
if (abs(fc)<abs(fb)) then
a=b
b=c
c=a
fa=fb
fb=fc
fc=fa
end if
tol1=2.*epsz*abs(b)+0.5*tol
xm=.5*(c-b)
if (abs(xm)<=tol1 .or. fb==0.) then
zbrent=b
return
end if
if (abs(e)>=tol1 .and. abs(fa)>abs(fb)) then
s=fb/fa
if(a==c) then
p=2.*xm*s
q=1.-s
else
q=fa/fc
r=fb/fc
p=s*(2.*xm*q*(q-r)-(b-a)*(r-1.))
q=(q-1.)*(r-1.)*(s-1.)
end if
if(p>0.) q=-q
p=abs(p)
if (2.*p<min(3.*xm*q-abs(tol1*q),abs(e*q))) then
e=d
d=p/q
else
d=xm
e=d
end if
else
d=xm
e=d
end if
a=b
fa=fb
if(abs(d)>tol1) then
b=b+d
else
b=b+sign(tol1,xm)
end if
fb=saf2
end do
pause 'zbrent exceeding maximum iterations'
zbrent=b
return
END FUNCTION zbrent
END PROGRAM PAUSEE
My basic questions concerns the the meaning of "pause" and "return" commands (I noticed that in the first use of the pause THERE IS NO "END IF" ) and whether those do have any equivalent in Matlab. I checked and find out, that they do exist such commands but I am not sure whether are the equivalent to fortran. Second, I really wonder whether the "pause" and "return" commands in the code, are of any value if someone translates this code into Matlab. In other words, can the algorithm here go through in matlab without the "pause" and "return" commands of the above code ? I tried a bit myself, with and without the "pause" and "return" commands, for some particular inputs, and seems to produce the same results. However, I am still concerned whether more generally those can be omitted or not.
many thanks
댓글 수: 0
채택된 답변
James Tursa
2014년 6월 14일
The PAUSE statement does just what the name implies, pauses execution with a message until user does something (e.g., presses Enter) to resume execution. Looks like Image Analyst has a good solution for that if you want the same behavior. I might suggest just using a warn( ) instead of a pause and simply let the routine continue on its own.
Regarding your question about a missing "endif", in Fortran, the following syntaxes mean the same thing:
if( condition ) then
statement
endif
if( condition ) statement
That is, you can either bracket the statement with a "then-endif", or you can simply include the statement on the same line without the "then-endif".
The RETURN means exactly the same thing as in MATLAB, simply returning control to the calling routine. In your Fortran above, the RETURN near the end of the zbrent function is simply returning control back to the calling routine, which in this case is PROGRAM PAUSEE.
댓글 수: 3
dpb
2014년 6월 14일
...would you suggest ? to include those equivalents (i.e pause and return) or not...
Well, clearly the warnings are there for a reason; it would be unwise to not make the user aware of an issue. HOW you choose to do that is a design decision. The above ensures there is a response from the user; whether your code is that onerous or not is your choice.
On the IF structure, NB: that the one-line IF in Fortran includes only the one statement on the same line in that form be careful to not accidentally either leave something out or include something that shouldn't be.
James Tursa
2014년 6월 15일
편집: James Tursa
2014년 6월 15일
@Safis: Since you asked, I would replace the pause with either a warn( ) or an error( ) depending on how severe you think the problem is. I would also keep the return statement just because I always explicitly include them in my MATLAB functions, but it is really optional whether you include it or not.
추가 답변 (1개)
Image Analyst
2014년 6월 14일
return is probably the same meaning. It's been about 30 years since I've used Fortran but from the looks of your code:
pause 'root must be bracketed for zbrent'
I'd guess the equivalent in MATLAB would be
uiwait(helpdlg('root must be bracketed for zbrent'));
댓글 수: 1
Image Analyst
2014년 6월 14일
Regarding your latest comment. I would leave the returns in there because that seems to be the normal (no error) mode of exiting this code. For the pause, I'd use my code since it will popup the information in a dialog box like the original FORTRAN code. If you think it should be more of a warning than an information, you can use helpdlg() or msgbox() instead of helpdlg(). Using the uiwait will wait for the user to click on it, otherwise it just throws up the message and continues on with the code.
참고 항목
카테고리
Help Center 및 File Exchange에서 Fortran with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!