Documentos de Académico
Documentos de Profesional
Documentos de Cultura
su Implementacin Computacional
Antonio Carrillo Ledesma y Omar Mendoza Bernal
Facultad de Ciencias, UNAM
http://www.mmc.geofisica.unam.mx/acl/
Una copia de este trabajo se puede descargar de
http://www.mmc.geofisica.unam.mx/acl/Textos/
Invierno 2015, Versin 1.0
ndice
1 Expansin en Series de Taylor
1.1 Aproximacin de la Primera Derivada
1.1.1 Diferencias Progresivas . . . . .
1.1.2 Diferencias Regresivas . . . . .
1.1.3 Diferencias Centradas . . . . .
1.2 Derivadas de Ordenes Mayores . . . .
1.2.1 Derivada de Orden Dos . . . .
1.2.2 Derivadas de Ordenes Mayores
1.3 Derivadas en Dos Dimensiones . . . .
1.4 Derivadas en Tres Dimensiones . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
2
2
2
3
3
4
5
5
6
7
33
33
33
35
35
36
38
40
41
43
44
45
4 Implementacin Computacional del Mtodo de Diferencias Finitas para la Resolucin de Ecuaciones Diferenciales Parciales
47
4.1 Implementacin en SciLab . . . . . . . . . . . . . . . . . . . . . . 47
4.2 Implementacin en Octave (MatLab) . . . . . . . . . . . . . . . . 49
4.3 Implementacin en C++ . . . . . . . . . . . . . . . . . . . . . . . 51
4.4 Implementacin en Python . . . . . . . . . . . . . . . . . . . . . 53
5 Bibliografa
antoniocarrillo@ciencias.unam.mx
56
Sea f(x) una funcin definida en (a, b) que tiene hasta la ksima derivada,
entonces la expansin de f(x) usando series de Taylor alrededor del punto xi
contenido en el intervalo (a, b) ser
(x xi ) df
(x xi )2 d2 f
(x xi )k dk f
f (x) = f(xi ) +
+
+
...
+
(1.1)
1!
dx xi
2!
dx2 xi
k!
dxk
donde = xi + (x xi ) y 0 < < 1.
1.1
Diferencias Progresivas
(1.2)
(1.3)
dx xi
x
2! dx2 p
como
f (xi ) =
fi+1 fi
x
(1.5)
(1.6)
antoniocarrillo@ciencias.unam.mx
1.1.2
Diferencias Regresivas
(1.7)
=
(1.8)
dx xi
x
2! dx2 r
como
f (xi ) =
fi fi1
x
(1.10)
(1.11)
Diferencias Centradas
f(xi x) = f(xi ) x
df
x2 d2 f
x3 d3 f
+
dx xi
2! dx2 xi
3! dx3 r
(1.13)
(1.14)
antoniocarrillo@ciencias.unam.mx
x0
x0
como
f (xi ) =
fi+1 fi1
2x
(1.17)
(1.18)
(1.19)
1.2
antoniocarrillo@ciencias.unam.mx
1.2.1
x2
x3
x4 (4)
f (xi )+
f (xi )+
f ( p ) (1.21)
2!
3!
4!
y
f (xi x) = f(xi )xf (xi )+
x2
x3
x4 (4)
f (xi )
f (xi )+
f ( r ) (1.22)
2!
3!
4!
y eliminado las derivadas primeras, sumando las ecuaciones anteriores y despejando se encuentra que
f (xi ) =
f ( c )
x2
12
(1.23)
(1.24)
fi+2 2fi+1 + fi
+ O(x)
x2
fi+3 + 4fi+2 5fi+1 + 2fi
+ O(x2 )
x2
fi+2 + 16fi+1 30fi + 16fi1 fi2
+ O(x4 )
12x2
(1.25)
antoniocarrillo@ciencias.unam.mx
1.3
f(xi + x, yj ) = f (xi , yj ) + x
f (xi + x, yj ) f (xi , yj )
x
f (xi , yj + y) f (xi , yj )
y
(1.29)
fi+1,j fi,j
h
fi,j+1 fi,j
.
k
(1.30)
f (xi , yj ) f (xi x, yj )
x
f (xi , yj ) f (xi , yj y)
y
(1.31)
fi,j fi1,j
h
fi,j fi,j1
.
k
(1.32)
f (xi + x, yj ) f (xi x, yj )
2x
f (xi , yj + y) f (xi , yj y)
2y
(1.33)
fi+1,j fi1,j
2h
fi,j+1 fi,j1
.
2k
(1.34)
1.4
(1.36)
f(xi + x, yj , zk ) = f(xi , yj , zk ) + x
antoniocarrillo@ciencias.unam.mx
es
f (xi , yj , zk )
x
f (xi , yj , zk )
y
f (xi , yj , zk )
z
f (xi + x, yj , zk ) f (xi , yj , zk )
x
f (xi , yj + y, zk ) f (xi , yj , zk )
y
f (xi , yj , zk + z) f (xi , yj , zk )
z
(1.38)
fi+1,j,k fi,j,k
h
fi,j+1,k fi,j,k
l
fi,j,k+1 fi,j,k
.
m
(1.39)
f (xi , yj , zk ) f (xi x, yj , zk )
x
f (xi , yj , zk ) f (xi , yj y, zk )
y
f (xi , yj , zk ) f (xi , yj , zk z)
z
(1.40)
fi,j,k fi1,j,k
h
fi,j,k fi,j1,k
l
fi,j,k fi,j,k1
.
m
(1.41)
f (xi + x, yj , zk ) f (xi x, yj , zk )
2x
f (xi , yj + y, zk ) f (xi , yj y, zk )
2y
f (xi , yj , zk + z) f (xi , yj , zk z)
2z
antoniocarrillo@ciencias.unam.mx
(1.42)
fi+1,j,k fi1,j,k
2h
fi,j+1,k fi,j1,k
2l
fi,j,k+1 fi,j,k1
.
2m
(1.43)
2 f (xi , yj , zk )
x2
2 f (xi , yj , zk )
y 2
2
f (xi , yj , zk )
z 2
antoniocarrillo@ciencias.unam.mx
(1.45)
(2.1)
en a x b
2.1
0 x 1,
u(0) = u ,
u(1) = u
(2.2)
i = 0, 1, ..., n,
h=
1
= x
n
(2.3)
(2.4)
4 Notemos que en cada punto de la malla, la aproximacin por diferencias finitas supone
la solucin de tres puntos de la malla xi1 , xi y xi+1 . Al conjunto de estos tres puntos de la
malla son comnmente llamados el estncil de diferencias finitas.
antoniocarrillo@ciencias.unam.mx
10
o en su forma simplificada
u (xi )
(2.5)
= f (x1 )
= f (x2 )
..
.
(2.6)
= f (xi )
..
.
= f (xn2 )
= f (xn1 ).
Este sistema de ecuaciones se puede escribir como un matriz A y los vectores u y fde la forma
h22
1
h2
1
h2
h22
1
h2
1
h2
h22
..
1
h2
..
1
h2
..
.
h22
1
h2
1
h2
h22
u1
u2
u3
..
.
un2
un1
f(x1 ) uh2
f (x2 )
f (x3 )
..
.
f (xn2 )
u
f(xn1 ) h2
2 1
1 2 1
1 2
1
..
h2
.
1
..
.
1
u1
u2
u3
..
.
..
2 1 un2
1 2
un1
f(x1 ) uh2
f (x2 )
f (x3 )
..
.
f(xn2 )
u
f(xn1 ) h2
11
3. Resolviendo el sistema de ecuaciones (vase captulo 3), obtenemos la solucin aproximada en cada punto interior de la malla. La solucin completa
al problema la obtenemos al formar el vector
u u1 u2 u3 un2 un1 u .
Para el problema general dado por la Ec.(2.1)
(2.7)
i = 1, 2, ..., n. (2.8)
u
x
u (x + x, t) u (x, t)
p
(x, t) p x +
,t
(2.9)
x
x
2
x
x
u (x, t) u (x x, t)
p x
,t
/x
2
x
entonces se tiene que
i1
p(ui+1 , t) ui+1h+ui p (ui1,t ) ui u
ui+1 ui1
h
+ qi
ri ui = fi (2.10)
h
2h
antoniocarrillo@ciencias.unam.mx
12
El esquema mixto, en donde se usa el esquema de diferencias finitas centradas para el trmino de difusin y el esquema upwind para el trmino
de adveccin
pi
= fi , si qi 0
= fi , si qi < 0
pi
(2.11)
0 x 1,
u(0) = 0,
u(1) = 1
P=2/(h^2);
Q=-1/(h^2)+1/(2*h);
R=-1/(h^2)-1/(2*h);
antoniocarrillo@ciencias.unam.mx
13
A(i,i-1)=R;
A(i,i)=P;
A(i,i+1)=Q;
end
// Relglon final de la matriz A y vector b
A(N,N-1)=R;
A(N,N)=P;
b(N)=-Y1*Q;
// Resuleve el sistema lineal Ax=b
x=inv(A)*b;
// Prepara la graficacin
xx=zeros(M,1);
for i=1:M
xx(i)=a+h*(i-1);
end
yy=zeros(M,1);
yy(1)=Y0; // Condicin inicial
for i=1:N
yy(i+1)=x(i);
end
yy(M)=Y1; // Condicin inicial
// Grafica la solucin de la Ecuacin Diferencial Parcial en 1D
plot2d(xx,yy)
Ejemplo 2 Sea
u (x) = 2 cos (x) ,
0 x 1,
u(0) = 1,
u(1) = 1
14
antoniocarrillo@ciencias.unam.mx
15
2.2
Consideremos el problema
u (x) = f (x),
0x1
(2.12)
quedando
u1 u0
= cte1
(2.13)
h
para la segunda condicin de frontera una aproximacin usando diferencias regresivas Ec.(1.10)
u(xi ) u(xi h)
du
=
dx xi
h
quedando
un un1
= cte2
(2.14)
h
pero el orden de aproximacin no seria el adecuado pues estamos aproximando el
dominio con diferencias centradas con un error local de truncamiento de segundo
orden Oc (x2 ), en lugar de ello usaremos diferencias centradas Ec.(1.15) para
tener todo el dominio con el mismo error local de truncamiento.
Para usar diferencias centradas Ec.(1.15)
du
u(xi + h) u(xi h)
=
dx xi
2h
en el primer nodo necesitamos introducir un punto de la malla ficticio x1 =
(x0 x) con un valor asociado a u1 , entonces
u1 u1
= cte1
2h
(2.15)
(2.16)
Estos valores no tienen significado fsico alguno, dado que esos puntos se encuentran fuera del dominio del problema. Entonces debemos de hacer:
antoniocarrillo@ciencias.unam.mx
16
i = 0, 1, ..., n,
h=
1
= x.
n
(2.17)
(2.18)
= cte1
= f (x1 )
..
.
= f (xi )
(2.19)
..
.
= f (xn1 )
= cte2 .
2.3
u1 u1
+ un =
2h
o
u1 = u1 +
antoniocarrillo@ciencias.unam.mx
2
2h
un
17
1
f0
2+
un + 2 u1 =
+
h
h
h
2
h
(2.20)
0x1
u(0) = u
du
= cte1 en u(1).
dx
respectivamente. Para usar el procedimiento general de solucin numrica mediante el mtodo de diferencias finitas, primeramente debemos de expresar la
condicin de frontera Neumann mediante diferencias centradas Ec.(1.15)
du
u(xi + h) u(xi h)
=
dx xi
2h
(2.21)
Este valor no tiene significado fsico alguno, dado que este punto se encuentra
fuera del dominio del problema.
Entonces debemos de hacer:
1. Generamos una malla homognea del dominio
xi = ih,
i = 0, 1, ..., n,
h=
1
= x.
n
(2.22)
antoniocarrillo@ciencias.unam.mx
18
(2.23)
= f (x1 )
= f (x2 )
..
.
(2.24)
= f (xi )
..
.
= f (xn1 )
= cte1 .
u u1 u2 u3 un2 un1 un .
Veamos unos ejemplos desarrollados en SCILAB7
Ejemplo 3 Sea
u (x) = 2 cos (x) ,
0 x 0.5,
u(0) = 1,
u (0.5) =
antoniocarrillo@ciencias.unam.mx
19
antoniocarrillo@ciencias.unam.mx
20
Ejemplo 4 Sea
u (x) k2 u(x) = 0,
0 x 1,
u(0) = 1,
u (1) = iku(1)
antoniocarrillo@ciencias.unam.mx
21
22
plot2d(xxx,zzz)
2.4
donde
ut = Lu
(2.25)
(2.26)
ut = (1 ) (Lu)j + (Lu)j+1 .
(2.27)
aqu, los coeficientes p, q y r pueden depender del espacio y del tiempo. Entonces
el esquema theta est dado por
antoniocarrillo@ciencias.unam.mx
23
uj+1
uji
i
t
(2.28)
j
j
j
j+1
j+1
j+1
u
2u
+
u
2u
+
u
u
p
i1
i
i+1
i1
i
i+1
(p (x) u (x))
+
2
x2
x2
j
j
j+1
j+1
q ui1 + ui+1 ui1 + ui+1
q (x) u (x)
+
2
2x
2x
(2.29)
(2.30)
(2.31)
esto se logra, colocando del lado izquierdo la igualdad de los trminos que contengan el paso del tiempo correspondiente a j + 1 y del lado derecho a los
correspondientes trminos de j.
A continuacin, veamos un ejemplo del esquema Crank-Nicolson desarrollados en SCILAB9
Ejemplo 5 Sea
ut a(x)u (x) b(x)u (x) + c(x)u = f,
l0 x l,
0<t<T
antoniocarrillo@ciencias.unam.mx
24
25
end
disp(espacio, "Espacio")
// Condicion inicial
for i=1:M
w(i) = condicion_inicial(espacio(i+1));
end
w_sol(1) = cond_izq;
for kk = 1:M
w_sol(kk + 1) = w(kk);
end
w_sol(m) = cond_izq;
plot(espacio, w_sol);
disp(w, "Condiciones iniciales")
// primer renglon de cada matriz
A(1,1) = 1/k - a(l0 + h)/(h*h);
A(1,2) = a(l0 + h)/(2*h*h) + b(l0 + h)/(4*h) ;
B(1,1) = 1/k + a(l0 + h)/(h*h) - c(l0 + h);
B(1,2) = - a(l0 + h)/(2*h*h) - b(l0 + h)/(4*h);
ff(1) = f(l0 + h) - cond_izq;
// se completa las matrices desde el renglon 2 hasta el m-2
for i = 2:M-1
A(i, i-1) = a(l0 + i*h)/(2*h*h) - b(l0 + i*h)/(4*h) ;
A(i,i) = 1/k - a(l0 + i*h)/(h*h);
A(i,i+1) = a(l0 + i*h)/(2*h*h) + b(l0 + i*h)/(4*h) ;
B(i, i-1) = - a(l0 + i*h)/(2*h*h) + b(l0 + i*h)/(4*h);
B(i,i) = 1/k + a(l0 + i*h)/(h*h) - c(l0 + i*h);
B(i,i+1) = - a(l0 + i*h)/(2*h*h) - b(l0 + i*h)/(4*h);
end
// Ultimo renglon de cada matriz
A(M,M-1) = a(l - h)/(2*h*h) - b(l-h)/(4*h) ;
A(M,M) = 1/k - a(l - h)/(h*h);
B(M,M-1) = - a(l-h)/(2*h*h) + b(l-h)/(4*h);
B(M,M) = 1/k + a(l-h)/(h*h) - c(l-h);
ff(M) = f(l - h) - cond_der;
// Resolvemos el sistema iterativamente
for j=1:21
t = j*k;
B_prima = B * w + ff;
w = inv(A) * B_prima;
disp(t, "tiempo")
disp(w, "Sol")
w_sol(1) = cond_izq;
for kk = 1:M
w_sol(kk + 1) = w(kk);
end
w_sol(m) = cond_izq;
antoniocarrillo@ciencias.unam.mx
26
plot(espacio, w_sol);
end
2.4.2
(p (x) u (x)) p
x2
j
ui1 + uji+1
q (x) u (x) q
2x
utt
(2.32)
(2.33)
(2.34)
(2.35)
uj+1
= 2uji uj1
+ (t)2 Luj
i
i
(2.36)
2
0
u1i = 2u0i u1
i + (t) Lu
(2.37)
esto se logra, colocando del lado izquierdo la igualdad de los trminos que contengan el paso del tiempo correspondiente a j + 1 y del lado derecho a los
correspondientes trminos de j y j 1. Para calcular uj+1
es necesario conocer
i
ui1 , ui , ui+1 en los dos instantes inmediatos anteriores, i.e. tj y tj1 .
En particular para calcular u1i es necesario conocer u0i y u1
i , si consideramos
para j = 0, entonces
u1i u1
i
u1i
(2.38)
2uji
+ uj+1
i
2
(t)
uj+1
i
= Luj
(2.39)
= Luj
= 2uji uj1
+ (t)2 Luj
i
27
Ejemplo 6 Sea
utt 4u (x) = 0,
0 x l,
0<t<T
sujeta a
u(0, t) = u(1, t) = 0,
u(x, 0) = sin(x),
ut (x, 0) = 0
28
w_ = zeros(m,1);
A = eye(m,m);
B = zeros(m,m);
B_prima = zeros(m,1);
espacio = zeros(m,1)
sol = zeros(m,1);
// primer renglon de cada matriz
B(1,1) = 2*a(a_)*dt*dt/(h*h)
B(1,2) = -a(a_)*dt*dt/(h*h)
// se completa las matrices desde el renglon 2 hasta el m-1
for i = 2:m-1
B(i, i-1) = -a(i*h)*dt*dt/(h*h)
B(i,i) = 2*a(i*h)*dt*dt/(h*h)
B(i,i+1) = -a(i*h)*dt*dt/(h*h)
end
// Ultimo renglon de cada matriz
B(m,m-1) = -a(b_)*dt*dt/(h*h)
B(m,m) = 2*a(b_)*dt*dt/(h*h)
// muestro la matriz
//printf("Matriz B\n");
//disp(B);
for i=1:m
xx = (i-1)*h;
espacio(i) = a_ + xx;
w(i) = inicial(espacio(i)); // Condiciones iniciales
w_(i) = inicial(espacio(i)) + u_t(espacio(i)) * dt
end
//
//disp(espacio)
//disp(w)
////////////
//Para t = 0
B_prima = B * w;
for i = 1:m
w_sol(i) = w_(i) + B_prima(i);
end
////////////
printf("w para t = 0\n");
disp(w_sol);
for i = 1:m
sol(i) = analitica(espacio(i), 0)
end
printf("Solucion analitica para t = 0\n")
disp(sol)
plot(espacio,w_sol)
//plot(espacio,sol,r)
antoniocarrillo@ciencias.unam.mx
29
w_sol_temp = w_sol;
w_temp = w
for i=1:500
t = i*dt
B_prima = B * w_sol_temp;
w_ = 2 * w_sol_temp - w_temp
w_sol = w_ + B_prima;
////
// for j = 1:m
// sol(j) = analitica(espacio(j), t)
// end
//
// printf("Sol analitica dt = %f", t)
// disp(sol)
// printf("Sol metodo dt = %f", t)
// disp(w_sol)
w_temp = w_sol_temp
w_sol_temp = w_sol
if i == 5 | i == 50 | i == 100 | i == 150 | i == 200 | i == 250 | i ==
300 | i== 350 | i == 400 | i == 450 | i == 500 then
plot(espacio,w_sol)
end
//plot(espacio,sol,r)
end
2.5
30
n
i=1
|ei |
n
i=1
e2i
12
Antonio Carrillo Ledesma
lim E = 0.
h0
(2.41)
T (x) = Lu Lh u.
Para la ecuacin diferencial u (x) = f(x) y el esquema de diferencias centradas usando tres puntos u(xh)2u(x)+u(x+h)
, el error de truncamiento local
h2
es
u(x h) 2u(x) + u(x + h)
h2
u(x h) 2u(x) + u(x + h)
= f (x)
.
h2
T (x) = Lu Lh u = u (x)
(2.43)
h0
h0
(2.44)
T (x) = u (x)
antoniocarrillo@ciencias.unam.mx
31
1 (4)
donde C = 12
u (x). Por lo tanto, este esquema de diferencias finitas es consistente y la discretizacin es de segundo orden de precisin.
La consistencia no puede garantizar un esquema de diferencias finitas trabaje. Para ello, necesitamos determinar otra condicin para ver si converge o
no. Tal condicin es llamada la estabilidad de un mtodo de diferencias finitas.
Para el problema modelo, tenemos que
Au = F + T,
AU = F,
A (u U) = T = AE
(2.46)
antoniocarrillo@ciencias.unam.mx
32
Los modelos matemticos de muchos sistemas en Ciencia e Ingeniera y en particular una gran cantidad de sistemas continuos geofsicos requieren el procesamiento de sistemas algebraicos de gran escala. En este trabajo se muestra
como proceder, para transformar un problema de ecuaciones diferenciales parciales en un sistema algebraico de ecuaciones lineales; y as, poder hallar la
solucin a dicho problema al resolver el sistema lineal, estos sistemas lineales
son expresados en la forma matricial siguiente
Au = f
(3.1)
3.1
Mtodos Directos
Factorizacin LU
antoniocarrillo@ciencias.unam.mx
33
(3.2)
Ly = f
(3.3)
= f
y = fi
y
Uu = y
u =
1
Aii
yi
i1
j=1
n
y1 =
f1 /A11
(3.4)
xn = y
n /Ann
.(3.5)
j=i+1
1 1 Notemos
antoniocarrillo@ciencias.unam.mx
34
para la matriz llena pero slo del orden O(N b2 ) operaciones aritmticas para
la matriz con un ancho de banda de b. La solucin de los sistemas Ly = f
y Uu = y requieren n2 /2 n/2 operaciones aritmticas cada uno (vase [10] y
[13]).
3.1.2
Factorizacin Cholesky
Aii = A11
Para j = 2, ..., n calcule Aj1 = Aj1 /A11
Para i = 2,
..., n {
i1
Aii = Aii
A2ik
k=1
Para j = i +
1, ..., ni1
Aji = Aji
Ajk Aik /Aii
(3.6)
k=1
}
Ann
n1
= Ann
(Ank )2
k=1
35
(3.8)
3.2
Mtodos Iterativos
Au = f
(3.10)
36
max
1
min
(3.11)
4 3
0
x1
24
3 4 1 x2 = 30
0 1 4
x3
24
k0
rk = b Axk .
antoniocarrillo@ciencias.unam.mx
37
(3.13)
Antonio Carrillo Ledesma
(3.14)
donde , = (, ) ser el producto interior adecuado al sistema lineal en particular, la solucin aproximada ser un+1 y el vector residual ser rn+1 .
En la implementacin numrica y computacional del mtodo es necesario
realizar la menor cantidad de operaciones posibles por iteracin, en particular
en Apn , una manera de hacerlo queda esquemticamente como:
Dado el vector de bsqueda inicial u, calcula r = f Au, p = r y = r r.
Para n = 1, 2, ...,Mientras ( < ) {
v = Ap
= pv
u = u + p
antoniocarrillo@ciencias.unam.mx
38
r = r v
= r r
=
p = r + p
=
}
La solucin aproximada ser u y el vector residual ser r.
N
2k
1 Cond(A)
u uk u u0
.
%
(3.15)
A
A
1 + Cond(A)
El siguiente teorema da idea del espectro de convergencia del sistema Au = b
para el mtodo de Gradiente Conjugado.
Teorema 14 Sea = cond(A) = max
1, entonces el mtodo de Gradiente
min
Conjugado satisface la Anorma del error dado por
n
en
2
1
2
(3.16)
&
'n &
'n
e0
+1
+1
+1
+
1
1
donde em = u um del sistema Au = b.
1
2
1
+1
tal que
(3.17)
n
en A e0 A exp 2
(3.18)
39
3.2.2
generacin de una base ortonormal. Sea V n = v1 , v2 , ..., v n la cual denota la
matriz conteniendo vj en la j-sima columna, para j = 1, 2, ..., n, y sea H n =
[hi,j ] , 1 i, j n, donde las entradas de H n no especificadas en el algoritmo
son cero. Entonces, H n es una matriz superior de Hessenberg. i.e. hij = 0 para
j < i 1, y
AV n = V n H n + hn+1,n 0, ..., 0, vn+1
(3.20)
Hn
= H Tn AV n .
(3.22)
n
0
n
= [hij ]
donde H
1in+1,1jn , la solucin aproximada ser u = u + V n y ,
n
y el vector residual ser
&
'
yn .
rn = r0 AV n y n = V n+1 0 e1 H
(3.23)
n
antoniocarrillo@ciencias.unam.mx
40
3.3
V
es el nmero de condicionamiento de V .
V 1
antoniocarrillo@ciencias.unam.mx
41
antoniocarrillo@ciencias.unam.mx
42
3.3.1
Matrices Bandadas
a1 b1
c1
d2 a2 b2
c2
d3 a3 b3
c3
d4 a4 b4
c4
d5 a5 b5
c5
A = e5
(3.25)
e6
d6 a6 b6
e7
d7 a7 b7
e8
d8 a8 b8
e9
d9 a9
la cual puede ser almacenada usando el algoritmo (vase [13]) Compressed Diagonal Storage (CDS), optimizado para ser usado en C++, para su almacenamiento y posterior recuperacin. Para este ejemplo en particular, se har uso
de un vector de ndices
(3.26)
0 0
0 d2
0 d3
0 d4
Dat =
e5 d5
e6 d6
e7 d7
e8 d8
e9 d9
estructura
a1
a2
a3
a4
a5
a6
a7
a8
a9
b1
b2
b3
b4
b5
b6
b7
b8
0
c1
c2
c3
c4
c5
0
0
0
0
(3.27)
de tal forma que la matriz A puede ser reconstruida de forma eficiente. Para
obtener el valor Ai,j , calculo ind = j i, si el valor ind esta en la lista de
ndices Ind supngase en la columna k, entonces Ai,j = Datik , en otro caso
Ai,j = 0.
Casos Particulares de la Matriz Bandada A Bsicamente dos casos particulares surgen en el tratamiento de ecuaciones diferenciales parciales: El primer
caso es cuando el operador diferencial parcial es simtrico y el otro, en el que
los coeficientes del operador sean constantes.
Para el primer caso, al ser la matriz simtrica, slo es necesario almacenar
la parte con ndices mayores o iguales a cero, de tal forma que se buscara el
ndice que satisfaga ind = |j i| , reduciendo el tamao de la banda a b/2 en la
matriz A.
antoniocarrillo@ciencias.unam.mx
43
Matrices Dispersas
a1
b1
c1
a2
b2
c2
a
b
c
3
3
3
a4
b
4
a5
b5
c5
A=
(3.28)
a6 b6 c6
a7 b7 c7
a8
b8 c8
a9 b9
la cual puede ser almacenada usando el algoritmo (vase [13]) Jagged Diagonal Storage (JDC), optimizado para ser usado en C++. Para este ejemplo en
particular, se har uso de una matriz de ndices
1 6 9
2 5 8
5 8 9
1 4 0
(3.29)
Ind =
3 6 9
1 2 3
7 8 9
4 7 8
7 8 0
a1
a2
a3
a4
Dat =
a5
a6
a7
a8
a9
antoniocarrillo@ciencias.unam.mx
44
estructura
b1 c1
b2 c2
b3 c3
b4 0
b5 c5
b6 c6
b7 c7
b8 c8
b9 0
(3.30)
de tal forma que la matriz A puede ser reconstruida de forma eficiente. Para
obtener el obtener el valor Ai,j , busco el valor j en la lista de ndices Ind dentro
del rengln i, si lo encuentro en la posicin k, entonces Ai,j = Datik , en otro
caso Ai,j = 0.
Casos Particulares de la Matriz Dispersa A Si la matriz A, que al ser
almacenada, se observa que existen a lo ms r diferentes renglones con valores
distintos de los n con que cuenta la matriz y si r << n, entonces es posible slo
guardar los r renglones distintos y llevar un arreglo que contenga la referencia
al rengln almacenado.
3.3.3
Multiplicacin Matriz-Vector
antoniocarrillo@ciencias.unam.mx
45
antoniocarrillo@ciencias.unam.mx
46
4.1
Implementacin en SciLab
0 x 0.5,
u(0) = 1,
u (0.5) =
antoniocarrillo@ciencias.unam.mx
47
antoniocarrillo@ciencias.unam.mx
48
4.2
0 x 1,
u(0) = 1,
u(1) = 1
Octave [http://www.gnu.org/software/octave/]
[http://www.mathworks.com/products/matlab/]
1 7 MatLab
antoniocarrillo@ciencias.unam.mx
49
plot(x,U-u);
return
% Lado derecho
function y = f(x)
y = -pi*pi*cos(pi*x);
return
Para hacer uso del programa, escribir en la consola
[x,U] = fdm1d(0,1,1,-1,40);
antoniocarrillo@ciencias.unam.mx
50
4.3
Implementacin en C++
GMM++18 es una librera para C++ que permite definir diversos tipos de
matrices y vectores adems operaciones bsicas de algebra lineal. La facilidad
de uso y la gran cantidad de opciones hacen que GMM++ sea una buena opcin
para trabajar con operaciones elementales de algebra lineal.
Se instala en Debian Linux y/o Ubuntu como:
# apt-get install libgmm++-dev
Para compilar el ejemplo usar:
$ g++ ejemplito.cpp
Para ejecutar usar:
$ ./a.out
Ejemplo 20 Sea
u (x) + u(x) = 0,
0 x 1,
u(0) = 0,
u(1) = 1
[http://download.gna.org/getfem/html/homepage/gmm/]
antoniocarrillo@ciencias.unam.mx
51
b[0]=-Y0*R;
// Renglones intermedios de la matriz A y vector b
for(i=1;i <N-1;i++)
{
A(i,i-1)=R;
A(i,i)=P;
A(i,i+1)=Q;
}
// Relglon final de la matriz A y vector b
A(N-1,N-2)=R;
A(N-1,N-1)=P;
b[N-1]=-Y1*Q;
gmm::copy(A,AA);
// LU para matrices densa
gmm::lu_solve(AA, x, b);
std::cout << "LU"<< x << gmm::endl;
gmm::identity_matrix PS; // Optional scalar product for cg
gmm::identity_matrix PR; // Optional preconditioner
gmm::iteration iter(10E-6);// Iteration object with the max residu
size_t restart = 50; // restart parameter for GMRES
gmm::cg(A, x, b, PS, PR, iter); // Conjugate gradient
std::cout << "CGM"<< x << std::endl;
gmm::bicgstab(A, x, b, PR, iter); // BICGSTAB BiConjugate Gradient Stabilized
std::cout << "BICGSTAB"<< x << std::endl;
gmm::gmres(A, x, b, PR, restart, iter); // GMRES generalized minimum
residual
std::cout << "GMRES"<< x << std::endl;
gmm::qmr(A, x, b, PR, iter); // Quasi-Minimal Residual method.
std::cout << "Quasi-Minimal"<< x << std::endl;
// computation of a preconditioner (ILUT)
gmm::ilut_precond< gmm::row_matrix< gmm::rsvector<double> > > Pre(A,
10, 1e-4);
gmm::gmres(A, x, b, Pre, restart, iter); // execute the GMRES algorithm
std::cout << "GMRES preconditiones ILUT"<< x << std::endl;
return 0;
}
antoniocarrillo@ciencias.unam.mx
52
4.4
Implementacin en Python
2 x 8
53
nNodos = 401
h = (b - a)/(nNodos - 1)
# Intervalo de tiempo
dt = 0.012
# creo el vector w donde se guradar la solucin para cada tiempo
# B matriz del lado derecho
w = np.zeros((nNodos,1))
B = np.zeros((nNodos, nNodos))
B = np.matrix(B)
espacio = np.zeros((nNodos,1))
for i in xrange(nNodos):
xx_ = a + i*h
espacio[i] = xx_
w[i] = condicion_inicial(xx_)
print "Espacio"
print espacio
print "Condicin Inicial"
print w
mu = coef_a * dt / h
if mu <= 1:
print "mu ", mu
print "Buena aproximacin"
else:
print "mu ", mu
print "Mala aproximacin"
if coef_a >= 0:
B[0,0] = 1 - mu
for i in xrange (1, nNodos):
B[i,i-1] = mu
B[i,i] = 1 - mu
else:
B[0,0] = 1 + mu;
B[0,1] = -mu;
# se completa las matrices desde el renglon 2 hasta el m-2
for i in xrange(1, nNodos-1):
B[i,i] = 1 + mu;
B[i, i+1] = -mu;
B[nNodos-1,nNodos-1] = 1 + mu
# para guardar la soulcin analitica
xx = np.zeros((nNodos,1));
iteraciones = 201
# Matriz sparse csr
Bs = sparse.csr_matrix(B);
# Resolvemos el sistema iterativamente
for j in xrange (1, iteraciones):
t = j*dt;
antoniocarrillo@ciencias.unam.mx
54
w = Bs*w;
# Imprimir cada 20 iteraciones
if j%20 == 0:
print "t", t
print "w", w
plt.plot(espacio, w)
antoniocarrillo@ciencias.unam.mx
55
Bibliografa
Referencias
[1] K. Hutter y K Jhnk, Continuum Methods of Physical Modeling, SpringerVerlag Berlin Heidelberg New York, 2004.
[2] J. L. Lions y E. Magenes, Non-Homogeneous Bounduary Value Problems
and Applicarions Vol. I, Springer-Verlag Berlin Heidelberg New York, 1972.
[3] A. Quarteroni y A. Valli, Domain Decomposition Methods for Partial Differential Equations. Clarendon Press Oxford, 1999.
[4] A. Quarteroni y A. Valli; Numerical Approximation of Partial Diferential
Equations. Springer, 1994.
[5] B. Dietrich, Finite Elements: Theory, Fast Solvers, and Applications in
Solid Mechanics, Cambridge University, 2001.
[6] B. F. Smith, P. E. Bjrstad, W. D. Gropp; Domain Decomposition, Parallel
Multilevel Methods for Elliptic Partial Differential Equations. Cambridge
University Press, 1996.
[7] Fuzhen Zhang, The Schur Complement and its Aplications, Springer, Numerical Methods and Algorithms, Vol. 4, 2005.
[8] B. I. Wohlmuth; Discretization Methods and Iterative Solvers Based on
Domain Decomposition. Springer, 2003.
[9] L. F. Pavarino, A. Toselli; Recent Developments in Domain Decomposition
Methods. Springer, 2003.
[10] M.B. Allen III, I. Herrera & G. F. Pinder; Numerical Modeling in Science
And Engineering. John Wiley & Sons, Inc . 1988.
[11] R. L. Burden y J. D. Faires; Anlisis Numrico. Math Learning, 7 ed. 2004.
[12] S. Friedberg, A. Insel, and L. Spence; Linear Algebra, 4th Edition, Prentice
Hall, Inc. 2003.
[13] Y. Saad; Iterative Methods for Sparse Linear Systems. SIAM, 2 ed. 2000.
[14] Y. Skiba; Mtodos y Esquemas Numricos, un Anlisis Computacional.
UNAM, 2005.
[15] W. Gropp, E. Lusk, A. Skjellem, Using MPI, Portable Parallel Programming Whit the Message Passing Interface. Scientific and Engineering Computation Series, 2ed, 1999.
[16] I. Foster; Designing and Building Parallel Programs. Addison-Wesley Inc.,
Argonne National Laboratory, and the NSF, 2004.
antoniocarrillo@ciencias.unam.mx
56
[17] Jorge L. Ortega-Arjona, Patterns for Parallel Software Design, Wiley series
in Software Design Patterns, 2010.
[18] DDM Organization, Proceedings of International Conferences on Domain
Decomposition Methods, 1988-2012.
http://www.ddm.org and http://www.domain-decomposition.com
[19] Toselli, A., and Widlund O. Domain decomposition methods- Algorithms
and theory, Springer Series in Computational Mathematics, SpringerVerlag, Berlin, 2005, 450p.
[20] Farhat, C. and Roux, F. X. A Method of Finite Element Tearing and Interconnecting and its Parallel Solution Algorithm. Int. J. Numer. Meth.
Engng., 32:1205-1227, 1991.
[21] Mandel J. & Tezaur R. Convergence of a Substructuring Method with Lagrange Multipliers, Numer. Math. 73 (1996) 473-487.
[22] Farhat C., Lesoinne M. Le Tallec P., Pierson K. & Rixen D. FETI-DP a
Dual-Primal Unified FETI method, Part 1: A Faster Alternative to the
two-level FETI Method, Int. J. Numer. Methods Engrg. 50 (2001) 15231544.
[23] Farhat C., Lesoinne M., Pierson K. A Scalable Dual-Primal Domain Decomposition Method, Numer. Linear Algebra Appl. 7 (2000) 687-714.
[24] Mandel J. & Tezaur R. On the Convergence of a Dual-Primal Substructuring Method, Numer. Math. 88(2001), pp. 5443-558.
[25] Mandel, J. Balancing Domain Decomposition. Comm. Numer. Meth. Engrg., 9:233-241, 1993.
[26] Mandel J., & Brezina M., Balancing Domain Decomposition for Problems
with Large Jumps in Coefficients, Math. Comput. 65 (1996) 1387-1401.
[27] Dohrmann C., A Preconditioner for Substructuring Based on Constrained
Energy Minimization. SIAM J. Sci. Comput. 25 (2003) 246-258.
[28] Mandel J. & Dohrmann C., Convergence of a Balancing Domain Decomposition by Constraints and Energy Minimization. Numer. Linear Algebra
Appl. 10 (2003) 639-659.
[29] Da Conceio, D. T. Jr., Balancing Domain Decomposition Preconditioners for Non-symetric Problems, Instituto Nacional de Matemtica pura e
Aplicada, Agencia Nacional do Petrleo PRH-32, Rio de Janeiro, May. 9,
2006.
[30] J. Li and O. Widlund, FETI-DP, BDDC and block Cholesky Methods, Int.
J. Numer. Methods Engrg. 66, 250-271, 2005.
antoniocarrillo@ciencias.unam.mx
57
[31] Farhat Ch., Lesoinne M., Le Tallec P., Pierson K. and Rixen D. FETI-DP:
A Dual-Primal Unified FETI Method-Part I: A Faster Alternative to the
Two Level FETI Method. Internal. J. Numer. Methods Engrg., 50:15231544, 2001.
[32] Rixen, D. and Farhat Ch. A Simple and Efficient Extension of a Class of
Substructure Based Preconditioners to Heterogeneous Structural Mechanics
Problems. Internal. J. Numer. Methods Engrg., 44:489-516, 1999.
[33] J. Mandel, C. R. Dohrmann, and R. Tezaur, An Algebraic Theory for Primal and Dual Substructuring Methods by Constraints, Appl. Numer. Math.,
54 (2005), pp. 167-193.
[34] A. Klawonn, O. B. Widlund, and M. Dryja, Dual-primal FETI Methods
for Three-Dimensional Elliptic Problems with Heterogeneous Coefficients,
SIAM J. Numer. Anal., 40 (2002), pp. 159-179.
[35] Alberto Rosas Medina, Mtodos de Estabilizacin para Problemas de
Adveccin-Difusin, Trabajo de Investigacin para Sustentar el Examen
de Candidaura al Doctorado, Postgrado en Ciencias de la Tierra, UNAM,
2011.
[36] Klawonn A. and Widlund O.B., FETI and Neumann-Neumann Iterative
Substructuring Methods: Connections and New Results. Comm. Pure and
Appl. Math. 54(1): 57-90, 2001.
[37] Tezaur R., Analysis of Lagrange Multipliers Based Domain Decomposition.
P.H. D. Thesis, University of Colorado, Denver, 1998.
[38] Herrera I. & Rubio E., Unified Theory of Differential Operators Acting
on Discontinuos Functions and of Matrices Acting on Discontinuous Vectors, 19th International Conference on Domain Decomposition Methods,
Zhangjiajie, China 2009. (Oral presentation). Internal report #5, GMMCUNAM, 2011.
[39] Valeri I. Agoshkov, Poincar-Steklov Operators and Domain Decomposition
Methods in Finite Dimensional Spaces. First International Symposium on
Domain Decomposition Methods for Partial Differential Equations, pages
73-112, Philadelphia, PA, 1988. SIAM. Paris, France, Junuary 7-9, 1987.
[40] Toselli, A., FETI Domain Decomposition Methods for Escalar AdvectionDiffusion Problems. Compututational Methods Appl. Mech. Engrg. 190.
(2001), 5759-5776.
[41] C.T. Keller, Iterative Methods for Linear and Nonlinear Equations, Societe
for Industrial and Applied Mathematics, 1995.
[42] Manoj Bhardwaj, David Day, Charbel Farhat, Michel Lesoinne, Kendall
Pierson, and Daniel Rixen. Application of the PETI Method to ASCI Problems: Scalability Results on One Thousand Processors and Discussion
antoniocarrillo@ciencias.unam.mx
58
antoniocarrillo@ciencias.unam.mx
59