Program Hello
c..... First example
c..... Just say Hello and quit
print*, 'Hello world !!'
stop
end
|
Vamos a tratar de correr ese programa. Veamos primero en que consiste. La línea mas interesante es:
print*,'Hello world !!'
|
La forma de compilar este programa es:
problemas> f77 hello.f -o hello.x |
Ahora, corremos el programa:
problemas> ./hello.x |
Modifiquemos ahora el programa, y en lugar de imprimir el texto en la pantalla, hagámoslo en un file llamado "hello.txt".
Program Hello
c..... Second example
c.... Say Hello, but now in an output file
c...... open output file
open (unit=10,file='hello.txt',status='new')
write(10,*) 'Hello world !!'
c...... close output file
close (unit=10)
stop
end
|
Hagamos ahora algo un poquito mas útil. Vamos a compilar y ejecutar un programa que calcula el seno de una función. El programa se llama senocalc.f y es asi:
program sin_calc
c..... Calculation of sin(x) for a given x
print*,' Calculation of sin(x)'
c.... reading input
100 print*,' give the value of x (rad):'
read*,x
c..... calculation of sin(x)
y = sin(x)
print*,' x (rad) =:',x,' sin(x)=:',y
c..... finish
print*,' continue? ("no=0" "yes=1") '
read*,icont
if (icont.ne.0) go to 100
stop
end
|
real*8 x,y |
print*,' x (rad) =:',x,' sin(x)=:',y
|
print150,x,y 150 format(2x,'x (rad) =:',f12.8,5x,'sin(x)=:',f12.8) |
program table_sin
c....... This program prints two columns in file "tablesin.dat"
c X: "npoints" angles between 0 and 3*Pi
c Y: sin(X)
c....... variables declaration
implicit none !! this is another way to comment
integer i,npoints
real*8 x,y,Pi,dang
data npoints/50/
c...... output file
open (unit=15,file='tablesin.dat',status='unknown')
c...... calculation of Pi
Pi = 2.0d0*dasin(1.0d0)
c...... calculation of angular step
dang = 3.0d0*Pi/npoints
c...... table generation
x = -dang
do 100 i=1,npoints
x = x + dang
y = sin(x)
write(15,25) x,y
100 continue
25 format(5x,f10.3,3x,f14.4)
c..... close file
close (unit=15)
stop
end
|
problemas> xmgrace tablesin.dat |
y = sin(x) |
y = func_sin(x) |
real*8 function func_sin(x) func_sin = sin(x) return end |
Los resultados estarán en el documento "tablesin2.dat". Se pueden comparar con los resultados anteriores haciendo:
problemas> xmgrace tablesin.dat tablesin2.dat |
La solución a este problema se encuentra en el programa seno3.f. Compará el nuevo programa y notá la modificación. Graficá los resultados nuevos (tablesin3.dat).