Prev: hydraulic valve modelling
Next: Fast Fourier Transform
From: Yi on 22 Mar 2010 14:15 Hello all Matlab lovers: I meet some problem when I translate some fortran code into Matlab. In the fortran it is like this: ITER = 0; ...... (very long, around 2000 lines) ...... UMAX = ..... (100) if (UMAX <= 1e-7 or ITER >= 100) then LSTOP =TRUE ITER=ITER+1 GOTO 100 .... How can I translate this? I'm thinking to use "while" loop. I am using the following logic, but it is not working, anyone have some better idea? ITER = 0 while if (UMAX <= 1e-7 or ITER >= 100) .... .... UMAX = ... Iter = Iter + 1 end I feel the command "GOTO" in fortran is difficult to implement in Matlab, by the way, I am using R2009a. Thanks and Best Regards Yi Thanks
From: James Tursa on 22 Mar 2010 14:30 "Yi " <hvachy(a)163.com> wrote in message <ho8c3q$r7r$1(a)fred.mathworks.com>... > Hello all Matlab lovers: > > I meet some problem when I translate some fortran code into Matlab. > > In the fortran it is like this: > > ITER = 0; > ..... (very long, around 2000 lines) > ..... > UMAX = ..... > > (100) if (UMAX <= 1e-7 or ITER >= 100) > > then LSTOP =TRUE > ITER=ITER+1 > > GOTO 100 You don't show enough of the Fortran. Is the ITER = ITER + 1 supposed to be inside the if test shown on line 100 or not? What about the GOTO 100? Is that inside the if test or not? etc. etc. Please show the complete control structure (where exactly are the endif statements, etc). But in general, you are on the right track ... use a while loop to emulate this in MATLAB. James Tursa
From: dpb on 22 Mar 2010 14:32 Yi wrote: > Hello all Matlab lovers: > > I meet some problem when I translate some fortran code into Matlab. > > In the fortran it is like this: > > ITER = 0; > ..... (very long, around 2000 lines) > ..... > UMAX = ..... > > (100) if (UMAX <= 1e-7 or ITER >= 100) > then LSTOP =TRUE > ITER=ITER+1 > > GOTO 100 > ... > > > How can I translate this? > I'm thinking to use "while" loop. > I am using the following logic, but it is not working, anyone have some > better idea? > > ITER = 0 > while if (UMAX <= 1e-7 or ITER >= 100) ... > ... > UMAX = ... > Iter = Iter + 1 > end > > > I feel the command "GOTO" in fortran is difficult to implement in > Matlab, by the way, I am using R2009a. .... Yes, it can be difficult to recode existing code; I think it a case of "throwing the baby out w/ the bath water" in the decision to not incorporate it at all in the language specifications but to leave it to the user to make wise use of the language features. But, then again, they didn't ask me... :( It looks like your solution should be ok excepting you do not want the "if" in the while condition and you'll want to reverse the logic to look at the continuing condition instead of stopping as it's currently written. Note this also means turning the condition into an "AND" instead of "OR". You'll also need to ensure the initial conditions are satisfied to make the while (UMAX > 1e-7 & ITER < 100) Example for study... >> iter=1; >> umax=10; >> while iter<10 & umax>0.3, iter=iter+1,umax=umax/10, end iter = 2 umax = 1 iter = 3 umax = 0.1000 >> --
From: Michael Hosea on 22 Mar 2010 15:05 I agree. When there is more than one exit criterion, you sometimes have different things to do after exiting the loop. For that, I employ Booleans (logicals). ITER = 0; 100 CONTINUE IF (ITER.EQ.MAXIT) GOTO 200 blah, blah, blah IF (ABS(DELTA).LT.TOL) GOTO 300 ITER = ITER + 1 GOTO 100 200 CONTINUE PRINT *,'Maximum iterations exceeded.' 300 CONTINUE I might write that as goto200 = false; iter = 0; while ~(abs(delta)<tol) if iter == maxit goto200 = true; break end blah, blah, blah iter = iter + 1; end if goto200 fprintf(1,'Maximum iterations exceeded.\n'); end Of course if the program just ends there, you don't need to simulate the GOTO, you could simply have put the fprintf inside the if block inside the loop, but I'm trying to describe a general approach that works when GOTO's are used in non-trivial ways. Sometimes with nested loops, the CONTINUE command in MATLAB is also useful. Point is, both BREAK and CONTINUE imply goto-like jumps in MATLAB execution, so with care and with some Boolean variables you can handle a lot of cases in FORTRAN. -- Mike "dpb" <none(a)non.net> wrote in message news:ho8d6n$td0$1(a)news.eternal-september.org... > Yi wrote: >> Hello all Matlab lovers: >> >> I meet some problem when I translate some fortran code into Matlab. >> >> In the fortran it is like this: >> >> ITER = 0; >> ..... (very long, around 2000 lines) >> ..... >> UMAX = ..... >> >> (100) if (UMAX <= 1e-7 or ITER >= 100) >> then LSTOP =TRUE >> ITER=ITER+1 >> >> GOTO 100 >> ... >> >> >> How can I translate this? >> I'm thinking to use "while" loop. >> I am using the following logic, but it is not working, anyone have some >> better idea? >> >> ITER = 0 >> while if (UMAX <= 1e-7 or ITER >= 100) ... >> ... >> UMAX = ... >> Iter = Iter + 1 >> end >> >> >> I feel the command "GOTO" in fortran is difficult to implement in Matlab, >> by the way, I am using R2009a. > ... > Yes, it can be difficult to recode existing code; I think it a case of > "throwing the baby out w/ the bath water" in the decision to not > incorporate it at all in the language specifications but to leave it to > the user to make wise use of the language features. But, then again, they > didn't ask me... :( > > It looks like your solution should be ok excepting you do not want the > "if" in the while condition and you'll want to reverse the logic to look > at the continuing condition instead of stopping as it's currently written. > Note this also means turning the condition into an "AND" instead of "OR". > > You'll also need to ensure the initial conditions are satisfied to make > the > > while (UMAX > 1e-7 & ITER < 100) > > Example for study... > > >> iter=1; > >> umax=10; > >> while iter<10 & umax>0.3, iter=iter+1,umax=umax/10, end > iter = > 2 > umax = > 1 > iter = > 3 > umax = > 0.1000 > >> > > --
From: benbarrowes on 22 Mar 2010 19:55 On Mar 22, 3:05 pm, "Michael Hosea" <Michael.Ho...(a)mathworks.com> wrote: > I agree. When there is more than one exit criterion, you sometimes have > different things to do after exiting the loop. For that, I employ Booleans > (logicals). > > ITER = 0; > 100 CONTINUE > IF (ITER.EQ.MAXIT) GOTO 200 > blah, blah, blah > IF (ABS(DELTA).LT.TOL) GOTO 300 > ITER = ITER + 1 > GOTO 100 > 200 CONTINUE > PRINT *,'Maximum iterations exceeded.' > 300 CONTINUE > > I might write that as > > goto200 = false; > iter = 0; > while ~(abs(delta)<tol) > if iter == maxit > goto200 = true; > break > end > blah, blah, blah > iter = iter + 1; > end > if goto200 > fprintf(1,'Maximum iterations exceeded.\n'); > end > > Of course if the program just ends there, you don't need to simulate the > GOTO, you could simply have put the fprintf inside the if block inside the > loop, but I'm trying to describe a general approach that works when GOTO's > are used in non-trivial ways. Sometimes with nested loops, the CONTINUE > command in MATLAB is also useful. Point is, both BREAK and CONTINUE imply > goto-like jumps in MATLAB execution, so with care and with some Boolean > variables you can handle a lot of cases in FORTRAN. > -- > Mike > > "dpb" <n...(a)non.net> wrote in message > > news:ho8d6n$td0$1(a)news.eternal-september.org... > > > Yi wrote: > >> Hello all Matlab lovers: > > >> I meet some problem when I translate some fortran code into Matlab. > > >> In the fortran it is like this: > > >> ITER = 0; > >> ..... (very long, around 2000 lines) > >> ..... > >> UMAX = ..... > > >> (100) if (UMAX <= 1e-7 or ITER >= 100) > >> then LSTOP =TRUE > >> ITER=ITER+1 > > >> GOTO 100 > >> ... > > >> How can I translate this? > >> I'm thinking to use "while" loop. > >> I am using the following logic, but it is not working, anyone have some > >> better idea? > > >> ITER = 0 > >> while if (UMAX <= 1e-7 or ITER >= 100) ... > >> ... > >> UMAX = ... > >> Iter = Iter + 1 > >> end > > >> I feel the command "GOTO" in fortran is difficult to implement in Matlab, > >> by the way, I am using R2009a. > > ... > > Yes, it can be difficult to recode existing code; I think it a case of > > "throwing the baby out w/ the bath water" in the decision to not > > incorporate it at all in the language specifications but to leave it to > > the user to make wise use of the language features. But, then again, they > > didn't ask me... :( > > > It looks like your solution should be ok excepting you do not want the > > "if" in the while condition and you'll want to reverse the logic to look > > at the continuing condition instead of stopping as it's currently written. > > Note this also means turning the condition into an "AND" instead of "OR". > > > You'll also need to ensure the initial conditions are satisfied to make > > the > > > while (UMAX > 1e-7 & ITER < 100) > > > Example for study... > > > >> iter=1; > > >> umax=10; > > >> while iter<10 & umax>0.3, iter=iter+1,umax=umax/10, end > > iter = > > 2 > > umax = > > 1 > > iter = > > 3 > > umax = > > 0.1000 > > > -- I have refactored lots of goto's out of fortran on the way to matlab and while and break are my best tools. FYI, have you tried f2maltab at the FEX to help get you >90% into matlab? Ben
|
Pages: 1 Prev: hydraulic valve modelling Next: Fast Fourier Transform |