An advanced example in fortran 77

  • Fortran
  • Thread starter Pual Black
  • Start date
  • #1
92
1

Main Question or Discussion Point

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.
 

Answers and Replies

  • #2
DrClaude
Mentor
7,142
3,278
  • #3
92
1
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).
 
  • #4
DrDu
Science Advisor
6,023
755
  • #6
92
1
ok thank you very much. Now i will read the book and the homepage and choose the best one for me.
 
  • #7
92
1
You could also have a look at Numerical recipes in Fortran 77:
http://numerical.recipes/oldverswitcher.html
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:
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
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:
:\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:
       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
 
  • #8
DrClaude
Mentor
7,142
3,278
also im not used to this statement
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
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
Fortran:
      do 11 j=1,n
C     Some code here
      enddo 11
Replace that by
Fortran:
      do j=1,n
C     Some code here
      enddo
or
Fortran:
      do 11 j=1,n
C     Some code here
   11 continue
 
  • #9
92
1
ok thank you very much.i got it.
I also have the program Silverfrost Plato 4.7
shall i use this instead of MSDEV?
 
  • #10
DrClaude
Mentor
7,142
3,278
ok thank you very much.i got it.
I also have the program Silverfrost Plato 4.7
shall i use this instead of MSDEV?
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?
 
  • #11
92
1
on page 145 there is a 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.
 
  • #12
DrClaude
Mentor
7,142
3,278
i think this is just a subprogram. right?
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.
 
  • #13
92
1
but this book contains only the subprogram of all codes. that means that they are all useless for me? all the codes?
 
  • #14
DrClaude
Mentor
7,142
3,278
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.
 
  • #15
92
1
ok i will try this. i will ask you if i get problems again. hope this is ok with you.
 
  • #16
DrClaude
Mentor
7,142
3,278
ok i will try this. i will ask you if i get problems again. hope this is ok with you.
That's what PF is for :smile:
 
  • #17
92
1
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:
  • #19
92
1
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.
 
  • #20
11,473
4,996
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.
 
  • #21
92
1
of course you are right but i asked you because i don't have much time.
anyway i find another code and am trying to understand it 100%
there are some statements that i don't understand.

Fortran:
1 !**********************************************
2 !* This program allows user to perform combi- *
3 !* natory analysis, such as Factorial N, *
4 !* Combination C(n,p) and Permutation A(n,p). *
5 !* ------------------------------------------ *
6 !* Ref.: "Mathématiques en Turbo-Pascal By *
7 !* M. Ducamp and A. Reverchon (2), *
8 !* Eyrolles, Paris, 1988". *
9 !* ------------------------------------------ *
10 !* Sample runs: *
11 !* *
12 !* COMBINATORY ANALYSIS *
13 !* *
14 !* 1: Factorial n! *
15 !* 2: Combination C n,p *
16 !* 3: Permutation A n,p *
17 !* 0: Quit *
18 !* *
19 !* Your choice (0 to 3): 1 *
20 !* *
21 !* N = 100 *
22 !* N! = 9.3248476268 10^ 157 *
23 !* *
24 !* Your choice (0 to 3): 2 *
25 !* *
26 !* N = 7 *
27 !* P = 3 *
28 !* Cnp = 35 *
29 !* *
30 !* Your choice (0 to 3): 3 *
31 !* *
32 !* N = 10 *
33 !* P = 6 *
34 !* Anp = 151200 *
35 !* *
36 !* F90 version by J-P Moreau *
37 !* (www.jpmoreau.fr) *
38 !**********************************************
39 PROGRAM COMBI_ANALYSIS
40 implicit none    ! Take i,j,k,l,m,n as integer and other as real
41 INTEGER p, ichoice, n
42 real*4 Anp, Cnp, Factorial, R1, r2, s  !real*4 single precision=precision 7digits
43 ichoice=1
44 do while(ichoice.ne.0)
45 print *,''
46 print *,' COMBINATORY ANALYSIS'
47 print *,''
48 print *,' 1: Factorial n!'
49 print *,' 2: Combination C n,p'
50 print *,' 3: Permutation A n,p'
51 print *,' 0: Quit'
52 print *,''
53 write(*,100,advance='no'); read *, ichoice
54 print *,''
55 if (ichoice<0) ichoice=0
56 if (ichoice>3) ichoice=3
57 if (ichoice.eq.0) goto 90 !quit
58 if (ichoice.eq.1) then !Factorial n
59 write(*,110,advance='no'); read *, n
60 s=Factorial(n,r1,r2)
61 if (s>0) then
62 write(*,150) s
63 else
64 write(*,151) r1, r2
65 endif
66 endif
67 if (ichoice.eq.2) then !Combination n,p
68 write(*,110,advance='no'); read *, n
69 write(*,120,advance='no'); read *, p
70 s=Cnp(n,p) !=Anp(n,p)/p!
71 if (s>0.) then
72 write(*,160) s
73 else
74 write(*,161)
75 endif
76 endif
77 if (ichoice.eq.3) then !Permutation n,p
78 write(*,110,advance='no'); read *, n
79 write(*,120,advance='no'); read *, p
80 s=Anp(n,p)
81 if (s>0.) then
82 write(*,170) s
83 else
84 write(*,161)
85 endif
86 endif
87 print *,''
88 Pause ' Hit any key to continue...'
89 enddo
90 90 stop
91
92 100 format(' Your choice (0 to 3): ')
93 110 format(' N = ')
94 120 format(' P = ')
95
96 150 format(' N! =',F12.0)
97 151 format(' N! =',F12.10,' 10^',F4.0)
98
99 160 format(' Cnp =',F12.0)
100 161 format(' Quantity impossible to evaluate')
101
102 170 format(' Anp =',F12.0)
103
104 END
105
106 !Factorial n
107 real*4 Function Factorial(n,mantissa,exponent)
108 implicit none
109 real*4, parameter :: PI=3.1415926535
110 REAL*4 exponent, mantissa, fa
111 integer i, n
112 fa=1; Factorial=0;
113 if (n<25) then  ! i think FORTRAN can't handle such a big number 25! and more
114 do i=1, n
115 fa=fa*i
116 end do
117 Factorial=fa
118 else
119 fa=(LOG(2.*PI*n)/2. + n*LOG(FLOAT(n))-n)/LOG(10.)          
120 exponent=INT(fa); mantissa=EXP((fa-INT(fa))*LOG(10.))
121 endif
122 End
123
124 !Comnination C n,p
125 real*4 Function Cnp(n,p)
126 implicit none
127 INTEGER n, p
128 REAL*4 a, b
129 real*4 Anp, Factorial
130 Cnp=Anp(n,p)/Factorial(p,a,b)
131 End
132
133 !Permutation A n,p
134 Real*4 Function Anp(n,p)
135 implicit none
136 real*4, parameter :: REALMAX=1E30
137 INTEGER n, p, i
138 real*4 r
139 Anp=0.
140 if (p>n) return
141 r=1.
142 do i=n-p+1, n
143 if (r>REALMAX/i) return
144 r=r*i
145 end do
146 Anp=r
147 End
148
149
150 !end of file combi.pas
1-line 113 is FORTRAN unable to handle such a big number?
2-in line 119-120 he uses a formula for approximation. it looks like Stirlings Approximation ##\ln n!=n\, ln\, n - n +\frac{1}{2} \ln(2 \pi n)##
but why does he divide by log 10?
3-line 136 REALMAX=1E30 for what is it useful?
4-line 140 if (p>n) return if not go to next line. but when p>n where does the program go? to line 80?
 
  • #22
SteamKing
Staff Emeritus
Science Advisor
Homework Helper
12,798
1,666
of course you are right but i asked you because i don't have much time.
anyway i find another code and am trying to understand it 100%
there are some statements that i don't understand.

Fortran:
1 !**********************************************
2 !* This program allows user to perform combi- *
3 !* natory analysis, such as Factorial N, *
4 !* Combination C(n,p) and Permutation A(n,p). *
5 !* ------------------------------------------ *
6 !* Ref.: "Mathématiques en Turbo-Pascal By *
7 !* M. Ducamp and A. Reverchon (2), *
8 !* Eyrolles, Paris, 1988". *
9 !* ------------------------------------------ *
10 !* Sample runs: *
11 !* *
12 !* COMBINATORY ANALYSIS *
13 !* *
14 !* 1: Factorial n! *
15 !* 2: Combination C n,p *
16 !* 3: Permutation A n,p *
17 !* 0: Quit *
18 !* *
19 !* Your choice (0 to 3): 1 *
20 !* *
21 !* N = 100 *
22 !* N! = 9.3248476268 10^ 157 *
23 !* *
24 !* Your choice (0 to 3): 2 *
25 !* *
26 !* N = 7 *
27 !* P = 3 *
28 !* Cnp = 35 *
29 !* *
30 !* Your choice (0 to 3): 3 *
31 !* *
32 !* N = 10 *
33 !* P = 6 *
34 !* Anp = 151200 *
35 !* *
36 !* F90 version by J-P Moreau *
37 !* (www.jpmoreau.fr) *
38 !**********************************************
39 PROGRAM COMBI_ANALYSIS
40 implicit none    ! Take i,j,k,l,m,n as integer and other as real
41 INTEGER p, ichoice, n
42 real*4 Anp, Cnp, Factorial, R1, r2, s  !real*4 single precision=precision 7digits
43 ichoice=1
44 do while(ichoice.ne.0)
45 print *,''
46 print *,' COMBINATORY ANALYSIS'
47 print *,''
48 print *,' 1: Factorial n!'
49 print *,' 2: Combination C n,p'
50 print *,' 3: Permutation A n,p'
51 print *,' 0: Quit'
52 print *,''
53 write(*,100,advance='no'); read *, ichoice
54 print *,''
55 if (ichoice<0) ichoice=0
56 if (ichoice>3) ichoice=3
57 if (ichoice.eq.0) goto 90 !quit
58 if (ichoice.eq.1) then !Factorial n
59 write(*,110,advance='no'); read *, n
60 s=Factorial(n,r1,r2)
61 if (s>0) then
62 write(*,150) s
63 else
64 write(*,151) r1, r2
65 endif
66 endif
67 if (ichoice.eq.2) then !Combination n,p
68 write(*,110,advance='no'); read *, n
69 write(*,120,advance='no'); read *, p
70 s=Cnp(n,p) !=Anp(n,p)/p!
71 if (s>0.) then
72 write(*,160) s
73 else
74 write(*,161)
75 endif
76 endif
77 if (ichoice.eq.3) then !Permutation n,p
78 write(*,110,advance='no'); read *, n
79 write(*,120,advance='no'); read *, p
80 s=Anp(n,p)
81 if (s>0.) then
82 write(*,170) s
83 else
84 write(*,161)
85 endif
86 endif
87 print *,''
88 Pause ' Hit any key to continue...'
89 enddo
90 90 stop
91
92 100 format(' Your choice (0 to 3): ')
93 110 format(' N = ')
94 120 format(' P = ')
95
96 150 format(' N! =',F12.0)
97 151 format(' N! =',F12.10,' 10^',F4.0)
98
99 160 format(' Cnp =',F12.0)
100 161 format(' Quantity impossible to evaluate')
101
102 170 format(' Anp =',F12.0)
103
104 END
105
106 !Factorial n
107 real*4 Function Factorial(n,mantissa,exponent)
108 implicit none
109 real*4, parameter :: PI=3.1415926535
110 REAL*4 exponent, mantissa, fa
111 integer i, n
112 fa=1; Factorial=0;
113 if (n<25) then  ! i think FORTRAN can't handle such a big number 25! and more
114 do i=1, n
115 fa=fa*i
116 end do
117 Factorial=fa
118 else
119 fa=(LOG(2.*PI*n)/2. + n*LOG(FLOAT(n))-n)/LOG(10.)         
120 exponent=INT(fa); mantissa=EXP((fa-INT(fa))*LOG(10.))
121 endif
122 End
123
124 !Comnination C n,p
125 real*4 Function Cnp(n,p)
126 implicit none
127 INTEGER n, p
128 REAL*4 a, b
129 real*4 Anp, Factorial
130 Cnp=Anp(n,p)/Factorial(p,a,b)
131 End
132
133 !Permutation A n,p
134 Real*4 Function Anp(n,p)
135 implicit none
136 real*4, parameter :: REALMAX=1E30
137 INTEGER n, p, i
138 real*4 r
139 Anp=0.
140 if (p>n) return
141 r=1.
142 do i=n-p+1, n
143 if (r>REALMAX/i) return
144 r=r*i
145 end do
146 Anp=r
147 End
148
149
150 !end of file combi.pas
1-line 113 is FORTRAN unable to handle such a big number?
The REAL data type in Fortran should be able to handle floating point numbers with magnitudes of about 10-38 to 1038, which is equivalent to about 34!.
2-in line 119-120 he uses a formula for approximation. it looks like Stirlings Approximation ##\ln n!=n\, ln\, n - n +\frac{1}{2} \ln(2 \pi n)##
but why does he divide by log 10?
To convert the Natural Log into a Common Log (or LOG10). Remember the Laws of Logarithms!
3-line 136 REALMAX=1E30 for what is it useful?
I think the Programmer is trying to keep his calculations < 25!.
4-line 140 if (p>n) return if not go to next line. but when p>n where does the program go? to line 80?
Item 4: When the RETURN statement is executed, at Line 140 or Line 143, control passes back to the program unit which called Function ANP. Function ANP appears in both Function CNP and the Main Program, so it's not clear exactly where execution will resume, unless you do a trace on the program.
 
  • #23
92
1
ok thank you all. Now i fully understand the code.
 

Related Threads for: An advanced example in fortran 77

  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
1
Views
4K
  • Last Post
Replies
7
Views
5K
Replies
8
Views
2K
Replies
2
Views
2K
Replies
3
Views
596
  • Last Post
Replies
2
Views
2K
Replies
4
Views
1K
Top