Métodos Númericos 2 Parcial

30
etodos Num´ ericos Hua Lun Barrigas David Jonathan Gualoto Departamento de Ciencias Exactas Nrcs: 4309 Docente Mgs. Fabi´ an Ordo˜ nez 30 de Junio de 2015 Trabajo Segundo Parcial 1. Desarrolle un c´ odigo que permita calcular el producto entre matrices odigo del Programa function [C] = Multmatr( A,B ) %A=[1 2; 3 4] %B=[1 2; 2 3] %Como utilizar el Codigo %[C] = Multmatr([1 2; 3 4],[1 2; 2 3]) [m,n]=size(A); [p,l]=size(B); if (n==p) for i=1:1:m for k=1:1:l j=k; C(i,j)=A(i,:)*B(:,j); end end 1

description

Universidad de las Fuerzas Armadas

Transcript of Métodos Númericos 2 Parcial

Page 1: Métodos Númericos 2 Parcial

Metodos Numericos

Hua Lun Barrigas

David Jonathan Gualoto

Departamento de Ciencias ExactasNrcs: 4309

Docente Mgs. Fabian Ordonez

30 de Junio de 2015

Trabajo Segundo Parcial

1. Desarrolle un codigo que permita calcular el producto entre matrices

Codigo del Programa

function [C] = Multmatr( A,B )

%A=[1 2; 3 4]

%B=[1 2; 2 3]

%Como utilizar el Codigo

%[C] = Multmatr([1 2; 3 4],[1 2; 2 3])

[m,n]=size(A);

[p,l]=size(B);

if (n==p)

for i=1:1:m

for k=1:1:l

j=k;

C(i,j)=A(i,:)*B(:,j);

end

end

1

Page 2: Métodos Númericos 2 Parcial

else

disp(’NO EXISTE PRODUCTO PARA LA DIMENSION DE LAS MATRICES DADAS’);

end

end

Ejecucion del programa

>> [C] = Multmatr([1 2 3; 3 4 3; 3 5 2],[1 3 2;3 2 3;5 2 1])

C =

22 13 11

30 23 21

28 23 23

2. Desarrolle un algoritmo que permita determinar la transpuesta de una matriz.

Desarrollo de algorıtmo a11 a12 a13 − a1ma21 a22 a23 − a2ma31 a32 a33 − a3m| | | − |

an1 an2 an3 − anm

a12 ↔ a21

a13 ↔ a31

a23 ↔ a32

a1m ↔ an1

a2m ↔ an2

a3m ↔ an3

n=tamano de filas de la matrizm=tamano de columnas de la matriz

for i : 1 : nfor j : 1 : mA(i, j) = B(j, i)

Codigo del programa

function [ B ] = transpuesta( A )

%[ B ] = transpuesta([1 3 1 -4;5 1 -2 1;2 5 1 -2;5 2 3 1])

[n,m]=size(A);

A

for i=1:n

for j=1:m

B(j,i)=A(i,j);

end

end

end

2

Page 3: Métodos Númericos 2 Parcial

Ejecucion del programa

>> [ B ] = transpuesta([1 3 1 -4;5 1 -2 1;2 5 1 -2;5 2 3 1])

A =

1 3 1 -4

5 1 -2 1

2 5 1 -2

5 2 3 1

B =

1 5 2 5

3 1 5 2

1 -2 1 3

-4 1 -2 1

3. Desarrolle un algoritmo que permita determinar el producto escalar.

Algoritmo del Programa

function [D] = Productoesc(a,b)

%a=[1 2 3]

%b=[3 2 1]

%Como ingresar los valores

%Productoesc([1 2 3],[3 2 1])

[n,m]=size(a);

[l,p]=size(b);

D=0;

if m==p

for i=1:1:n

for k=1:1:m

j=k;

e=a(i,j)*b(i,j);

D=D+e;

end

end

else

disp(’No Se puede realizar producto escalar para las dimensiones dadas’)

end

Ejecucion del programa

3

Page 4: Métodos Númericos 2 Parcial

>> Productoesc([1 2 3],[3 2 1])

D =

10

4. Desarrolle un codigo que calcule la norma de un vector.

Codigo del programa

function [ B ] = ejr4norma( A )

%[ B ] = ejr4norma([1 2 1 -4])

[m,n]=size(A);

A

B=A;

R=0;

for i=1:1:n

R=(A(1,i)).^2+R;

end

B=sqrt(R);

end

Ejecucion del programa

>> [ B ] = ejr4norma([1 2 1 -4])

A =

1 2 1 -4

B =

4.6904

5. Construya un codigo que determine la traza de una matriz.

Codigo del Programa

function [t] = Traza( A )

%A=[1 2 3 ; 3 3 2; 2 4 1]

%Como utilizarlo

%[t] = Traza([1 2 3 ; 3 3 2; 2 4 1])

[n,n]=size(A);

t=0;

for k=1:1:n

i=k;

j=k;

a=A(i,j);

t=t+a;

4

Page 5: Métodos Númericos 2 Parcial

end

end

Ejecucion del programa

>>[t] = Traza([1 2 3 ; 3 3 2; 2 4 1])

t =

5

6. Construya un algoritmo para determinar la inversa de una matriz A, siendo una matriz nosingular.

Codigo del programa

function [inversa] = ejr6gaussjordaninversa(A,b)

%[inversa] = ejr6gaussjordaninversa([1 1 1;2 -1 3;3 2 -2],[eye(3)])

[n,n]=size(A);

B=eye(n);

Ab=[A b]; %matriz ampliada entre A y b

for k=1:n-1

[p,q]=max(abs(Ab(k:n,k)));

if p==0

error(’La matriz es singular’)

end

m=k+q-1;

Ab=cambiofilas(Ab,k,m);

for j=k+1:n

Ab=combinacionfilas(Ab,k,j,-Ab(j,k)/Ab(k,k));

end

end

for k=n:-1:2

for j=k-1:-1:1

Ab=combinacionfilas(Ab,k,j,-Ab(j,k)/Ab(k,k));

end

end

C=Ab;

for k=1:n

C(k,:)=C(k,:)/C(k,k);

end

x=matriztrianinf(C(:,1:n),C(:,n+1));

h=1;

x=matriztriansup(Ab(:,1:n),Ab(:,n+1));

inversa=C(1:n,(n+1):2*n);

end

Ejecucion del programa

>> [inversa] = ejr6gaussjordaninversa([1 1 1;2 -1 3;3 2 -2],[eye(3)])

5

Page 6: Métodos Númericos 2 Parcial

inversa =

-0.2500 0.2500 0.2500

0.8125 -0.3125 -0.0625

0.4375 0.0625 -0.1875

7. Halle la solucion de los siguientes sistemas lineales, utilizando el metodo de Gauss, Gauss-Jordan, compruebe su solucion utilizando los codigos desarrollados para ello.

Gauss

x1 + 2x2 = 7

2x1 + 3x2 − x3 = 9

4x2 + 2x3 + 3x4 = 10

2x3 − 4x4 = 12

1 2 0 0 | 72 3 −1 0 | 90 4 2 3 | 100 0 2 −4 | 12

F2⇔ F1

2 3 −1 0 | 91 2 0 0 | 70 4 2 3 | 100 0 2 −4 | 12

F2 = F2−(

1

2

)F1

2 3 −1 0 | 90 1

212

0 | 52

0 4 2 3 | 100 0 2 −4 | 12

F3 = F3−(

8

7

)F2

2 3 −1 0 | 90 1

212

0 | 52

0 0 −2 3 | −100 0 2 −4 | 12

F4 = F4−F3

2 3 −1 0 | 90 1

212

0 | 52

0 0 −2 3 | −100 0 0 −1 | 2

2x1 + 3x2 − x3 = 9 (1)

1

2x2 +

1

2x3 =

5

2(2)

−2x3 + 3x4 = −10 (3)

−x4 = 2 (4)

(4) en (3)

−2x3 + 3(−2) = −10

x3 = 2 (3)

6

Page 7: Métodos Númericos 2 Parcial

(3) en (2)

1

2x2 +

1

2(2) =

5

2

x2 = 3 (2)

(3) & (2) en (1)

2x1 + 3(3)− (2) = 9

x1 = 1

X =

132−2

Gauss Jordan

1 2 0 0 | 72 3 −1 0 | 90 4 2 3 | 100 0 2 −4 | 12

F2⇔ F1

2 3 −1 0 | 91 2 0 0 | 70 4 2 3 | 100 0 2 −4 | 12

F2 = F2−(

1

2

)F1

2 3 −1 0 | 90 1

212

0 | 52

0 4 2 3 | 100 0 2 −4 | 12

F3 = F3−(

8

7

)F2

2 3 −1 0 | 90 1

212

0 | 52

0 0 −2 3 | −100 0 2 −4 | 12

F4 = F4−F3

2 3 −1 0 | 90 1

212

0 | 52

0 0 −2 3 | −100 0 0 −1 | 2

F3 = F3+3F4

2 3 −1 0 | 90 1

212

0 | 52

0 0 −2 0 | −40 0 0 −1 | 2

F2 = F2 +(14

)F3

F1 = F1−(12

)F3

2 3 0 0 | 110 1

20 0 | 3

2

0 0 −2 0 | −40 0 0 −1 | 2

F1 = F1− 6F2

2 0 0 0 | 20 1

20 0 | 3

2

0 0 −2 0 | −40 0 0 −1 | 2

F1 ∗ 1/2F2 ∗ 2

F3 ∗ −1/2F4 ∗ −1

1 0 0 0 | 10 1 0 0 | 30 0 1 0 | 10 0 0 1 | −2

X =

132−2

7

Page 8: Métodos Númericos 2 Parcial

Comprobacion MatLab

Codigo del Programa Metodo de Gauss

function [x] = gauss(A,b)

%[x] = gauss([8 1 6;3 5 7;4 9 2],[1;0;1])

[n,n]=size(A);

Ab=[A b]; %matriz ampliada entre A y b

for k=1:n-1

[p,q]=max(abs(Ab(k:n,k)));

if p==0

error(’La matriz es singular’)

return

end

m=k+q-1;%real ubicacion del elemento maximo y de la fila de cambio

Ab=cambiofilas(Ab,k,m);

for j=k+1:n

Ab=combinacionfilas(Ab,k,j,-Ab(j,k)/Ab(k,k));

end

end

Ab

h=1

x=matriztriansup(Ab(:,1:n),Ab(:,n+1));

fprintf(’El resultado del sistema de ecuaciones es:\n’)

for g=1:1:n

fprintf(’x%d=%d\n’,g,x(h,g))

end

end

Ejecucion del Programa

>> [x] = gauss([1 2 0 0;2 3 -1 0;0 4 2 3;0 0 2 -4],[7;9;10;12])

Ab =

Columns 1 through 4

2.0000 3.0000 -1.0000 0

0 4.0000 2.0000 3.0000

0 0 2.0000 -4.0000

0 0 0 0.1250

Column 5

9.0000

10.0000

8

Page 9: Métodos Númericos 2 Parcial

12.0000

-0.2500

El resultado del sistema de ecuaciones es:

x1=1

x2=3

x3=2

x4=-2

x =

1 3 2 -2

Codigo del Programa Metodo de Gauss-Jordan

function [C x] = gaussjordan(A,b)

%[C x] = gaussjordan([8 1 6;3 5 7;4 9 2],[1;0;1])

[n,n]=size(A);

Ab=[A b]; %matriz ampliada entre A y b

for k=1:n-1

[p,q]=max(abs(Ab(k:n,k)));

if p==0

error(’La matriz es singular’)

end

m=k+q-1;

Ab=cambiofilas(Ab,k,m);

for j=k+1:n

Ab=combinacionfilas(Ab,k,j,-Ab(j,k)/Ab(k,k));

end

end

for k=n:-1:2

for j=k-1:-1:1

Ab=combinacionfilas(Ab,k,j,-Ab(j,k)/Ab(k,k));

end

end

C=Ab;

for k=1:n

C(k,:)=C(k,:)/C(k,k);

end

x=matriztrianinf(C(:,1:n),C(:,n+1));

h=1;

x=matriztriansup(Ab(:,1:n),Ab(:,n+1));

fprintf(’El resultado del sistema de ecuaciones es:\n’)

for g=1:1:n

fprintf(’x%d=%d\n’,g,x(h,g))

end

end

9

Page 10: Métodos Númericos 2 Parcial

Ejecucion del Programa

>> [C x] = gaussjordan([1 2 0 0;2 3 -1 0;0 4 2 3;0 0 2 -4],[7;9;10;12])

El resultado del sistema de ecuaciones es:

x1=1

x2=3

x3=2

x4=-2

C =

1 0 0 0 1

0 1 0 0 3

0 0 1 0 2

0 0 0 1 -2

x =

1 3 2 -2

8. Halle la solucion del siguiente sistema lineal, utilizando el metodo de Gauss, Gauss Jordan,comprueba su solucion utilizando los codigos desarrollados para ello.

x1 + x2 + x3 = 12x1 − x2 + 3x3 = 4

3x1 + 2x2 − 2x3 = −2

Metodo de Gauss1 1 1 | 12 −1 3 | 43 2 −2 | −2

f3←→f1

3 2 −2 | −22 −1 3 | 41 1 1 | 1

f2 = f2−2

3f1

f3 = f3−1

3f1

3 2 −2 | −2

0 −7

3

13

3|

16

3

01

3

5

3|

5

3

f3 = f3 +1

7f2

3 2 −2 | −2

0 −7

3

13

3|

16

3

0 016

7|

17

7

ec. 1 :

16

7x3 =

17

7

x3 = 1716

= 1,0625

ec. 2 :

−7

3x2 +

13

3x3 =

16

3

−7

3x2 +

13

3

(1716

)=

16

3

10

Page 11: Métodos Númericos 2 Parcial

x2 = − 516

= −0,3125

ec. 3 :

3 · x1 + 2 · x2− 2 · x3 = −23 · x1 + 2 ·

(− 5

16

)− 2 ·

(1716

)= −2

x1 = 14

= 0,25

Metodo de Gauss Jordan1 1 1 | 12 −1 3 | 43 2 −2 | −2

f3←→f1

3 2 −2 | −22 −1 3 | 41 1 1 | 1

f2 = f2−2

3f1

f3 = f3−1

3f1

3 2 −2 | −2

0 −7

3

13

3|

16

3

01

3

5

3|

5

3

f3 = f3 +1

7f2

3 2 −2 | −2

0 −7

3

13

3|

16

3

0 016

7|

17

7

f2 = f2−91

48f3

f1 = f1 +7

8f3

3 2 0 |

1

8

0 −7

30 |

35

48

0 016

7|

17

7

f1 = f1 +6

7f2

3 0 0 |

3

4

0 −7

30 |

35

48

0 016

7|

17

7

ec. 1 :

16

7x3 =

17

7

x3 = 1716

= 1,0625

ec. 2 :

−7

3x2 =

35

48

x2 = − 516

= −0,3125

ec. 3 :

3 · x1 =3

4

x1 = 14

= 0,25

Comprobacion

Metodo de Gauss

Codigo del programa

11

Page 12: Métodos Númericos 2 Parcial

function [x] = ejr8gauss(A,b)

%[x] = ejr8gauss([1 1 1;2 -1 3;3 2 -2],[1;4;-2])

[n,n]=size(A);

Ab=[A b];%matriz ampliada entre A y b

for k=1:n-1

[p,q]=max(abs(Ab(k:n,k)));

if p==0

error(’La matriz es singular’)

return

end

m=k+q-1;%real ubicacion del elemento maximo y de la fila de cambio

Ab=cambiofilas(Ab,k,m);

for j=k+1:n

Ab=combinacionfilas(Ab,k,j,-Ab(j,k)/Ab(k,k));

end

end

Ab

x=matriztriansup(Ab(:,1:n),Ab(:,n+1));

h=1;

x=matriztriansup(Ab(:,1:n),Ab(:,n+1));

fprintf(’El resultado del sistema de ecuaciones es:\n’)

for g=1:1:n

fprintf(’x%d=%f\n’,g,x(h,g))

end

end

Ejecucion del programa

>> [x] = ejr8gauss([1 1 1;2 -1 3;3 2 -2],[1;4;-2])

Ab =

3.0000 2.0000 -2.0000 -2.0000

0 -2.3333 4.3333 5.3333

0 -0.0000 2.2857 2.4286

El resultado del sistema de ecuaciones es:

x1=0.250000

x2=-0.312500

x3=1.062500

x =

0.2500 -0.3125 1.0625

Metodo de Gauss Jordan

Codigo del programa

12

Page 13: Métodos Númericos 2 Parcial

function [C x] = ejr8gaussjordan(A,b)

%[C x] = ejr8gaussjordan([1 1 1;2 -1 3;3 2 -2],[1;4;-2])

[n,n]=size(A);

Ab=[A b]; %matriz ampliada entre A y b

for k=1:n-1

[p,q]=max(abs(Ab(k:n,k)));

if p==0

error(’La matriz es singular’)

end

m=k+q-1;

Ab=cambiofilas(Ab,k,m);

for j=k+1:n

Ab=combinacionfilas(Ab,k,j,-Ab(j,k)/Ab(k,k));

end

end

for k=n:-1:2

for j=k-1:-1:1

Ab=combinacionfilas(Ab,k,j,-Ab(j,k)/Ab(k,k));

end

end

C=Ab;

for k=1:n

C(k,:)=C(k,:)/C(k,k);

end

x=matriztrianinf(C(:,1:n),C(:,n+1));

h=1;

x=matriztriansup(Ab(:,1:n),Ab(:,n+1));

fprintf(’El resultado del sistema de ecuaciones es:\n’)

for g=1:1:n

fprintf(’x%d=%f\n’,g,x(h,g))

end

end

Ejecucion del programa

>> [C x] = ejr8gaussjordan([1 1 1;2 -1 3;3 2 -2],[1;4;-2])

El resultado del sistema de ecuaciones es:

x1=0.250000

x2=-0.312500

x3=1.062500

C =

1.0000 0 0 0.2500

0 1.0000 0 -0.3125

0 -0.0000 1.0000 1.0625

x =

0.2500 -0.3125 1.0625

13

Page 14: Métodos Númericos 2 Parcial

Comprobacion con remplazo

0,25 − 0,3125 + 1,0625 = 12(0,25) + 0,3125 + 3(1,0625) = 43(0,25) + 2(−0,3125) − 2(1,0625) = −2

Remplazando

1 = 14 = 4−2 = −2

9.-

x1+20x2 − x3 + 0, 001x4 = 0

2x1 − 5x2 + 30x3 − 0, 1x4 = 1

5x1 + x2 − 100x3 − 10x4 = 0

2x1−100x2 − x3 + x4 = 0

Metodo de Gauss

1 20 −1 0,001 | 02 −5 30 −0,1 | 15 1 −100 −10 | 02 −100 −1 1 | 0

F3⇔ F1F3⇔ F4

5 1 −100 10 | 02 −5 30 −0,1 | 12 −100 −1 1 | 01 20 −1 0,001 | 0

F2 = F2− 2/5F1F3 = F3− 2/5F1F4 = F4− 1/5F1

5 1 −100 10 | 00 −27/5 70 39/10 | 10 −502/5 39 5 | 00 99/5 19 2001/1000 | 0

F3 = F3− 502/27F2F4 = F4 + 11/3F2

5 1 −100 10 | 00 −27/5 70 39/10 | 10 −502/5 39 5 | 00 99/5 19 2001/1000 | 0

F3 = F3− 502/27F2F4 = F4 + 11/3F2

5 1 −100 10 | 00 −27/5 70 39/10 | 10 0 −34087/27 −3038/45 | −502/270 0 0 1,5597 | −0,3931

5x1 + x2 − 100x3 − 10x4 = 0 (1)

−27/5x2+70x3 + 39/10x4 = 1 (2)

−34087/27x3 − 3038/45x4 = −502/27 (3)

1, 5597x4 = −0, 3931 (4)

x4 = −0, 2520

14

Page 15: Métodos Númericos 2 Parcial

(4) en (3)

−34087/27x3 − 3038/45(−0, 2520) = −502/27

x3 = 0, 0282

(3) en (2)

−27/5x2+70(0, 0282) + 39/10(−0, 2520) = 1

x2 = −0, 0016

5x1 + (−0, 0016)− 100(0, 0282)− 10(−0, 2520) = 0

x1 = 0, 0604

X =

0, 0604−0, 00160, 0282−0, 2520

Comprobacion con el Programa Gauss

Codigo del Programa

function [x] = gauss(A,b)

%[x] = gauss([8 1 6;3 5 7;4 9 2],[1;0;1])

[n,n]=size(A);

Ab=[A b]; %matriz ampliada entre A y b

for k=1:n-1

[p,q]=max(abs(Ab(k:n,k)));

if p==0

error(’La matriz es singular’)

return

end

m=k+q-1;%real ubicacion del elemento maximo y de la fila de cambio

Ab=cambiofilas(Ab,k,m);

for j=k+1:n

Ab=combinacionfilas(Ab,k,j,-Ab(j,k)/Ab(k,k));

end

end

Ab

h=1;

x=matriztriansup(Ab(:,1:n),Ab(:,n+1));

15

Page 16: Métodos Númericos 2 Parcial

fprintf(’El resultado del sistema de ecuaciones es:\n’)

for g=1:1:n

fprintf(’x%d=%f\n’,g,x(h,g))

end

end

Ejecucion del Programa

>> [x] = gauss([1 20 -1 0.001;2 -5 30 -0.1;5 1 -100 -10;2 -100 -1 1],[0;1;0;0])

Ab =

5.0000 1.0000 -100.0000 -10.0000 0

0 -100.4000 39.0000 5.0000 0

0 0 67.9024 3.6311 1.0000

0 0 0 1.5597 -0.3931

El resultado del sistema de ecuaciones es:

x1=0.060357

x2=-0.001595

x3=0.028204

x4=-0.252017

x =

0.0604 -0.0016 0.0282 -0.2520

Gauss Jordan

1 20 −1 0,001 | 02 −5 30 −0,1 | 15 1 −100 −10 | 02 −100 −1 1 | 0

F3⇔ F1F3⇔ F4

5 1 −100 10 | 02 −5 30 −0,1 | 12 −100 −1 1 | 01 20 −1 0,001 | 0

F2 = F2− 2/5F1F3 = F3− 2/5F1F4 = F4− 1/5F1

5 1 −100 10 | 00 −27/5 70 39/10 | 10 −502/5 39 5 | 00 99/5 19 2001/1000 | 0

F3 = F3− 502/27F2F4 = F4 + 11/3F2

5 1 −100 10 | 00 −27/5 70 39/10 | 10 −502/5 39 5 | 00 99/5 19 2001/1000 | 0

F3 = F3− 502/27F2F4 = F4 + 11/3F2

5 1 −100 10 | 00 −27/5 70 39/10 | 10 0 −34087/27 −3038/45 | −502/270 0 0 1,5597 | −0,3931

16

Page 17: Métodos Númericos 2 Parcial

5 1 −100 10 | 00 −27/5 70 39/10 | 10 0 −34087/27 −3038/45 | −502/270 0 0 1,5597 | −0,3931

F1 = F1 + 6, 4115F4F2 = F2− 2, 5004F4F3 = F3 + 44, 7095

5 1 −100 0 | −2, 52020 −27/5 39 0 | 1, 26010 0 67, 9024 0 | 1, 915100 0 0 1,5597 | −0,3931

F1 = F1− 0, 5744F3F2 = F2 + 1, 4727F3

5 1 0 0 | 0, 30020 −100, 4000 0 0 | 0, 16010 0 67, 9024 0 | 1, 91510 0 0 1,5597 | −0,3931

F1 = F1 + 5/502F2

5 0 0 0 | 0, 30180 −100, 4000 0 0 | 0, 16010 0 67, 9024 0 | 1, 91510 0 0 1,5597 | −0,3931

∗1/5

∗1/100, 400∗1/67, 9024∗1/1, 5597

X =

0, 0604−0, 00159

0, 0282−0, 2520

Comprobacion con el Programa Gauss-Jordan

Codigo del Programa

function [C x] = gaussjordan(A,b)

%[C x] = gaussjordan([1 20 -1 0.001;2 -5 30 -0.1;5 1 -100 -10;2 -100 -1 1],[0;1;0;0])

[n,n]=size(A);

Ab=[A b]; %matriz ampliada entre A y b

for k=1:n-1

[p,q]=max(abs(Ab(k:n,k)));

if p==0

error(’La matriz es singular’)

end

m=k+q-1;

Ab=cambiofilas(Ab,k,m);

for j=k+1:n

Ab=combinacionfilas(Ab,k,j,-Ab(j,k)/Ab(k,k));

end

end

for k=n:-1:2

for j=k-1:-1:1

Ab=combinacionfilas(Ab,k,j,-Ab(j,k)/Ab(k,k));

end

end

17

Page 18: Métodos Númericos 2 Parcial

C=Ab;

for k=1:n

C(k,:)=C(k,:)/C(k,k);

end

x=matriztrianinf(C(:,1:n),C(:,n+1));

h=1;

x=matriztriansup(Ab(:,1:n),Ab(:,n+1));

fprintf(’El resultado del sistema de ecuaciones es:\n’)

for g=1:1:n

fprintf(’x%d=%d\n’,g,x(h,g))

end

end

Ejecucion del Programa

>> [C x] = gaussjordan([1 20 -1 0.001;2 -5 30 -0.1;5 1 -100 -10;2 -100 -1 1],[0;1;0;0])

El resultado del sistema de ecuaciones es:

x1=0.060357

x2=-0.001595

x3=0.028204

x4=-0.252017

C =

Columns 1 through 4

1.0000 0 0 0.0000

0 1.0000 0 0.0000

0 0 1.0000 0.0000

0 0 0 1.0000

Column 5

0.0604

-0.0016

0.0282

-0.2520

x =

0.0604 -0.0016 0.0282 -0.2520

10. Descomponer las siguientes matrices en matrices L y U y compruebe que PA = LU−5 2 −11 0 33 1 6

Matriz U

18

Page 19: Métodos Númericos 2 Parcial

−5 2 −11 0 33 1 6

f3↔ f2

−5 2 −13 1 61 0 3

f2 = f2 +

3

5f1

f3 = f3 +1

5f1

−5 2 −1

011

5

27

5

02

5

14

5

f3 = f3−2

11f2

−5 2 −1

011

5

27

5

0 020

11

Matriz L

1 0 00 1 00 0 1

1 0 0

−3

51 0

−1

50 1

1 0 0

−3

51 0

−1

5

2

111

Matriz P

1 0 00 1 00 0 1

f3↔ f2

1 0 00 0 10 1 0

Comprobacion PA=LU

1 0 00 0 10 1 0

·−5 2 −1

1 0 33 1 6

=

1 0 0

−3

51 0

−1

5

2

111

·−5 2 −1

011

5

27

5

0 020

11

−5 2 −1

3 1 61 0 3

=

−5 2 −13 1 61 0 3

Codigo del programa

function [L U P] = ejr10fac_LUP(A,B)

%[L U P] = ejr10fac_LUP([-5 2 -1;1 0 3;3 1 6],eye(3))

[n,n]=size(A);

L=eye(n);

P=eye(n);

for k=1:n-1

[a,b]=max(abs(A(k:n,k)));

if a==0

error(’La matriz es singular’)

return

end

m=k+b-1;%real ubicacion del elemento maximo y de la fila de cambio

A=cambiofilas(A,k,m);

19

Page 20: Métodos Númericos 2 Parcial

U=A;

P=cambiofilas(P,k,m);

for k=1:n-1

for j=k+1:n

mult=U(j,k)/U(k,k);

U(j,k:n)=U(j,k:n)-mult*U(k,k:n);

L(j,k)=mult;

end

end

end

M=P*B;

Y=gauss(L,M);

X=gauss(U,Y’);

end

Ejecucion del programa

>> [L U P] = ejr10fac_LUP([-5 2 -1;1 0 3;3 1 6],eye(3))

L =

1.0000 0 0

-0.6000 1.0000 0

-0.2000 0.1818 1.0000

U =

-5.0000 2.0000 -1.0000

0 2.2000 5.4000

0 0 1.8182

P =

1 0 0

0 0 1

0 1 0

11. 1 0 33 1 6−5 2 −1

1 0 3

3 1 6−5 2 −1

F3⇔ F1

−5 2 −13 1 61 0 3

Matriz De Permutacion.

1 0 00 1 00 0 1

F3⇔ F1

0 0 10 1 01 0 0

= P

20

Page 21: Métodos Númericos 2 Parcial

Matriz U

−5 2 −13 1 61 0 3

F2 = F2 + 3/5F1F3 = F3 + 1/5F1

−5 2 −10 11/5 27/50 2/5 14/5

F3 = F3− 2/11F2

−5 2 −10 11/5 27/50 0 20/11

= U

Matriz LObtenida mediante los coeficientes utilizados para hacer matriz triangular superior.

L =

1 0 0−3/5 1 0−1/5 2/11 1

PA = LU0 0 1

0 1 01 0 0

1 0 33 1 6−5 2 −1

=

1 0 0−3/5 1 0−1/5 2/11 1

−5 2 −10 11/5 27/50 0 20/11

−5 2 −1

3 1 61 0 3

=

−5 2 −13 1 61 0 3

Ejecucion Del Programa

>> [L U P] = fac_LUP([1 0 3;3 1 6 ;-5 2 -1],eye(3))

L =

1.0000 0 0

-0.6000 1.0000 0

-0.2000 0.1818 1.0000

U =

-5.0000 2.0000 -1.0000

0 2.2000 5.4000

0 0 1.8182

P =

0 0 1

0 1 0

1 0 0

21

Page 22: Métodos Númericos 2 Parcial

12. Descomponer las siguientes matrices en matrices L y U y compruebe que PA = LU4 2 12 5 −21 −2 7

Matriz U

4 2 12 5 −21 −2 7

f2 = f2−1

2f1

f3 = f3−1

4f1

4 2 1

0 4 −5

2

0 −5

2

27

4

f3 = f3 +5

8f2

4 2 1

0 4 −5

2

0 083

16

Matriz L

1 0 00 1 00 0 1

1 0 01

21 0

1

40 1

1 0 01

21 0

1

4−

5

81

Matriz P

1 0 00 1 00 0 1

Comprobacion PA=LU

1 0 00 1 00 0 1

·4 2 1

2 5 −21 −2 7

=

1 0 01

21 0

1

4−

5

81

4 2 1

0 4 −5

2

0 083

16

4 2 1

2 5 −21 −2 7

=

4 2 12 5 −21 −2 7

Codigo del programa

function [L U P] = ejr12fac_LUP(A,B)

%[L U P] = ejr12fac_LUP([4 2 1;2 5 -2;1 -2 7],eye(3))

[n,n]=size(A);

L=eye(n);

P=eye(n);

for k=1:n-1

[a,b]=max(abs(A(k:n,k)));

if a==0

error(’La matriz es singular’)

22

Page 23: Métodos Númericos 2 Parcial

return

end

m=k+b-1;%real ubicacion del elemento maximo y de la fila de cambio

A=cambiofilas(A,k,m);

U=A;

P=cambiofilas(P,k,m);

for k=1:n-1

for j=k+1:n

mult=U(j,k)/U(k,k);

U(j,k:n)=U(j,k:n)-mult*U(k,k:n);

L(j,k)=mult;

end

end

end

M=P*B;

Y=gauss(L,M);

X=gauss(U,Y’);

end

Ejecucion del programa

>> [L U P] = ejr12fac_LUP([4 2 1;2 5 -2;1 -2 7],eye(3))

L =

1.0000 0 0

0.5000 1.0000 0

0.2500 -0.6250 1.0000

U =

4.0000 2.0000 1.0000

0 4.0000 -2.5000

0 0 5.1875

P =

1 0 0

0 1 0

0 0 1

13.1 1 0 42 −1 5 05 2 1 2−3 0 2 6

1 1 0 42 −1 5 05 2 1 2−3 0 2 6

F3⇔ F1

5 2 1 22 −1 5 01 1 0 4−3 0 2 6

F4⇔ F3

5 2 1 22 −1 5 0−3 0 2 61 1 0 4

23

Page 24: Métodos Númericos 2 Parcial

Matriz De Permutacion.

1 0 0 00 1 0 00 0 1 00 0 0 1

F3⇔ F1

0 0 1 00 1 0 01 0 0 00 0 0 1

F4⇔ F3

0 0 1 00 1 0 00 0 0 11 0 0 0

= P

Matriz U

5 2 1 22 −1 5 0−3 0 2 61 1 0 4

F2 = F2− 2/5F1F3 = F3 + 3/5F1F4 = F4− 1/5F1

5 2 1 20 −9/5 23/5 −4/50 3/5 −1/5 18/50 6/5 13/5 36/5

F3 = F3 + 1/3F2F4 = F4 + 2/3F2

5 2 1 20 −9/5 23/5 −4/50 0 17/3 20/30 0 4/3 10/3

F4 = F4− 4/17F3

5 2 1 20 −9/5 23/5 −4/50 0 17/3 20/30 0 0 30/7

= U

Matriz LObtenida mediante los coeficientes utilizados para hacer matriz triangular superior.

L =

1 0 0 0

2/5 1 0 0−3/5 −1/3 1 0−1/5 −2/3 4/17 1

PA = LU

0 0 1 00 1 0 00 0 0 11 0 0 0

1 1 0 42 −1 5 05 2 1 2−3 0 2 6

=

1 0 0 0

2/5 1 0 0−3/5 −1/3 1 0−1/5 −2/3 4/17 1

5 2 1 20 −9/5 23/5 −4/50 0 17/3 20/30 0 0 30/7

5 2 1 22 −1 5 0−3 0 2 61 1 0 4

=

5 2 1 22 −1 5 0−3 0 2 61 1 0 4

Ejecucion Del Programa

>> [L U P] = fac_LUP([1 1 0 4;2 -1 5 0;5 2 1 2;-3 0 2 6 ],eye(4))

L =

1.0000 0 0 0

0.4000 1.0000 0 0

-0.6000 -0.6667 1.0000 0

24

Page 25: Métodos Númericos 2 Parcial

0.2000 -0.3333 0.2353 1.0000

U =

5.0000 2.0000 1.0000 2.0000

0 -1.8000 4.6000 -0.8000

0 0 5.6667 6.6667

0 0 0 1.7647

P =

0 0 1 0

0 1 0 0

0 0 0 1

1 0 0 0

14. Resuelva el siguiente sistema de ecuaciones utilizando el metodo de la factorizaciontriangular LU.

x1 + 3x2 + 5x3 + 7x4 = 1

2x1 − x2 + 3x3 + 5x4 = 2

2x3 + 5x4 = 3

−2x1 − 6x2 − 3x3 + x4 = 4

Matriz U

1 3 5 72 −1 3 50 0 2 5−2 −6 −3 1

f1↔ f2f3↔ f4

2 −1 3 51 3 5 7−2 −6 −3 10 0 2 5

f2↔ f3

2 −1 3 5−2 −6 −3 11 3 5 70 0 2 5

f2 = f2 + f1

f3 = f3−1

2f1

2 −1 3 50 −7 0 6

07

2

7

2

9

20 0 2 5

f3 = f3 +1

2f3

2 −1 3 50 −7 0 6

0 07

2

15

20 0 2 5

f4 = f4−4

7f3

2 −1 3 50 −7 0 6

0 07

2

15

2

0 0 05

7

Matriz L

25

Page 26: Métodos Númericos 2 Parcial

1 0 0 0−1 1 0 01

2−

1

21 0

0 04

71

Matriz P

1 0 0 00 1 0 00 0 1 00 0 0 1

f1↔ f2f3↔ f4

0 1 0 01 0 0 00 0 0 10 0 1 0

f2↔ f3

0 1 0 00 0 0 11 0 0 00 0 1 0

L(Y)=Pb

y1 0 0 0−y1 y2 0 01

2y1 −

1

2y2 y3 0

0 04

7y3 y4

=

0 1 0 00 0 0 11 0 0 00 0 1 0

·

1234

y1 0 0 0−y1 y2 0 01

2y1 −

1

2y2 y3 0

0 04

7y3 y4

=

2413

1)

y1 = 2

2)−y1 + y2 = 4−(2) + y2 = 4

y2 = 6

3)1

2y1 −

1

2y2 + y3 = 1

1

2(2)−

1

2(6) + y3 = 1

y3 = 3

4)4

7y3 + y4 = 3

4

7(3) + y4 = 3

y4 =9

7

26

Page 27: Métodos Númericos 2 Parcial

Y =

263

9/7

Ux=Y

2x1 −x2 3x3 5x4

0 −7x2 0 6x4

0 07

2x3

15

2x4

0 0 05

7x4

=

263

9/7

1)5

7x4 =

9

7

x4 = 1,8

2)7

2x3 +

15

2x4 = 3

7

2x3 +

15

2(1,8) = 3

x3 = −3

3)−7x2 + 6x4 = 6−7x2 + 6(1,8) = 6

x2 =24

35= 0,6857

4)2x1 − x2 + 3x3 + 5x4 = 2

2x1 −

(24

35

)+ 3(−3) + 5(1,8) = 2

x1 =47

35

Codigo del programa

function [L U P] = ejr14fac_LUP(A,B)

%[L U P] = ejr14fac_LUP([1 3 5 7;2 -1 3 5;0 0 2 5;-2 -6 -3 1],[1 2 3 4]’)

[n,n]=size(A);

L=eye(n);

P=eye(n);

for k=1:n-1

27

Page 28: Métodos Númericos 2 Parcial

[a,b]=max(abs(A(k:n,k)));

if a==0

error(’La matriz es singular’)

return

end

m=k+b-1;%real ubicacion del elemento maximo y de la fila de cambio

A=cambiofilas(A,k,m);

U=A;

P=cambiofilas(P,k,m);

for k=1:n-1

for j=k+1:n

mult=U(j,k)/U(k,k);

U(j,k:n)=U(j,k:n)-mult*U(k,k:n);

L(j,k)=mult;

end

end

end

M=P*B;

Y=gauss(L,M);

X=gauss(U,Y’);

h=1;

fprintf(’El resultado del sistema de ecuaciones es:\n’)

for g=1:1:n

fprintf(’x%d=%f\n’,g,X(h,g))

end

end

Ejecucion del programa

>> [L U P] = ejr14fac_LUP([1 3 5 7;2 -1 3 5;0 0 2 5;-2 -6 -3 1],[1 2 3 4]’)

El resultado del sistema de ecuaciones es:

x1=1.342857

x2=0.685714

x3=-3.000000

x4=1.800000

L =

1.0000 0 0 0

-1.0000 1.0000 0 0

0.5000 -0.5000 1.0000 0

0 0 0.5714 1.0000

U =

2.0000 -1.0000 3.0000 5.0000

0 -7.0000 0 6.0000

0 0 3.5000 7.5000

0 0 0 0.7143

28

Page 29: Métodos Númericos 2 Parcial

P =

0 1 0 0

0 0 0 1

1 0 0 0

0 0 1 0

15.-Considere el sistema Ax = b, donde A y b estan dados por:

A =

2 −2 0ε− 2 2 0

0 −1 3

y b =

0ε2

Calcule la descomposicion LU de la matriz A y verifique que la entrada L32 →∞si ε →

0..Luego, resuelva el sistema de ecuaciones con esta descomposicion. Finalmente, compruebe que,pese a que L32 →∞ la solucion calculada es exacta.

Matriz De Permutacion.

1 0 00 1 00 0 1

= P

Matriz U

2 −2 0ε− 2 2 0

0 −1 3

F2 = F2− (ε− 2)/2F1

2 −2 00 ε 00 −1 3

F3 = F3 + 1/3F2

2 −2 00 ε 00 0 3

= U

Matriz LObtenida mediante los coeficientes utilizados para hacer matriz triangular superior.

1 0 0(ε− 2)/2 1 0

0 −1/ε 1

L32 →∞si ε → 0.

L(Y)=b

1y1 0 0(ε− 2)/2y1 1y2 0

0 −1/εy2 1y3

=

0ε2

29

Page 30: Métodos Númericos 2 Parcial

y1 = 0

y2 = ε

y3 = 3

Y =

0ε3

UX=Y

2x1 −2x2 00 εx2 00 0 3x3

=

0ε3

x3 = 1

x2 = 1

2x1 − 2(1) = 0

x1 = 1

x1 = 1

x2 = 1

x3 = 1

X =

111

30