Upload
others
View
20
Download
0
Embed Size (px)
Citation preview
Simulacion de Procesos Fısicos
(Fısica Computacional).Herramientas Matematicas.
F. A. [email protected]
Departamento de FısicaCUCEI UdeG
2018A
Derivacion Numerica
La derivada de primer orden de una funcion de una variablef = f(x) en el punto x = a es:
f ′(a) = lımh→0
f(a+ h)− f(a)
h(1)
f ′(a) = lımh→0
f(a)− f(a− h)
h(2)
f ′(a) = lımh→0
f(a+ h)− f(a− h)
2h(3)
donde h = ∆x
La derivada de segundo orden de una funcion de unavariable f = f(x) en el punto x = a es:
f ′′(a) = lımh→0
f(a+ h)− 2f(a) + f(a− h)
h2(4)
f ′′(a) = lımh→0
f(a+ 2h)− 2f(a+ h) + f(a)
h2(5)
0 0.2 0.4 0.6 0.8 1−0.2
0
0.2
0.4
0.6
0.8
1
(a)
0 0.2 0.4 0.6 0.8 1−0.2
0
0.2
0.4
0.6
0.8
1
(b)
Figura : (a) aproximacion de la derivada con (a) h→ π/4 y (b)con h→ 0
numericamente tenemos que el esquema de diferenciacion es
xp(k) =x(k + 1)− x(k)
dx(6)
para k = 1, 2, ..., N − 1, donde x(k) son datos de una serie ovalores de una funcion, y xp(k), los valores aproximados desu derivada.
ejemplo:
considere valores de f = sin(x) para x = [0, 2π].
a) calcule la funcion f ′ = dfdx
.
b) calcula la aproximacion de f ′ con diferencias:adelantadas, centradas y atrasadas.
Diagrama de flujo:
I Entradas
I Variables
I Proceso
I Salida
I Visualizacion
programa para calcular la derivada numerica de la funcionf(x) = sin(x).
program numdiff
parameter (n=20)i n t e g e r ir e a l : : x (n ) , y (n ) , f (n ) , yp (n ) , dx
dx=2∗3.1416/(n−1)
do i =1,nx ( i )=( i −1)∗dxy ( i )= s in (x ( i ) )f ( i )=cos (x ( i ) )
enddo
yp(1)=(y(2)−y (1 ) )/ dxdo i =2,n−1
yp ( i )=(y ( i+1)−y ( i −1))/(2 . e0∗dx )enddoyp (n)=(y (n)−y (n−1))/dx
open ( un i t =100 , f i l e =’numdiff . dat ’ )do i =1,n
wr i t e (100 ,200) x ( i ) , y ( i ) , yp ( i ) , f ( i )enddo
200 format ( ’4 F10 . 4 ’ )c l o s e (100)
end
Programa en Matlab para graficar la funcion sin(x), suderivada analıtica cos(x) y la aproximacion a la derivadapor diferencias finitas.c l e a r a l l
dat=load ( ’ numdiff . dat ’ ) ;
c l fsubplot 211p lo t ( dat ( : , 1 ) / pi , dat ( : , 2 ) , ’ . − k ’ )g r id ons e t ( gca , ’ FontSize ’ , 1 6 )s e t ( gca , ’ Xtick ’ , [ 1 2 ] )ax i s ([−0 2 −1 1 ] )t ext ( 0 . 2 5 , 0 . 5 , ’ f ( x ) ’ , ’ FontSize ’ , 1 8 )
subplot 212p lo t ( dat ( : , 1 ) / pi , dat ( : , 4 ) , ’ . − k ’ )hold onp lo t ( dat ( : , 1 ) / pi , dat ( : , 3 ) , ’ o−k ’ )g r id ons e t ( gca , ’ FontSize ’ , 1 6 )s e t ( gca , ’ Xtick ’ , [ 1 2 ] )ax i s ([−0 2 −1 1 ] )t ext ( 0 . 4 , 0 . 5 , ’ df ( x )/dx ’ , ’ FontSize ’ , 1 8 )
drawnow
pr in t −depsc numdiff2
N = 10, ∆x = 2π/10
1 2−1
−0.5
0
0.5
1
f(x)
1 2−1
−0.5
0
0.5
1
df(x)/dx
N = 20, ∆x = 2π/20
1 2−1
−0.5
0
0.5
1
f(x)
1 2−1
−0.5
0
0.5
1
df(x)/dx
Integracion Numerica
Integracion por area del rectangulo bajo la curva
a b
f(a)
f(b)
∫f(x)dx = lım
n→∞
n∑i=1
f(xi)∆x
donde ∆x = b− a
Integracion por area del rectangulo bajo la curva
a b
f(a)
f(b)
∫f(x)dx ≈ f(a)(b− a)
Integracion por area del rectangulo bajo la curva
x1
y1
x2
y2
x3
y3
x4
y4
x5∫
f(x)dx ≈n−1∑i=1
f(xi)∆x
donde ∆x = x2 − x1 = x3 − x2 = ... = xn − xn−1
Metodo del Trapecio (rectangulo + triangulo)
a b
f(a)
f(b)
∫f(x)dx ≈ f(a)(b− a) +
1
2(f(b)− f(a)) (b− a)
∫f(x)dx ≈
[1
2f(a) +
1
2f(b)
](b− a)∫
f(x)dx ≈ 1
2[f(a) + f(b)] (b− a)
Metodo del trapecio
x1
y1
x2
y2
x3
y3
x4
y4
x5
∫f(x) ≈ 1
2f(x1)dx+
1
2f(x2)∆x+ (7)
1
2f(x2)dx+
1
2f(x3)∆x+ (8)
... (9)1
2f(xn−1)dx+
1
2f(xn)∆x
Metodo del trapecio
x1
y1
x2
y2
x3
y3
x4
y4
x5
∫f(x) ≈
[1
2f(x1) +
n−1∑i=2
f(xi) +1
2f(xn)
]∆x
Metodo de la regla de Simpson
−dx 0 +dx
f(−dx)
f(0)f(+dx)
∫f(x) ≈ ∆x
3[f(−dx) + 4f(0) + f(dx)]∫
f(x) ≈ ∆x
3[f(x1) + 4f(x2) + f(x3)]
Metodo de la regla de Simpson
x1 x
2 x
3
y1
y2
y3
xi−1
xi x
i+1
yi−1
yi y
i+1
xn−2
xn−1
xn
yn−2y
n−1 yn
∫f(x) ≈ ∆x
3
f(x1) +
n/2∑i=2
(4f(x2i) + 2f(x2i+1)) + f(xn)
Aplicaciones
Aproximacion de la ecuacion de caida libre
d2y
dt2= g
sujeta ay(0) = yo
y′(0) = vo
Primero recordemos que
d2y
dt2=dv
dt= g
y por diferenciasdv
dt≈ vk+1 − vk
∆t
de donde podemos obtener
vk+1 = vk + g∆t
Una vez que calculamos v, podemos calcular y mediante
dy
dt= v
y por diferenciasdy
dt≈ yk+1 − yk
∆t
de donde podemos obtener
yk+1 = yk + v∆t
La forma directa de calcular una solucion de
d2y
dt2= g
es por diferencias de segundo orden
d2y
dt2≈ yk+1 − 2yk + yk−1
∆t2
de donde podemos obtener
yk+1 = −yk−1 + 2yk + g∆t
program movr e a l : : yf , v fr e a l : : yo , vo ,vmr e a l : : dt , g , ti n t e g e r k
yo=0. e0vo=0. e0
g =−9.81e0t = 0 .0 e0dt = 1 . e0tmx= 5tk = tmx/dt
open ( un i t =100 , f i l e =’movdata1 . dat ’ )wr i t e (100 ,∗ ) t , vo , yodo k=1, tk
t=k∗dtvf=vo+g∗dt
vm=(vo+vf ) /2 . e0
yf=yo+vm∗dt
vo=vfyo=yf
wr i t e (100 ,∗ ) t , vf , y f
enddo200 format ( ’2 F10 . 4 ’ )
c l o s e (100)end program
program movr e a l : : yf , v fr e a l : : yb , yo , vo ,vmr e a l : : dt , g , ti n t e g e r k
yb=0. e0yo=0. e0vo=0. e0
g =−9.81e0t = 0 .0 e0dt = 1 . e−2tmx= 5tk = tmx/dt
open ( un i t =100 , f i l e =’movdata3 . dat ’ )wr i t e (100 ,∗ ) t , vo , yodo k=1, tk
t=k∗dtvf = vo + g∗dt
!vm=(vo+vf ) /2 . e0
yf = − yb + 2∗yo + g∗dt ∗∗2. e0
vo=vfyb=yoyo=yf
wr i t e (100 ,∗ ) t , vf , y f
enddo200 format ( ’2 F10 . 4 ’ )
c l o s e (100)end program
program movr e a l : : yf , v fr e a l : : yo , vo ,vmr e a l : : dt , g , ti n t e g e r k
yo=0. e0vo=0. e0
g =−9.81e0t = 0 .0 e0dt = 1 . e0tmx= 5tk = tmx/dt
open ( un i t =100 , f i l e =’movdata1 . dat ’ )wr i t e (100 ,∗ ) t , vo , yodo k=1, tk
t=k∗dtvf=vo+g∗dt
vm=(vo+vf ) /2 . e0
yf=yo+vm∗dt
vo=vfyo=yf
wr i t e (100 ,∗ ) t , vf , y f
enddo200 format ( ’2 F10 . 4 ’ )
c l o s e (100)end program
program movr e a l : : yf , v fr e a l : : yb , yo , vo ,vmr e a l : : dt , g , ti n t e g e r k
yb=0. e0yo=0. e0vo=0. e0
g =−9.81e0t = 0 .0 e0dt = 1 . e−2tmx= 5tk = tmx/dt
open ( un i t =100 , f i l e =’movdata3 . dat ’ )wr i t e (100 ,∗ ) t , vo , yodo k=1, tk
t=k∗dtvf = vo + g∗dt
!vm=(vo+vf ) /2 . e0
yf = − yb + 2∗yo + g∗dt ∗∗2. e0
vo=vfyb=yoyo=yf
wr i t e (100 ,∗ ) t , vf , y f
enddo200 format ( ’2 F10 . 4 ’ )
c l o s e (100)end program
circulo rojo: aproximacion por diferenciaspunto azul: solucion analıtica
1 2 3 4 5−140
−120
−100
−80
−60
−40
−20
0posición [m]
1 2 3 4 5−50
−45
−40
−35
−30
−25
−20
−15
−10
−5
0velocidad [m s
−1]
yk+1 y vk+1 con diferencias de primer orden
1 2 3 4 5−500
−450
−400
−350
−300
−250
−200
−150
−100
−50
0posición [m]
1 2 3 4 5−50
−45
−40
−35
−30
−25
−20
−15
−10
−5
0velocidad [m s
−1]
yk+1 con diferencias de segundo orden (∆t = 1,00 s)vk+1 con diferencias de primer orden
circulo rojo: aproximacion por diferenciaspunto azul: solucion analıtica
1 2 3 4 5−140
−120
−100
−80
−60
−40
−20
0posición [m]
1 2 3 4 5−50
−45
−40
−35
−30
−25
−20
−15
−10
−5
0velocidad [m s
−1]
yk+1 y vk+1 con diferencias de primer orden
1 2 3 4 5−500
−450
−400
−350
−300
−250
−200
−150
−100
−50
0posición [m]
1 2 3 4 5−50
−45
−40
−35
−30
−25
−20
−15
−10
−5
0velocidad [m s
−1]
yk+1 con diferencias de segundo orden (∆t = 1,00 s)vk+1 con diferencias de primer orden
circulo rojo: aproximacion por diferenciaspunto azul: solucion analıtica
1 2 3 4 5−140
−120
−100
−80
−60
−40
−20
0posición [m]
1 2 3 4 5−50
−45
−40
−35
−30
−25
−20
−15
−10
−5
0velocidad [m s
−1]
1 2 3 4 5−140
−120
−100
−80
−60
−40
−20
0posición [m]
1 2 3 4 5−50
−45
−40
−35
−30
−25
−20
−15
−10
−5
0velocidad [m s
−1]
yk+1 con diferencias de segundo orden (∆t = 0,01 s)vk+1 con diferencias de primer orden
Solucion de Ecuaciones Diferenciales Ordinarias.
Ecuacion Diferencial Ordinaria de Primer Orden
dy(x)
dx= f(x, y)
sujeto a la condicion inical y(x = a) = αy definida para a ≤ x ≤ b.
De forma general:
dy
dt= f(t), y(t = to) = yo
∫ yn
yo
dy =
∫ tn
to
f(t)dt
y = yo+
∫ tn
to
f(t)dx
Metodo de Runge-Kutta para EDO de primer orden
Metodo de Runge-Kutta de segundo orden
yi = yo
k1 = hf(xi , yi)
k2 = hf(xi + h , yi+ k1)
yi+1 = yi +1
2(k1 + k2)
Metodo de Runge-Kutta de tercer orden
yi = yo
k1 = hf(xi , yi)
k2 = hf(xi + h/2, yi + k1/2)
k3 = hf(xi + h/2, yi + k2/2)
k4 = hf(xi + h , yi+ k3)
yi+1 = yi +1
6(k1 + 2k2 + 2k3 + k4)
Ejemplo de Solucion por el metodo de Runge-Kutta
dy
dx= 2xy
bajo la condiciony(0) = 1
definido para x en [0 0.5].
I Definir variables
I Valor de variables
I Principal y subrutinas
I Diagrama de flujo
I Visualizacion de resultados
I Validacion
program rk1CC dyC −−−− = 2xy ;C dxCC y(0)=1 , dx=0.1; t = [ 0 , 0 . 5 ] .C
r e a l : : y , yo , to , t fi n t e g e r : : k
yo=1.0;to =0.0;t f =0.1;
open ( un i t =100 , f i l e =’ rk1 . dat ’ )wr i t e (100 , ’ ( 2F9 . 3 ) ’ ) to , yodo k=1,5
c a l l runkut ( yo , to , t f , y ) ;wr i t e (100 , ’ ( 2F9 . 3 ) ’ ) t f , yyo=yto=t ft f=t f +0.1
enddoc l o s e (100)returnend
C −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−subrout ine fun (x , y , f )
r e a l x , y , f
f=2∗x∗y
end
subrout ine runkut ( yo , xo , xf , y )
r e a l : : x , y , xo , yo , xf , f , dxr e a l : : K1 ,K2 ,K3 ,K4i n t e g e r : :N
N=10dx=(xf−xo )/(N)
do j =1,Nx=xoy=yoc a l l fun (x , y , f )K1=dx∗ f
x=xo+0.5∗dxy=yo+K1/2c a l l fun (x , y , f )K2=dx∗ f
x=xo+0.5∗dxy=yo+K2/2c a l l fun (x , y , f )K3=dx∗ f
x=xo+dxy=yo+K3c a l l fun (x , y , f )K4=dx∗ f
y=yo+(K1+2∗K2+2∗K3+K4)/6xo=xo+dxyo=y
enddoend
0 .000 1 .0000 .100 1 .0100 .200 1 .0410 .300 1 .0940 .400 1 .1740 .500 1 .284
c l e a r a l lc l cdso lve ( ’Dy=2∗x∗y ’ , ’ y (0)=1 ’ , ’ x ’ ) ;x=0;f o r k=1:6
eva l ( [ ’ y=’ char ( ans ) ’ ; ’ ] )X(k)=x ; Y(k)=y ;x=x+0.1;
end%− − − − − − − − − − − − − − − − −dat=load ( ’ rk1 . dat ’ ) ;c l fp l o t ( dat ( : , 1 ) , dat ( : ,2) , ’ .−−k ’ , . . .
’ MarkerSize ’ , 5 0 , . . .’ Linewidth ’ , 2 )
hold onp lo t (X,Y, ’ . r ’ , ’ MarkerSize ’ , 2 0 )s e t ( gca , ’ YTick ’ , [ 1 : 0 . 1 : 1 . 3 ] )s e t ( gca , ’ FontSize ’ , 1 8 )g r id onax i s ( [ 0 0 .5 1 1 . 3 ] )drawnowpr in t −depsc rk1
0 0.1 0.2 0.3 0.4 0.51
1.1
1.2
1.3
Ecuacion Diferencial de Segundo Orden
d2y
dt2= f(y, y′, t)
sujeto a la condicion inical y(t = a) = α y y′(t = a) = θy definida para a ≤ t ≤ b.
Reduccion a sistema de Ecuaciones Ordinarias
u1 = y; u2 = y′
u′1 = u2; u′2 = y′′
u′1 = u2 ←− f1
u′2 = f(u1, u2, t)←− f2
sujeto a la condicion inical u1(0) = α y u2(0) = θy definida para t > 0. (a = 0 en este caso).
Metodo de Runge-Kutta para sistema de 2 EDO de primer orden. El indice 1 serefiere a u′1 = v (ec. para posicion) y el indice 2, a u′2 = f(y, y′, y) (ec. paravelocidad).
u1 = α
u2 = θ
k11 = hf1(t, y, y′)
k21 = hf2(t, y, y′)
k12 = hf1(t+ h/2, y + k11/2, y′ + k21/2)
k22 = hf2(t+ h/2, y + k11/2, y′ + k21/2)
k13 = hf1(t+ h/2, y + k12/2, y′ + k22/2)
k23 = hf2(t+ h/2, y + k12/2, y′ + k22/2)
k14 = hf1(t+ h, y + k13 , y′ + k23)
k24 = hf2(t+ h, y + k13 , y′ + k23)
yi+1 = yi +1
6(k11 + 2k12 + 2k13 + k14)
y′i+1 = y′i +1
6(k21 + 2k22 + 2k23 + k24)
NOTA ACLARATORIA
I se debe considerar un sistema de dos ecuacionesdiferenciales ordinarios de primer orden donde losterminos del lado derecho son representados porfunciones f1 y f2.
I ambas funciones dependen, en general, de y, y′ y t.
I se deben calcular las k’s de forma simultanea para lasdos ecuaciones, es decir, se calculan las dos k1, despueslas dos k2, etc.
I esto es porque las k4 dependen de las k3, las k3, de lask2 y las k2, de las k1.
I se puede generalizar con ciclos, tanto para los calculosen el metodo RK como para evaluar las funciones.