Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran An advanced example in fortran 77

  1. Apr 28, 2016 #1
    Hello
    I have to search for a few solved examples in fortran and understand them so next week i can make a presentation and explain in to the other students. It is a homework for everyone and i dont know which programm i should use. there are few homepages which have many examples but they are so complex i don't understand them at all.
    like this homepage http://people.sc.fsu.edu/~jburkardt/f77_src/f77_src.html

    this page http://web.stanford.edu/class/me200c/tutorial_77/ has a tutorial for fortran 77 and i understand the expressions to point 12 (Arrays in subprograms)
    i would be grateful if you can suggest me a few programms. They should be a little complex with few subroutines,functions, do loops etc.
     
  2. jcsd
  3. Apr 28, 2016 #2

    DrClaude

    User Avatar

    Staff: Mentor

  4. Apr 28, 2016 #3
    thank you very much for your quick answer.
    yes it wasn't really difficult to understand the code. But im studying physics and it would be nice if i get a programm in this topic. Like a complex equation with many variables, Gamma function ( ok this is easy but just an example).
     
  5. Apr 28, 2016 #4

    DrDu

    User Avatar
    Science Advisor

  6. Apr 28, 2016 #5

    jedishrfu

    Staff: Mentor

  7. Apr 28, 2016 #6
    ok thank you very much. Now i will read the book and the homepage and choose the best one for me.
     
  8. Apr 29, 2016 #7
    this book is great but i have problems with the codes.
    the codes are all for FORTRAN 77 right?
    im using MS Developer Studio Fortran PowerStation 4.0.
    when i copy and paste a code i get this errors

    Code (Text):
    libc.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/tese.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    any solution?

    also im not used to this statement
    Code (Fortran):
    do 11 j=1,n
    p3=p2
    p2=p1
    p1=((2.d0*j-1.d0)*z*p2-(j-1.d0)*p3)/j
    enddo 11
    when i use this code i get this
    Code (Text):
    :\Users\HomeD\Desktop\tese\Text2.f90(26): error FOR3852: syntax error detected between END DO and 11
    C:\Users\HomeD\Desktop\tese\Text2.f90(37): error FOR3852: syntax error detected between END DO and 12
    C:\Users\HomeD\Desktop\tese\Text2.f90(39): error FOR3587: unterminated DO with label 12
    C:\Users\HomeD\Desktop\tese\Text2.f90(39): error FOR3587: unterminated DO with label 11
    C:\Users\HomeD\Desktop\tese\Text2.f90(39): error FOR3309: undefined label 12
    C:\Users\HomeD\Desktop\tese\Text2.f90(39): error FOR3309: undefined label 11
     
    how to write the column number to solve the errors?

    i only know this statement
    Code (Text):
           Do n Index=m1,m2,m3
           .....
           .....
        n    continue
    and i have to wirte the code in the 7th column and "n" in the 2-6th column
     
  9. Apr 29, 2016 #8

    DrClaude

    User Avatar

    Staff: Mentor

    That's an idosyncracy of the Numerical Recipes book. The authors do not like the do / continue syntax (I don't blame them), but then use an invalid hybrid in case some compilers don't accept the do /enddo construct, and people have to resort to using continues. So, when in the book you see
    Code (Fortran):

          do 11 j=1,n
    C     Some code here
          enddo 11
     
    Replace that by
    Code (Fortran):

          do j=1,n
    C     Some code here
          enddo
     
    or
    Code (Fortran):

          do 11 j=1,n
    C     Some code here
       11 continue
     
     
  10. Apr 29, 2016 #9
    ok thank you very much.i got it.
    I also have the program Silverfrost Plato 4.7
    shall i use this instead of MSDEV?
     
  11. Apr 29, 2016 #10

    DrClaude

    User Avatar

    Staff: Mentor

    I don't use Windows, so I can't help you here. But the error you posted in #7 is a linking error, and what is not found is _main, which should be the main program. Are you compiling a full program (i.e., there is a PROGRAM statement somewhere) or only a subroutine?
     
  12. Apr 29, 2016 #11
    on page 145 there is a code
    Code (Fortran):
    SUBROUTINE gauleg(x1,x2,x,w,n)
    INTEGER n
    REAL x1,x2,x(n),w(n)
    DOUBLE PRECISION EPS
    PARAMETER (EPS=3.d-14) !EPS is the relative precision.
    !Given the lower and upper limits of integration x1 and x2, and given n, this routine returns
    !arrays x(1:n) and w(1:n) of length n, containing the abscissas and weights of the Gauss-
    !Legendre n-point quadrature formula.
    INTEGER i,j,m
    DOUBLE PRECISION p1,p2,p3,pp,xl,xm,z,z1
    !High precision is a good idea for this routine.
    m=(n+1)/2 !The roots are symmetric in the interval, so we
    xm=0.5d0*(x2+x1) !only have to nd half of them.
    xl=0.5d0*(x2-x1)
    do 12 i=1,m !Loop over the desired roots.
    z=cos(3.141592654d0*(i-.25d0)/(n+.5d0))
    !Starting with the above approximation to the ith root, we enter the main loop of refinement by Newton's method.
    1 continue
    p1=1.d0
    p2=0.d0
    do 11 j=1,n !Loop up the recurrence relation to get the Leg
    p3=p2 !endre polynomial evaluated at z.
    p2=p1
    p1=((2.d0*j-1.d0)*z*p2-(j-1.d0)*p3)/j
    enddo 11
    !p1 is now the desired Legendre polynomial. We next compute pp, its derivative, by a standard relation involving also p2, the polynomial of one lower order.
    pp=n*(z*p1-p2)/(z*z-1.d0)
    z1=z
    z=z1-p1/pp !Newton's method.
    if(abs(z-z1).gt.EPS)goto 1
    x(i)=xm-xl*z !Scale the root to the desired interval,and put in its symmetric counterpart.
    x(n+1-i)=xm+xl*z
    w(i)=2.d0*xl/((1.d0-z*z)*pp*pp) !Compute the weight    and its symmetric counterpart.
    w(n+1-i)=w(i)
    enddo 12
    return
    END
    i just copy and paste and add "!" so the comments will not interfere with the code
    i think this is just a subprogram. right? sorry but i started 2 weeks ago learning fortran.
     
  13. Apr 29, 2016 #12

    DrClaude

    User Avatar

    Staff: Mentor

    Right, and that's why the compiler is complaining. If you do not have a main program, the most you can do is create an object file, containing the compiled code. At the linking stage, one or many object files are combined to create one executable file, which has to have a starting point. In C, it is what is inside int main, in Fortran it is what follows PROGRAM.
     
  14. Apr 29, 2016 #13
    but this book contains only the subprogram of all codes. that means that they are all useless for me? all the codes?
     
  15. Apr 29, 2016 #14

    DrClaude

    User Avatar

    Staff: Mentor

    No. It means that there are to be used when writing your own program to solve your own problems. Take the simple example of a sorting routine: it will sort any set of data, but it is useless unless there are data to sort!

    In many cases, you can write a simple program to make use of a routine. If you take something simple, like the Gamma function, just write a few lines that call the subroutine for different values of the input and then prints the value of Gamma.
     
  16. Apr 29, 2016 #15
    ok i will try this. i will ask you if i get problems again. hope this is ok with you.
     
  17. Apr 29, 2016 #16

    DrClaude

    User Avatar

    Staff: Mentor

    That's what PF is for :smile:
     
  18. Apr 30, 2016 #17
    i solved some examples using function and subroutines.
    the homepage http://rosettacode.org/wiki/Category:Fortran has a code to solve the roots of a quadratic equation. I understand the most. Now i want to improve the code and let it solve the roots of quadratic and cubic equation. I searched for a code for the cubic equation but didn't find one. only this http://jean-pierre.moreau.pagesperso-orange.fr/Fortran/croot3_f90.txt but thats not what im searching for. Also it has integrated values so it is useless as a calculator.
    if you have a code that finds the roots of a cubic equation, can you please share it with me.


    edit: i just found this code on the homepage http://jean-pierre.moreau.pagesperso-orange.fr/Fortran/root4_f90.txt it looks a bit difficult but i will try to understand it. What do you think about this code? can a beginner understand it?
     
    Last edited: Apr 30, 2016
  19. Apr 30, 2016 #18

    jedishrfu

    Staff: Mentor

  20. May 1, 2016 #19
    excuse me but i really need your help. On Wednesday i have to find a FORTRAN code and it should be a little bit complex. This will be like an exam and i must be as good as possible.
    i have this code http://jean-pierre.moreau.pagesperso-orange.fr/Fortran/root4_f90.txt but it is so difficult for me
    it has an explanation for it http://jean-pierre.moreau.pagesperso-orange.fr/Cplus/root4.txt
    but still difficult for me.
    now i ask you if you can explain me the programm ( i don't know the most statements. they are all new for me) or if it takes to much time, can you suggest me another code please.
     
  21. May 1, 2016 #20

    jedishrfu

    Staff: Mentor

    Your request isn't reasonable. Try reading and commenting the program yourself and work through the logic.

    This is what real programmers do all the time. Only in this way will you understand it and be able to explain it to your teacher or someone else.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: An advanced example in fortran 77
  1. Fortran 77 (Replies: 1)

Loading...