The pause and return commands of fortran 90 to matlab
Mostra commenti meno recenti
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
Risposta accettata
Più risposte (1)
Image Analyst
il 14 Giu 2014
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 Commento
Image Analyst
il 14 Giu 2014
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.
Categorie
Scopri di più su Fortran with MATLAB in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!