An advanced example in fortran 77

In summary: SUBROUTINE gaulegThis routine takes two double precision arguments, x1 and x2. The routine will find the Legendre polynomial z(x) given the lower and upper limits of integration x1 and x2, and the desired precision (EPS). The routine will also return arrays x(1:n) and w(1:n) of length n containing the abscissas and weights of the Gauss-Legendre n-point quadrature formula. The routine will start with the approximation for the ith root z=cos(3.141592654d0*(i-.25d0)/(n+.5d0)) and refine the polynomial
  • #1
Pual Black
92
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 into the other students. It is a homework for everyone and i don't 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.
 
Technology news on Phys.org
  • #2
  • Like
Likes Pual Black
  • #3
thank you very much for your quick answer.
yes it wasn't really difficult to understand the code. But I am 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).
 
  • #6
ok thank you very much. Now i will read the book and the homepage and choose the best one for me.
 
  • #7
DrDu said:
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 I am 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
Pual Black said:
also I am 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
 
  • Like
Likes Pual Black
  • #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?
 
  • #10
Pual Black said:
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?
 
  • Like
Likes Pual Black
  • #11
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
Pual Black said:
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.
 
  • Like
Likes Pual Black
  • #13
but this book contains only the subprogram of all codes. that means that they are all useless for me? all the codes?
 
  • #14
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.
 
  • Like
Likes Pual Black
  • #15
ok i will try this. i will ask you if i get problems again. hope this is ok with you.
 
  • #16
Pual Black said:
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:
 
  • Like
Likes Pual Black
  • #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 that's not what I am 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:
  • #18
Here's an article that shows the roots from a mathematical sense that you could convert yourself into a fortran subroutine:

http://www.aip.de/groups/soe/local/numres/bookfpdf/f5-6.pdf
 
  • Like
Likes Pual Black
  • #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.
 
  • #20
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
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
Pual Black said:
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.
 
  • Like
Likes Pual Black
  • #23
ok thank you all. Now i fully understand the code.
 

1. What is Fortran 77?

Fortran 77 (or simply Fortran) is a programming language commonly used for scientific and numerical computing. It was first introduced in 1957 and has undergone several revisions since then. It is known for its efficient handling of mathematical operations and is still widely used in many scientific fields.

2. What makes Fortran 77 an advanced language?

Fortran 77 is considered an advanced language due to its ability to handle complex mathematical operations and its efficient memory management. It also has a large library of built-in functions and subroutines specifically designed for scientific computations.

3. Can you give an example of a program written in Fortran 77?

Sure, here is a simple "Hello World" program written in Fortran 77:

PROGRAM HELLO

WRITE(*,*) 'Hello, world!'

END

4. How is Fortran 77 different from other programming languages?

Fortran 77 is a compiled language, meaning the code is translated into machine code before it is executed. This allows for faster execution and better performance compared to interpreted languages. Fortran 77 also has specific features and syntax for handling complex mathematical operations, making it a popular choice for scientific computing.

5. Is Fortran 77 still relevant in today's programming landscape?

Yes, Fortran 77 is still widely used in scientific and numerical computing, especially in fields such as physics, engineering, and meteorology. It has also been updated and modernized in newer versions, such as Fortran 90 and Fortran 95, to keep up with evolving technology and programming standards.

Similar threads

  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
6K
  • Programming and Computer Science
Replies
5
Views
9K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
8K
  • Programming and Computer Science
Replies
18
Views
6K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top