Está en la página 1de 19

TRABAJO COLABORATIVO No 2 1

Trabajo Colaborativo No 2

Evaluacin unidad 2

Unidad 2. Ecuaciones lineales e interpolacin.

Jos Fernando Bolaos Ramrez

Ingeniera en Electrnica

Mtodos numricos/Curso 48

Marzo 28 de 2017, Santiago de Cali

Tutor Jess Omar vargas


Mtodos numricos 2

Introduccin

En este trabajo colaborativo se desarrollaran los ejercicios contenidos en la gua de

actividades de la unidad 2: Eliminacin de Gauss, Gauus- Jordan, Mtodo de Jacobi, Mtodo de

Gauss Sedel, Polinomios de Lagrange, Diferencias Divididas, Aproximacin Polinomial de

Newton y Polinomio de Newton en diferencias finitas. Los resultados se presentaran en lo

posible en forma de codigo en Python y de manera analtica tal como lo exige la gua.
Mtodos numricos 3

Evaluacin unidad 2

Unidad 2. Ecuaciones lineales e interpolacin.

Punto 1.

Solucione el siguiente sistema de ecuaciones utilizando los Mtodo de eliminacin de Gauss.

2 x1 6 x 2 +4 x3 8 x 4= 66
2 x 1 +4 x2 +2 x3 +3 x 4 = 40
7 x1 7 x 2 5 x 3 +7 x 4 = 92
9 x 19 x 2+9 x37 x 4 = 32
Algoritmo

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
# gauss.py
#
# Copyright 2017 Jose Fernando <perseo@Doom>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# fila 4 x 4 Mtodos numricos UNAD
#

import numpy as np

fila = np.zeros((4, 5))


x = [0, 0, 0, 0]
Mtodos numricos 4

def f(xi):
if(xi == 0): return [ 2, -6, 4, -8, 66]
if(xi == 1): return [ 2, 4, 2, 3, -40]
if(xi == 2): return [ 7, -7, -5, 7, 92 ]
if(xi == 3): return [ -9, -9, 9, -7, 32 ]

def formatearfila():
index = [0, 1, 2, 3]
for i in index:
fila[i] = f(i)

def gauss_eliminar():
orden = 4
while (orden != 0):
i = orden - 1
while(i != 0):
m = -fila[4 -i][4-orden] / fila[4 - orden][4 - orden]
fila[4 - i] = fila[4 - i] + m * fila[4 - orden]
i=i-1
orden = orden - 1

def gauss_reducir():
x[3] = round(fila[3][4] / fila[3][3])
x[2] = round((fila[2][4] - (fila[2][3]*x[3]))/fila[2][2])
x[1] = round((fila[1][4] - fila[1][3]*x[3] - fila[1][2]*x[2]) / fila[1][1]) x[0] =
round((fila[0][4] - fila[0][3]*x[3] - fila[0][2]*x[2] - fila[0][1]*x[1]) / fila[0][0])

formatearfila()
gauss_eliminar()
gauss_reducir()
print fila
print "\n", x

>>>[perseo@Doom metodos]$ python2 gauss_metodo.py

[[ 2. -6. 4. -8. 66. ]


[ 0. 10. -2. 11. -106. ]
[ 0. 0. -16.2 19.6 9.4 ]
[ 0. 0. 0. 20.55555556 -41.11111111]]

[4.0, -9.0, -3.0, -2.0]


Mtodos numricos 5

A continuacin el mtodo analtico

Paso 1 eliminacin.

Transformando el sistema a un formato matricial

[[ 2 -6 4 -8 66 ]
[ 2 4 2 3 -40 ]
[ 7 -7 -5 7 92 ]
[ -9 -9 9 -7 32 ]]

Realizando las siguientes operaciones utilizando la fila [0] como pivote

fila[1] = fila[1] + (-1 * fila[0])

fila[2] = fila[2] + (-3.5 * fila[0])

fila[3] = fila[3] + ( 4.5 * fila[0])

[[ 2 -6 4 -8 66 ]
[ 0 10 -2 11 -106 ]
[ 0 14 -19 35 -139 ]
[ 0 -36 27 -43 329 ]

Realizando las siguientes operaciones utilizando la fila [1] como pivote

fila[2] = fila[2] + (-1.4 * fila[1])

fila[3] = fila[3] + ( 3.6 * fila[1])

[[ 2. -6. 4. -8. 66. ]


[ 0. 10. -2. 11. -106. ]
[ 0. 0. -16.2 19.6 9.4 ]
[ 0. 0. 19.8 -3.4 -52.6]]

Realizando la siguiente operacion utilizando la fila [2] como pivote

fila[3] = fila[3] + ( -19.8/16.2 * fila[2])

[[ 2 -6 4 -8 66 ]
[ 0 10 -2 11 -106 ]
[ 0 0 -16.2 19.6 9. 4 ]
[ 0 0 0 20.55555556 -41.11111111]]
Mtodos numricos 6

Retornando al formato de sistema de ecuaciones:

2 x 16 x 2+ 4 x38 x 4 = 66
10 x2 2 x3 +11 x 4= 106
16.2 x 3 +19.6 x 4= 9.4
20.55555556 x 4 = 41.11111111
Paso 2 reducir.

20.55555556 x 4= 41.11111111

41.11111111
x 4=
20.55555556

x 4= 2

16.2 x 3 +19.6(2)= 9.4

(9.419.6(2))
x 3=
16.2

x 3=3

10 x 22(3)+11(2)= 106

(106+2(3)11(2))
x 2=
10

x 2= 9

2 x 16(9)+4 (3)8 (2)= 66

(66(6 (9)+4 (3)8 (2)))


x 1=
2

x 1= 4

Valores de las incognitas:

x 1= 4 x 2= 9 x 3= 3 x 4 = 2
Mtodos numricos 7

Punto 2.

Solucione el siguiente sistema de ecuaciones utilizando los Mtodo de eliminacin de Gauss-

Jordan.

5 x 1 +2 x 2+5 x 3 +6 x 4= 90
1 x 16 x 2 +4 x 3+5 x 4 = 104
3 x 15 x 2 7 x 3 3 x4 = 63
2 x 15 x 29 x39 x 4 = 108
Algoritmo

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
# gauss_jordan.py
#
# Copyright 2017 Jose Fernando <perseo@Doom>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# fila 4 x 4 Mtodos numricos UNAD
#

import numpy as np

fila = np.zeros((4, 5))


x = [0, 0, 0, 0]
Mtodos numricos 8

def f(xi):
if(xi == 0): return [ 5., 2., 5., 6., -90. ]
if(xi == 1): return [ -1., -6., 4., 5., -104.]
if(xi == 2): return [ -3., -5., -7., -3., 63. ]
if(xi == 3): return [ 2., -5., -9., -9., 108.]

def formatearfila():
index = [0, 1, 2, 3]
for i in index:
fila[i] = f(i)

def triangular():
orden = 4
while (orden != 0):
i = orden - 1
while(i != 0):
m = -fila[4 -i][4-orden] / fila[4 - orden][4 - orden]
fila[4 - i] = fila[4 - i] + m * fila[4 - orden]
i=i-1
orden = orden - 1

def inv_triangular():
orden = 4
while (orden != 0):
i = orden - 1
while(i != 0):
m = -fila[i - 1][orden - 1] / fila[orden - 1][orden - 1]
fila[i - 1] = fila[i - 1] + m * fila[orden - 1]
i=i-1
orden = orden - 1

def gauss_reducir():
x[3] = round(fila[3][4] / fila[3][3])
x[2] = round((fila[2][4] - (fila[2][3]*x[3]))/fila[2][2])
x[1] = round((fila[1][4] - fila[1][3]*x[3] - fila[1][2]*x[2]) / fila[1][1]) x[0] =
round((fila[0][4] - fila[0][3]*x[3] - fila[0][2]*x[2] - fila[0][1]*x[1]) / fila[0][0])

formatearfila()
triangular()
inv_triangular()
gauss_reducir()

print fila
print x
Mtodos numricos 9

>>>[perseo@Vector metodos]$ python2 gauss_metodo.py


[[ 5. 0. 0. 0. -15. ]
[ 0. -5.6 0. 0. - 33.6 ]
[ 0. 0. -7.39285714 0. 66.53571429]
[ 0. 0. 0. -9.92753623 69.49275362]]

[-3.0, 6.0, -9.0, -7.0]

A continuacin el mtodo analtico

5 x 1 +2 x 2+5 x 3 +6 x 4= 90
1 x 16 x 2 +4 x 3+5 x 4 = 104
3 x 15 x 2 7 x 3 3 x4 = 63
2 x 15 x 29 x39 x 4 = 108
Paso 1 eliminacin

Escribimos el sistema de ecuaciones en un formato matricial

[[ 5. 2. 5. 6. -90.]
[ -1. -6. 4. 5. -104.]
[ -3. -5. -7. -3. 63.]
[ 2. -5. -9. -9. 108.]]

Generamos la fila triangular superior realizando las siguientes operaciones utilizando la fila [0]

como pivote

fila[1] = fila[1] + ( 0.2 * fila[0])

fila[2] = fila[2] + ( 0.6 * fila[0])

fila[3] = fila[3] + ( -0.4 * fila[0])

[[ 5. 2. 5. 6. -90. ]
[ 0. -5.6 5. 6.2 -122. ]
[ 0. -3.8 -4. 0.6 9. ]
[ 0. -5.8 -11. -11.4 144. ]]
Mtodos numricos 10

Utilizando la fila [1] como pivote

fila[2] = fila[2] + (-f1 * fila[1])


fila[3] = fila[3] + (-f2 * fila[1])

[[ 5. 2. 5. 6. -90. ]
[ 0. -5.6 5. 6.2 -122. ]
[ 0. 0. -7.39285714 -3.60714286 91.78571429]
[ 0. 0. -16.17857143 -17.82142857 270.35714286]]

Utilizando la fila [2] como pivote

fila[3] = fila[3] + ( -2.1884057971* fila[2])

[[ 5. 2. 5. 6. -90. ]
[ 0. -5.6 5. 6.2 -122. ]
[ 0. 0. -7.39285714 -3.60714286 91.78571429]
[ 0. 0. 0. -9.92753623 69.49275362]]

Reducimos para generar la matriz triangular inferior utilizando la fila[3] como pivote

fila[2] = fila[2] + ( -0.363347236705* fila[3])


fila[1] = fila[1] + ( --0.624525547445* fila[3])
fila[0] = fila[0] + ( --0.604379562044* fila[3])

[[ 5. 2. 5. 0. -48. ]
[ 0. -5.6 5. 0. -78.6 ]
[ 0. 0. -7.39285714 0. 66.53571429]
[ 0. 0. 0. -9.92753623 69.49275362]]

Utilizando la fila[2] como pivote

fila[1] = fila[1] + (0.676328502415 * fila[2])


fila[0] = fila[0] + (0.676328502415 * fila[2])

[[ 5. 2. 0. 0. -3. ]
[ 0. -5.6 0. 0. -33.6 ]
[ 0. 0. -7.39285714 0. 66.53571429]
[ 0. 0. 0. -9.92753623 69.49275362]]
Mtodos numricos 11

Utilizando la fila [1] como pivote

matriz[0] = matriz[0] + ( --0.357142857143 * matriz[1])

[[ 5. 0. 0. 0. -15. ]
[ 0. -5.6 0. 0. -33.6 ]
[ 0. 0. -7.39285714 0. 66.53571429]
[ 0. 0. 0. -9.92753623 69.49275362]]

Reducimos

[[ 1 0. 0. 0. -3.]
[ 0. 1 0. 0. 6.]
[ 0. 0. 1 0. -9.]
[ 0. 0. 0. 1 -7.]]

Valores de las incognitas:

x 1= 3 x 2= 6 x 3= 9 x 4 = 7

Punto 3

Solucione el siguiente sistema de ecuaciones utilizando el Mtodo de Jacobi.

3 x yz=1
x +3 y + z=3
2 x + y +4 z=7
Utilizar un < 1%

Para resolver este ejercicio uso como fuente la siguiente referencia:

Jacobi Method - Math-Linux.com. Accessed April 6, 2017. https://math-

linux.com/mathematics/linear-systems/article/jacobi-method.
Mtodos numricos 12

Usando la siguiente expresin como frmula general para realizar la iteracin

x(k +1)= D 1 ( L+U ) x( k) + D1 b

Usando una forma matricial para representar el sistema

[ ][]
3 1 1 1
1 3 1 3
2 1 4 7
donde A

[ ]
3 1 1
1 3 1
2 1 4
y b

[ 1 3 7]

Luego segn nuestro curso de algebra lineal:

[ ] [ ] [ ]
3 0 0 0 0 0 0 1 1
D = 0 3 0 L = 1 0 0 U = 0 0 1
0 0 4 2 1 0 0 0 0

[ ]
1
1 /3 0 0
D = 0 1/ 3 0
0 0 1/ 4
Mtodos numricos

Con los datos recolectados generamos la expresin de matrices

x(k +1) = D1 ( L +U ) x( k )+ D1 b

[ ][[ ] [ ]] [ ] []
1/3 0 0 0 0 0 0 1 1 1/3 0 0 1
0 1 /3 0 1 0 0 0 0 1 0 1 /3 0 3
X(k+1) = 0 0 1/ 4 2 1 0 + 0 0 0 X(k) + 0 0 1/ 4 7

[ ][[ ]] [ ]
1/3 0 0 0 1 1 1/ 3
0 1 /3 0 1 0 1 1
X(k+1) = 0 0 1/ 4 2 1 0 X(k) + 7/4

[ ] [ ]
0 1 /3 1/3 1/ 3
(k+1)
X = 1/3 0 1/3 X(k) + 1
1/2 1 / 4 0 7/ 4

[ ] [ ][ ] [ ]
( k+1) ( k)
X 0 1 /3 1/ 3 X 1/ 3
Y ( k+1) = 1/3 0 1/3 Y ( k) + 1
Z(k +1) 1/2 1 / 4 0 Z(k ) 7/ 4
Mtodos numricos 14

[ ][ ][ ]
( k+1)
X 1/ 3 yk 1/ 3 z k 1/ 3
k k
Y ( k+1) =
1/ 3 x 1/3 z + 1
Z(k +1) k
1 /2 x 1/ 4 y
k 7/4

[ ][ ]
( k+1)
X 1 /3 + 1 /3 y k +1 /3 z k
k k
Y ( k+1) =
1 + 1/3 x 1 /3 z
k k
Z(k +1) 7 / 4 1 /2 x 1 /4 y

Obteniendo entonces las frmulas de iteracin para el mtodo de Jacobi

X(k+1) = 1/3 + 1/3 yk + 1/3 zk

Y(k+1) = 1 + 1/3 xk - 1/3 z k

Z(k+1) = 7/4 1/2 xk 1/4yk

Iniciando con los siguientes valores:

x0 = 0 X(1) = 1/3 + 1/3 y(0) + 1/3 z(0) X(1) = 1/3

y0 = 0 Y(1) = 1 + 1/3 x(0) - 1/3 z (0) Y(1) = 1

z0 = 0 Z(1) = 7/4 1/2 x(0) 1/4y(0) Z(1) = 7/4


Mtodos numricos 15

Al ser un mtodo iterativo, que se repite n veces podemos hacer un corto algoritmo que

simplifique nuestro trabajo.

[perseo@Doom Documents]$ python2


Python 2.7.13 (default, Dec 21 2016, 07:16:46)
[GCC 6.2.1 20160830] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>x = 0
>>>y = 0
>>>z = 0
>>>i = 0

>>>while(i < 20):


>>> print x
>>> print y
>>> print z
>>> xn = 1/3. + 1/3. * y + 1/3. * z
>>> yn = 1. + 1/3. * x - 1/3. * z
>>> zn = 7/4. - 1/2. * x - 1/4. * y
>>> print " "
>>> i = i + 1
>>> x = xn
>>> y = yn
>>> z = zn

Generando los datos necesarios para construir la siguiente tabla

Iteracin No Xn Yn Zn
0 0 0 0
1 0.333333333333 1.0 1.75
2 1.25 0.527777777778 1.33333333333
3 0.953703703704 0.972222222222 0.993055555556
4 0.988425925926 0.986882716049 1.03009259259
5 1.00565843621 0.986111111111 1.00906635802
6 0.998392489712 0.998864026063 1.00064300412
7 0.999835676726 0.999249828532 1.00108774863
8 1.00011252572 0.999582642699 1.0002697045
9 0.999950782401 0.999947607072 1.00004807647
10 0.999998561179 0.999967568645 1.00003770703
11 1.00000175856 0.999986951383 1.00000882725
12 0.999998592877 0.99999764377 1.00000238287
13 1.00000000888 0.999998736667 1.00000129262
14 1.00000000976 0.999999572088 1.00000031139
15 0.99999996116 0.999999899457 1.0000001021

Obteniendo como resultado que:

X = 0.99999996116 Y = 0.999999899457 Z = 1.0000001021


Mtodos numricos 16

Podemos apreciar en la siguiente grfica como se comportan los valores de la tabla

from mpl_toolkits.mplot3d import Axes3D


import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x =[0, 0.333333333333,
1.25 ,0.953703703704, 0.988425925926 ,
1.00565843621, 0.998392489712,
0.999835676726, 1.00011252572 ,
0.999950782401, 0.999998561179,
1.00000175856, 0.999998592877,
1.00000000888, 1.00000000976,
0.99999996116]
y =[0, 1.
0, 0.527777777778,
0.972222222222, 0.986882716049,
0.986111111111, 0.998864026063,
0.999249828532, 0.999582642699,
0.999947607072, 0.999967568645,
0.999986951383, 0.99999764377,
0.999998736667, 0.999999572088,
0.999999899457]
z =[0, 1.75,
1.33333333333, 0.993055555556,
1.03009259259, 1.00906635802,
1.00064300412, 1.00108774863,
1.0002697045, 1.00004807647,
1.00003770703, 1.00000882725,
1.00000238287, 1.00000129262,
1.00000031139, 1.0000001021]

ax.scatter(x, y, z, c='r', marker='o')

ax.set_xlabel('Eje X')
ax.set_ylabel('Eje Y')
ax.set_zlabel('Eje Z')

plt.show()

Gracias a la grfica podemos observar como los diferentes valores convergen hacia el punto

1,1,1
Mtodos numricos 17

Punto 4

Solucione el siguiente sistema de ecuaciones utilizando el Mtodo de Gauss-Seidel

0.1 x1 +7 x 20.3 x 3= 19.3


3 x1 0.1 x2 0.2 x 3= 7.85
0.3 x1 0.2 x2 10 x3 =71.4

Para un error < 1%

Reordenamos el sistema

3 x1 0.1 x2 0.2 x 3= 7.85


0.1 x1 +7 x 20.3 x 3= 19.3
0.3 x1 0.2 x2 10 x3 =71.4

De esta manera logramos que en la diagonal esten los coeficientes de mayor valor y as

aseguramos la convergencia. En terminos de matrices, la definicin del metodo de Gauss-Seidel

puede expresarse como:

x(k) = (D L)-1 (Ux(k-1) + b)

Luego:

x1 = 1/3 * 7.85 + 0.1x2 + 0.2x3

x2 = 1/7 * -19.3 - 0.1x1 + 0.3x3

x3 = 1/10 * 71.4 - 0.3x1 + 0.2x2

Iniciamos la secuencia con valores para x2 = x3 = 0, calculamos x1

x1 = 1/3 * 7.85 + 0.1(0) + 0.2(0)

x1 = 2.6167
Mtodos numricos 18

Reemplazamos en x2

x2 = 1/7 * -19.3 - 0.1(157/60) + 0.3(0)

x2 = -2.7945

Reemplazamos en x3

x3 = 1/10 * (71.4 - 0.3*x1 + 0.2*x2)

x3 = 7.0056

Primera iteracin

x1 = 1/3 * (7.85 + 0.1*x2 + 0.2*x3)

x1 = 2.9906

Reemplazamos en x2

x2 = 1/7 * (-19.3 - 0.1*x1 + 0.3*x3)

x2 = -2.4996

Reeplazamos en x3

x3 = 1/10 * (71.4 - 0.3*x1 + 0.2*x2)

x3 = 7.0003

Segunda iteracin

x1 = 1/3 * (7.85 + 0.1*x2 + 0.2*x3)

x1 = 3.0000

Reemplazamos en x2

x2 = 1/7 * (-19.3 - 0.1*x1 + 0.3*x3)

x2 = -2.5000

Reeplazamos en x3

x3 = 1/10 * (71.4 - 0.3*x1 + 0.2*x2)

x3 = 7.0000
Mtodos numricos 19

Comparamos los valores obtenidos de las dos lrimas iteraciones para cumplir con la condicin

de error.

X 1 |3.0002.9906|=9.4e-3
E

X 2 |2.5000(2.4996)|=4.0000e-04
E

X 3 |7.00007.0003|=3.0000e-04
E

En este punto se cumple con las condiciones.

x1 = 3.0000

x2 = -2.5000

x3 = 7.0000

Podemos observar en la grfica como los valores convergen a los puntos encontrados

También podría gustarte