simulaci on de procesos f sicos (f sica...
TRANSCRIPT
![Page 1: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/1.jpg)
Simulacion de Procesos Fısicos
(Fısica Computacional).Conceptos de Programacion.
F. A. [email protected]
Departamento de FısicaCUCEI UdeG
2018A
![Page 2: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/2.jpg)
Sistemas Operativos
I ms-dos → windows
I linux → suse; redhat; ubuntu; etc.
I unix → Mac OSx
![Page 3: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/3.jpg)
usuario
aplicacion
sistema operativo
hardware
![Page 4: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/4.jpg)
Lenguajes de Programacon:
I Matlab
I Fortran
I C++
I Phyton
I
I Java
I etc.
![Page 5: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/5.jpg)
MatLab (MATrix LABoratory)The Language of Technical Computing
I High-level language for numerical computation, visualization, andapplication development
I Interactive environment for iterative exploration, design, and problemsolving.
I Mathematical functions for linear algebra, statistics, Fourier analysis,filtering, optimization, numerical integration, and solving ordinarydifferential equations.
I Built-in graphics for visualizing data and tools for creating custom plots
I Development tools for improving code quality and maintainability andmaximizing performance
I Tools for building applications with custom graphical interfaces
I Functions for integrating MATLAB based algorithms with externalapplications and languages such as C, Java, .NET, and Microsoft R© Excel R©
![Page 6: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/6.jpg)
![Page 7: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/7.jpg)
Fortran (Formula Translating System).
is a general-purpose, imperative programming languagethat is especially suited to numeric computation andscientific computing
![Page 8: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/8.jpg)
![Page 9: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/9.jpg)
Declaracion y uso de Variables
![Page 10: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/10.jpg)
Tipos de Variables
I INTEGER: entero
I REAL: real
I COMPLEX: complejo
I LOGICAL: logica (verdadero o falso)
I CHARACTER: caracter
![Page 11: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/11.jpg)
En un programa de fortran, la forma de escribir ladeclaracion de variables es:
program vars
i n t e g e r : : ir e a l : : acomplex : : imgl o g i c a l : : i ocharac t e r ( 1 0 ) : : f echa
i=1a=2.33img=cmplx (1 ,2 )i o=’ true ’f echa = ’2015/01/01 ’
wr i t e (6 ,∗ ) iwr i t e (6 ,∗ ) awr i t e (6 ,∗ ) imgwr i t e (6 ,∗ ) i owr i t e (6 ,∗ ) f echa
end program vars
![Page 12: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/12.jpg)
El resultado que muestra el programa en pantalla es:compu$ ./a.out
12.330000(1.000000,2.000000)T2015/01/01
![Page 13: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/13.jpg)
En un programa de MatLab no es necesario declarar lasvariables.
c l e a r a l li=1a=2.33img=1+2∗ ii o=f a l s ef echa = ’2015/01/01 ’
![Page 14: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/14.jpg)
i =1
a =2.3300
img =
3
i o =0
fecha =2015/01/01
![Page 15: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/15.jpg)
Programas basicos
![Page 16: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/16.jpg)
programa para sumar dos numeros:declarados en el programa.
prog.f
program prog
r e a l : : a , b , c
a=1.2b=3.4
c=a+b
wr i t e (6 ,∗ ) c
end program prog
![Page 17: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/17.jpg)
programa para sumar dos numeros:que pide al usuario.
prog.f
program prog
r e a l : : a , b , c
read (∗ ,∗ ) aread (∗ ,∗ ) b
c=a+b
wr i t e (6 ,∗ ) c
end program prog
![Page 18: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/18.jpg)
programa para sumar dos numeros:cargados de un archivo.
prog.f
program prog
r e a l : : a , b , c
open ( un i t =100 , f i l e =’datos . dat ’ )read (100 ,∗ ) aread (100 ,∗ ) bc l o s e (100)
c=a+b
wr i t e (6 ,∗ ) c
end program prog
archivo de datos datos.dat
10 .2520 .34
![Page 19: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/19.jpg)
programa que ademas salva los datos a un archivo
prog.f
program prog
r e a l : : a , b , c
open ( un i t =100 , f i l e =’ dts . dat ’ )read (100 ,110) aread (100 ,110) bc l o s e (100)
110 format (F7 . 2 )
c=a+b
wr i te (6 ,∗ ) awr i t e (6 ,∗ ) bwr i t e (6 ,∗ ) copen ( un i t =200 , f i l e =’ dts . out ’ )wr i t e (200 ,∗ ) cc l o s e (200)
end program prog
archivo de datos datos.dat
10 .2520 .34
archivo de datos datos.dat
1 10 .2 20 .3 30 .5
![Page 20: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/20.jpg)
Formato para datos de entrada y salida en fortransintaxis:
write/read (UNIT,FORMAT) variables
write/read (UNIT LABEL,FORMAT LABEL) variableslabel format (format-code)
ejemplo:
write(*,900) i,x900 format(I4,F8.3)
![Page 21: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/21.jpg)
A: text stringD: double precision numbers, exponent notationE: real numbers, exponent notationF: real numbers, fixed point formatI: integerX: horizontal skip (space)/: vertical skip (new line)
n FC w.d
![Page 22: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/22.jpg)
indices de variables
![Page 23: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/23.jpg)
Los indices se usan para identificar a un elemento de unarreglo de variables.por ejemplo: Se si define a(1) = 10; a(2) = 20; a(3) = 30 elarreglo es de dimension 1 x 3.Si queremos multiplicar el arreglo de numeros a por 2,tenemos que hacer:b(1) = 2 ∗ a(1)b(2) = 2 ∗ a(2)b(3) = 2 ∗ a(3)para n datos, la forma general es:
b(j) = 2 ∗ a(j) para j = 1, 2, 3, ..., n.
![Page 24: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/24.jpg)
ciclos
![Page 25: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/25.jpg)
Cuando se requiere hacer una tarea de forma repetitivautilizamos un ciclo.
para j=1 hasta nTAREA
termina
En fortrando j=1,n
b(j)=2*a(j)enddo
En MatLabfor j=1:n
b(j)=2*a(j);end
![Page 26: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/26.jpg)
Cuando se requiere hacer una tarea de forma repetitivautilizamos un ciclo.
para j=1 hasta nTAREA
termina
En fortrando j=1,n
b(j)=2*a(j)enddo
En MatLabfor j=1:n
b(j)=2*a(j);end
![Page 27: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/27.jpg)
Cuando se requiere hacer una tarea de forma repetitivautilizamos un ciclo.
para j=1 hasta nTAREA
termina
En fortrando j=1,n
b(j)=2*a(j)enddo
En MatLabfor j=1:n
b(j)=2*a(j);end
![Page 28: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/28.jpg)
program prog
i n t e g e r : : ir e a l : : a , b , ccharac t e r ( 3 0 ) : : headcharac t e r ( 3 0 ) : : basura
open ( un i t =100 , f i l e =’ e s t a c i on . dat ’ )read (100 ,101) headread (100 , ’ (A) ’ ) basuraread (100 ,102) i , a , bc l o s e (100)
101 format (A)102 format ( I1 , F5 . 1 , F6 . 2 )
c=a+b
wr i te (6 ,∗ ) headwr i t e (6 ,∗ ) i , a , b , c
open ( un i t =200 , f i l e =’ e s t a c i on . out ’ )wr i t e (200 ,∗ ) ’ datos de sa l i da ’wr i t e (200 , ’ ( I1 , 3F6 . 2 ) ’ ) i , a , b , cc l o s e (200)
end program prog
![Page 29: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/29.jpg)
program prog
i n t e g e r : : ir e a l : : a , b , ccharac t e r ( 3 0 ) : : headcharac t e r ( 3 0 ) : : basuradimension i ( 5 ) , a ( 5 ) , b (5 ) , c ( j )
open ( un i t =100 , f i l e =’ e s t a c i on1 . dat ’ )read (100 ,101) headread (100 , ’ (A) ’ ) basurado j =1,5read (100 ,102) i ( j ) , a ( j ) , b ( j )enddoc l o s e (100)
101 format (A)102 format ( I1 , F5 . 1 , F6 . 2 )
do j =1,5c ( j )=a ( j )+b( j )enddo
wr i t e (6 ,∗ ) headdo j =1,5wr i t e ( 6 , ’ ( I1 , 3F6 . 2 ) ’ ) i ( j ) , a ( j ) , b ( j ) , c ( j )enddo
open ( un i t =200 , f i l e =’ e s t a c i on1 . out ’ )wr i t e (200 ,201) ’ datos de sa l i da ’do j =1,5wr i t e (200 ,202) i ( j ) , a ( j ) , b ( j ) , c ( j )enddoc l o s e (200)
201 format (A)202 format ( I3 , 3F8 . 4 )
end program prog
![Page 30: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/30.jpg)
Ejemplo. Programa para evaluar y graficar una funcion
![Page 31: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/31.jpg)
funcion.f
program func ion
i n t e g e r : : i , nr e a l a , brea l , dimension ( 1 0 ) : : x , f
a=0. e0b=2. e0n=10dx=(b−a )/n
x(1)=af (1)=x (1 )∗∗2 . e0
do i =2,nx ( i )=a + i ∗dxf ( i )=x( i )∗∗2wr i t e (6 ,∗ ) x ( i ) , f ( i )enddo
wr i t e (6 ,∗ ) xwr i t e (6 ,∗ ) f
end program func ion
![Page 32: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/32.jpg)
funcion.f
program func ion
parameter (n=10)i n t e g e r : : ir e a l a , brea l , dimension (n ) : : x , f
a=0. e0b=2. e0dx=(b−a )/n
x(1)=af (1)=x (1 )∗∗2 . e0
do i =1,nx ( i )=a+i ∗dxf ( i )=x( i )∗∗2enddo
open ( un i t =90, f i l e =’dat . dat ’ )do i =1,nwr i t e (90 ,80) x ( i ) , f ( i )enddoc l o s e (90)
80 format (2 f16 . 4 )
end program func ion
funcion.m
c l e a r a l l
load funcdata . dat
x=funcdata ( : , 1 ) ;f=funcdata ( : , 2 ) ;
c l fp l o t (x , f , ’ o− ’)
0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 20
0.5
1
1.5
2
2.5
3
3.5
4
![Page 33: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/33.jpg)
funcion.f
program func ion
parameter (n=10)i n t e g e r : : ir e a l a , brea l , dimension (n ) : : x , f
a=0. e0b=2. e0dx=(b−a )/n
x(1)=af (1)=x (1 )∗∗2 . e0
do i =1,nx ( i )=a+i ∗dxf ( i )=x( i )∗∗2enddo
open ( un i t =90, f i l e =’dat . dat ’ )do i =1,nwr i t e (90 ,80) x ( i ) , f ( i )enddoc l o s e (90)
80 format (2 f16 . 4 )
end program func ion
funcion.m
c l e a r a l l
load funcdata . dat
x=funcdata ( : , 1 ) ;f=funcdata ( : , 2 ) ;
c l fp l o t (x , f , ’ o− ’)
0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 20
0.5
1
1.5
2
2.5
3
3.5
4
![Page 34: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/34.jpg)
funcion.f
program func ion
parameter (n=10)i n t e g e r : : ir e a l a , brea l , dimension (n ) : : x , f
a=0. e0b=2. e0dx=(b−a )/n
x(1)=af (1)=x (1 )∗∗2 . e0
do i =1,nx ( i )=a+i ∗dxf ( i )=x( i )∗∗2enddo
open ( un i t =90, f i l e =’dat . dat ’ )do i =1,nwr i t e (90 ,80) x ( i ) , f ( i )enddoc l o s e (90)
80 format (2 f16 . 4 )
end program func ion
funcion.m
c l e a r a l l
load funcdata . dat
x=funcdata ( : , 1 ) ;f=funcdata ( : , 2 ) ;
c l fp l o t (x , f , ’ o− ’)
0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 20
0.5
1
1.5
2
2.5
3
3.5
4
![Page 35: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/35.jpg)
sub rutinas
Una subrutina es una parte del codigo de un programa quehace una tarea especıfica y que puede ser utilizada en variasocaciones a lo largo de un programa principal. Puede estar
escrita en el mismo archivo del programa principal, y por logeneral se localizan en la parte inferior, despues que terminael programa principla. Tambien pueden estar en otro archivo,
que por lo general se nombra con el nombre de la subrutinay con la extension propia del lenguaje de programacion.
![Page 36: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/36.jpg)
programas: principal.f ; subrutina rutina.fcompilacion: pgf90 principal.f rutina.f -o principal.out
program p r i n c i p a l
r e a l : : a , b
a=3b=4
c a l l ru t ina (a , b)
end program p r i n c i p a l
subrout ine rut ina (x , y )
r e a l : : x , y , z
z=sq r t (x∗∗2+y∗∗2)
wr i t e (6 ,∗ ) z
re turnend
![Page 37: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/37.jpg)
compu$ pgf90 principal.f rutina.f -o prin.out principal.f:rutina.f:compu$ ./prin.out 5.000000
![Page 38: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/38.jpg)
programas: principal.f ; subrutina rutina.fcompilacion: pgf90 principal.f rutina.f -o principal.out
program p r i n c i p a l
r e a l : : a , b , c
a=3b=4
c a l l ru t ina (a , b , c )c a l l wrt ( c )
end program p r i n c i p a lc −−−
subrout ine wrt ( var )r e a l : : varwr i t e (6 ,∗ ) ’ ’wr i t e (6 ,∗ ) var
c −−−end
subrout ine rut ina (x , y , z )
r e a l : : x , y , z
z=sq r t (x∗∗2+y∗∗2)
wr i t e (6 ,∗ ) z
re turnend
![Page 39: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/39.jpg)
program p r i n c i p a l
parameter (n=10)r e a l : : a , b , x , f
a=0. e0b=10. e0dt=(b−a )/n
open ( un i t =100 , f i l e =’d3 . dat ’ )do i =1,nt=t+dtc a l l eva l ( t , f )
wr i t e (100 ,200) t , fenddo
200 format (2F11 . 4 )
end program p r i n c i p a lC −−−
subrout ine eva l ( t , f )r e a l : : x , f
f =(−9.81/2. e0 )∗ t ∗∗2
returnend
C −−−
program p r i n c i p a l
parameter (n=10)r e a l dt , a , brea l , dimension (n ) : : t , f
a=0. e0b=4. e0dt=(b−a )/n
do i =1,nt ( i )=( i −1)∗dtc a l l eva l ( t ( i ) , f ( i ) )
enddo
open ( un i t =100 , f i l e =’d4 . dat ’ )do i =1,nwr i t e (100 ,200) t ( i ) , f ( i )enddoc l o s e (100)
200 format (2F11 . 4 )
end program p r i n c i p a lC −−−
subrout ine eva l ( t , f )r e a l : : x , f
f =(−9.81/2. e0 )∗ t ∗∗2
returnend
C −−−
![Page 40: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/40.jpg)
archivo de salida funcdata3.dat
1.0000 −9.81002.0000 −39.24003.0000 −88.29004.0000 −156.96005.0000 −245.25006.0000 −353.16007.0000 −480.69008.0000 −627.84009.0000 −794.6100
10.0000 −981.0001
c l e a r a l l
dat=load ( ’ funcdata4 . dat ’ ) ;
t=dat ( : , 1 ) ;f=dat ( : , 2 ) ;
c l f
p l o t ( t , f , ’ .−k ’ , ’ MarkerSize ’ , 2 0 , ’ Linewidth ’ , 2 )
t i t l e ( ’ ca ida l i b r e ’ , ’ FontSize ’ , 1 8 )x l ab e l ( ’ tiempo [ seg ] ’ , ’ FontSize ’ , 1 8 )y l ab e l ( ’ d i s t an c i a [m] ’ , ’ FontSize ’ , 1 8 )
s e t ( gca , ’ FontSize ’ , 1 8 )
g r id on
pr in t −depsc p r i n c i p a l 4
![Page 41: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/41.jpg)
0 2 4 6 8 10−800
−700
−600
−500
−400
−300
−200
−100
0caida libre
tiempo [seg]
dis
tancia
[m
]
![Page 42: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/42.jpg)
CAIDA LIBRE
![Page 43: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/43.jpg)
program caida
parameter (n=10)r e a l dt , to , t fi n t e g e r i
i n c lude ’ ca ida . c ’
vo= 0 . e0yo= 0 . e0
to=0. e0t f =4. e0dt=( t f−to )/n
open ( un i t =90, f i l e =’c . dat ’ )do i =1,n+1
t=(i −1)∗dtc a l l eva l ( t , y , v )wr i t e (90 ,80) t , y , v
enddo80 format (3F10 . 4 )
c l o s e (90)end program caida
C −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−subrout ine eva l ( t , y , v )inc lude ’ ca ida . c ’
g=−9.81e0y=(g /2 . e0 )∗ t∗∗2+vo∗ t+yov=g∗ t + voreturnend
C −−−
CC common blockC
r e a l y , vr e a l vo , yo , dt
common/blk /vo , yo , dt
C −−−−−−−−−−−−−−−−−
caida.c
caida.f
![Page 44: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/44.jpg)
archivo de salida funcdata3.dat
0.0000 0.0000 0.00000.4000 −0.7848 −3.92400.8000 −3.1392 −7.84801.2000 −7.0632 −11.77201.6000 −12.5568 −15.69602.0000 −19.6200 −19.62002.4000 −28.2528 −23.54402.8000 −38.4552 −27.46803.2000 −50.2272 −31.39203.6000 −63.5688 −35.31604.0000 −78.4800 −39.2400
% Graf i ca de l o s datos de ca ida l i b r e% caida . dat% ca l cu l ado s con ca ida . f y ca ida . c
c l e a r a l l
dat=load ( ’ ca ida . dat ’ ) ;
t=dat ( : , 1 ) ;y=dat ( : , 2 ) ;v=dat ( : , 3 ) ;
c l fsubplot 211p lo t ( t , y , ’ k ’ , ’ LineWidth ’ , 2 )t i t l e ( ’ po s i c i on [m] ’ , ’ FontSize ’ , 1 6 )g r id ons e t ( gca , ’ Xtick ’ , [ 0 : 0 . 5 : 4 ] , ’ FontSize ’ , 1 6 )
subplot 212p lo t ( t , v , ’ k ’ , ’ LineWidth ’ , 2 )t i t l e ( ’ ve loc idad [m s ˆ{ −1} ] ’ , ’ FontSize ’ , 1 6 )g r id ons e t ( gca , ’ Xtick ’ , [ 0 : 0 . 5 : 4 ] , ’ FontSize ’ , 1 6 )
x l ab e l ( ’ tiempo [ s ] ’ )drawnow
pr in t −depsc c a i d a f i g
![Page 45: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/45.jpg)
0 0.5 1 1.5 2 2.5 3 3.5 4−80
−60
−40
−20
0posicion [m]
0 0.5 1 1.5 2 2.5 3 3.5 4−40
−30
−20
−10
0velocidad [m s
−1]
tiempo [s]
![Page 46: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/46.jpg)
−1
0
1
2
3
4
5
6
7
8
9
10
![Page 47: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/47.jpg)
−1
0
1
2
3
4
5
6
7
8
9
10 program caida1
r e a l dt , g , tmin t e g e r i ,Nr e a l t , y , v , vo , yo
vo= 0 . e+0yo= 4 . e+1dt= 1 . e−2tm= 14.25+0
N=tm/dt+1
g=−9.81e0
open ( un i t =100 , f i l e =’ ca ida1 . dat ’ )do i =1,N
t=(i −1)∗dty=0.5 e0∗g∗dt∗∗2 + vo∗dt + yov= g∗dt + vo
wr i t e (100 ,200) t , y , v
yo=yvo=v
i f ( y . l t . 0 . e0 ) thenvo=−0.75∗vyo=0. e0
end i f
enddo
200 format (3F10 . 4 )
c l o s e (100)end program caida1
![Page 48: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/48.jpg)
−1
0
1
2
3
4
5
6
7
8
9
10
![Page 49: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/49.jpg)
Manejo de archivos.
![Page 50: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/50.jpg)
![Page 51: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/51.jpg)
![Page 52: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/52.jpg)
![Page 53: Simulaci on de Procesos F sicos (F sica Computacional).fisica.cucei.udg.mx/~fvelazquez/cursos/fiscom/cap1.pdf · 2018-05-04 · open( unit =200, f i l e =’estacion1 . out ’) write](https://reader034.vdocuments.mx/reader034/viewer/2022042918/5f5f2d88f5ec5c73046442d5/html5/thumbnails/53.jpg)