resolucion laboratorio de matlab !

39
UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS FACULTAD DE INGENIERÍA ELECTRÓNICA Y ELÉCTRICA INFORME: SOLUCIONARIO DE EJERCICIOS PROPUESTOS DE LA GUIA DEL LABORATORIO DE METODOS NUMERICOS ALUMNO CÓDIGO

Upload: jonathan-lucas

Post on 27-Oct-2014

45 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Resolucion Laboratorio de Matlab !

UNIVERSIDAD NACIONAL MAYOR DE SAN MARCOS

FACULTAD DE INGENIERÍA ELECTRÓNICA Y ELÉCTRICA

INFORME: SOLUCIONARIO DE EJERCICIOS PROPUESTOS DE LA GUIA DEL LABORATORIO DE METODOS NUMERICOS

ALUMNO CÓDIGO

Lucas Huaman Jonathan 08190017

Page 2: Resolucion Laboratorio de Matlab !

CAPITULO 2: MANEJO DE ARREGLOS

MATRICES

1. Hallar las dimensiones, la traza, el determinante y la inversa de la matriz A

>> A=[2 3 -7; 2 1 -1; 1 2 3]

A =

2 3 -7

2 1 -1

1 2 3

dimensiones

>> size(A)

ans =

3 3

Traza

>> A(1,1)+A(2,2)+A(3,3)

ans =

6

Determinante

>> det(A)

ans =

-32

Page 3: Resolucion Laboratorio de Matlab !

La inversa

>> B=1\A

B =

2 3 -7

2 1 -1

1 2 3

2. Crear una matriz de dos columnas con la diagonal y antidiagonal de la matriz A

>> A=[2 3 -7; 2 1 -1; 1 2 3]

A =

2 3 -7 2 1 -1 1 2 3

>> C=[diag(A) diag(fliplr(A))]

C =

2 -7

1 1

3 1

Page 4: Resolucion Laboratorio de Matlab !

3. Crear una matriz M de 3 columnas:Primera columna con la diagonal de ASegunda columna con la diagonal inferior de BTercera columna con la diagonal superior de C

>> A=[1:11; 2:12; 3:13; 4:14; 5:15; 6:16; 7:17; 8:18; 9:19; 10:20; 11:21]

A =

1 2 3 4 5 6 7 8 9 10 11

2 3 4 5 6 7 8 9 10 11 12

3 4 5 6 7 8 9 10 11 12 13

4 5 6 7 8 9 10 11 12 13 14

5 6 7 8 9 10 11 12 13 14 15

6 7 8 9 10 11 12 13 14 15 16

7 8 9 10 11 12 13 14 15 16 17

8 9 10 11 12 13 14 15 16 17 18

9 10 11 12 13 14 15 16 17 18 19

10 11 12 13 14 15 16 17 18 19 20

11 12 13 14 15 16 17 18 19 20 21

Page 5: Resolucion Laboratorio de Matlab !

>> B=magic(12)

144 2 3 141 140 6 7 137 136 10 11 133

13 131 130 16 17 127 126 20 21 123 122 24

25 119 118 28 29 115 114 32 33 111 110 36

108 38 39 105 104 42 43 101 100 46 47 97

96 50 51 93 92 54 55 89 88 58 59 85

61 83 82 64 65 79 78 68 69 75 74 72

73 71 70 76 77 67 66 80 81 63 62 84

60 86 87 57 56 90 91 53 52 94 95 49

48 98 99 45 44 102 103 41 40 106 107 37

109 35 34 112 113 31 30 116 117 27 26 120

121 23 22 124 125 19 18 128 129 15 14 132

12 134 135 9 8 138 139 5 4 142 143 1

>> C=pascal(12)

1 1 1 1 1 1 1 1 1 1 1 1

1 2 3 4 5 6 7 8 9 10 11 12

1 3 6 10 15 21 28 36 45 55 66 78

1 4 10 20 35 56 84 120 165 220 286 364

1 5 15 35 70 126 210 330 495 715 1001 1365

1 6 21 56 126 252 462 792 1287 2002 3003 4368

Page 6: Resolucion Laboratorio de Matlab !

1 7 28 84 210 462 924 1716 3003 5005 8008 12376

1 8 36 120 330 792 1716 3432 6435 11440 19448 31824

1 9 45 165 495 1287 3003 6435 12870 24310 43758 75582

1 10 55 220 715 2002 5005 11440 24310 48620 92378 167960

1 11 66 286 1001 3003 8008 19448 43758 92378 184756 352716

1 12 78 364 1365 4368 12376 31824 75582 167960 352716 705432

>> M=[diag(A) diag(B,-1) diag(C,1)]

M =

1 13 1

3 119 3

5 39 10

7 93 35

9 65 126

11 67 462

13 91 1716

15 41 6435

17 117 24310

19 15 92378

21 143 352716

Page 7: Resolucion Laboratorio de Matlab !

4. Generar la matriz con la orden diag.

>> A=[diag(ones(5,1),2)]+[-4*diag(ones(6,1),1)]+diag([5 6 6 6 6 6 5])+

+[-4*diag(ones(6,1),-1)]+[diag(ones(5,1),-2)]

A =

5 -4 1 0 0 0 0

-4 6 -4 1 0 0 0

1 -4 6 -4 1 0 0

0 1 -4 6 -4 1 0

0 0 1 -4 6 -4 1

0 0 0 1 -4 6 -4

0 0 0 0 1 -4 5

5. Generar la matriz con las ordenes diag. Y fliplr.

>> A=fliplr([3*diag(ones(5,1),2)]+[-4*diag(ones(6,1),1)]+[2*eye(7)]+[-4*diag(ones(6,1),-1)])

A =

0 0 0 0 3 -4 2

0 0 0 3 -4 2 -4

0 0 3 -4 2 -4 0

0 3 -4 2 -4 0 0

3 -4 2 -4 0 0 0

-4 2 -4 0 0 0 0

2 -4 0 0 0 0 0

Page 8: Resolucion Laboratorio de Matlab !

CAPITULO 3: ALGEBRA MATRICIAL

1. Resolver el sistema Lineal :

2X1 +3X2 -4X3 = 3

X1 -2X2 +1X3 = 0

X1 -7X2 +14X3 = 2

>> A=[2,3,-4;1,-2,1;1,-7,14] A =

2 3 -4 1 -2 1 1 -7 14

>> b=[3,0,2]' b =

3 0 2

>> X=A\b X =

1.1967 0.8361 0.4754

2. Resolver el Sistema Lineal:

X1 +X2 +0X3+3X4 = 4

2X1 +X2 -X3+X4 = 1

3X1 -X2 -X3+2X4 = -3

-X1 +2X2 +3X3-X4 = 4

>> A=[1,1,0,3;2,1,-1,3,-1,-1,2;-1,2,3,-1] A =

1 1 0 3 2 1 -1 1

Page 9: Resolucion Laboratorio de Matlab !

3 -1 -1 2 -1 2 3 -1

>> b=[4,1,-3,4]' b =

4 1 -3 4

>> X=A\b X =

-1.0000 2.0000 -0.0000 1.0000

3. Resolver los sistemas A x = b y B x = b utilizando:

A=(1 22 4

0 11 1

−1 −51 5

0 02 1

)B=(−1 32 −6

2 1/2−1 0

0 65 −3

2 10 2

)Siendo b ==> b1=(

1010)b2=(

1234)b3=(

−102

−3)

>> A=[1 2 0 1;2 4 1 1;-1 -5 0 0;1 5 2 1] A =

1 2 0 1 2 4 1 1 -1 -5 0 0 1 5 2 1

>> b1=[1 0 1 0]' b1 =

1 0 1 0

>>x1=A\b1 x1 =

-0.4444

Page 10: Resolucion Laboratorio de Matlab !

-0.1111 -0.3333 1.6667

>> b2=[1 2 3 4]' b2 =

1 2 3 4

>> x2=A\b2 x2 =

-0.2222 -0.5556 2.3333 2.3333

>> b3=[-1 0 2 -3]' b3 =

-1 0 2 -3

>> x3=A\b3 x3 =

2.4444 -0.8889 0.3333 -1.6667

>> B=[-1 3 2 0.5;2 -6 -1 0;0 6 2 1;5 -3 0 2] B =

-1 3 2 0.5 2 -6 -1 0 0 6 2 1 5 -3 0 2

>> b1=[1 0 1 0]' b1 =

1 0 1 0

>>x4=B\b1 x4 =

0.8333 0.0556 1.3333

Page 11: Resolucion Laboratorio de Matlab !

-2.0000>> b2=[1 2 3 4]' b2 =

1 2 3 4

>> x5=B\b2 x5 =

5.1667 0.6111 4.6667 -10.0000

>> b3=[-1 0 2 -3]' b3 =

-1 0 2 -3

>> x6=B\b3 x6 =

6.6667 1.4444 4.6667 -16.0000

4. Factorice A=[1 11 3

1 1 15 3 7

1 31 40 2

6 1 11 2 33 4 5

] por Gauss Doolitle y también ortogonalmente.

>> A=[1 1 1 1 1;1 3 5 3 7;1 3 6 1 1;1 4 1 2 3;0 2 3 4 5] A =

1 1 1 1 1 1 3 5 3 7 1 3 6 1 1 1 4 1 2 3 0 2 3 4 5

>> [L,U,P]=lu(A) L =

1.0000 0 0 0 0

Page 12: Resolucion Laboratorio de Matlab !

1.0000 1.0000 0 0 0 1.0000 0.6667 1.0000 0 0 0 0.6667 0.6000 1.0000 0 1.0000 0.6667 0.8000 0.5000 1.0000

U =

1.0000 1.0000 1.0000 1.0000 1.0000 0 3.0000 0 1.0000 2.0000 0 0 5.0000 -0.6667 1.3333 0 0 0 3.7333 4.4667 0 0 0 0 3.5000

P =

1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0

>> [Q,U]=qr(A) Q =

0.5000 -0.5916 -0.1280 0.4907 -0.3780 0.5000 0.0845 0.3231 0.2590 0.7559 0.5000 0.0845 0.5364 -0.5588 -0.3780 0.5000 0.4226 -0.7314 -0.1908 0.0000 0 0.6761 0.2377 0.5861 -0.3780

U =

2.0000 5.5000 6.5000 3.5000 6.0000 0 2.9580 2.7890 3.2961 4.7329 0 0 4.6874 0.8655 1.6640 0 0 0 2.6714 4.1026 0 0 0 0 2.6458

5. Haga operaciones elementales filas a la matriz A con tal de convertir toda la 1ra columna y debajo del primer elemento de A en ceros.

>> A=[1 1 1 1 1;1 3 5 3 7;1 3 6 1 1;1 4 1 2 3;0 2 3 4 5] A =

1 1 1 1 1 1 3 5 3 7 1 3 6 1 1

Page 13: Resolucion Laboratorio de Matlab !

1 4 1 2 3 0 2 3 4 5

>> A(2,:)=A(2,:)-A(1,:) A =

1 1 1 1 1 0 2 4 2 6 1 3 6 1 1 1 4 1 2 3 0 2 3 4 5

>> A(3,:)=A(3,:)-A(1,:) A =

1 1 1 1 1 0 2 4 2 6 0 2 5 0 0 1 4 1 2 3 0 2 3 4 5

>> A(4,:)=A(4,:)-A(1,:) A =

1 1 1 1 1 0 2 4 2 6 0 2 5 0 0 0 3 0 1 2 0 2 3 4 5

Page 14: Resolucion Laboratorio de Matlab !

CAPITULO 4: POLINOMIOS

6. Evaluar los polinomios en x = 1:0.3:7

i. P2=2x5 + 3ix +( 6-2i)

ii. P3=x10+ x+ 1

En Matlab:

i)

>> p2=[2,0,0,0,3i,6-2i];>> x=1:0.3:7;>>polyval(p2,x)

ans =

1.0e+004 *

Columns 1 through 2

0.0008 + 0.0001i 0.0013 + 0.0002i

Columns 3 through 4

0.0027 + 0.0003i 0.0056 + 0.0004i

Columns 5 through 6

0.0109 + 0.0005i 0.0201 + 0.0006i

Columns 7 through 8

0.0350 + 0.0006i 0.0579 + 0.0007i

Page 15: Resolucion Laboratorio de Matlab !

Columns 9 through 10

0.0915 + 0.0008i 0.1393 + 0.0009i

Columns 11 through 12

0.2054 + 0.0010i 0.2946 + 0.0011i

Columns 13 through 14

0.4125 + 0.0012i 0.5656 + 0.0013i

Columns 15 through 16

0.7610 + 0.0014i 1.0072 + 0.0014i

Columns 17 through 18

1.3133 + 0.0015i 1.6898 + 0.0016i

Columns 19 through 20

2.1481 + 0.0017i 2.7009 + 0.0018i

Column 21

3.3620 + 0.0019i

ii)>> p3=[1,0,0,0,0,0,0,0,0,1,1];>> x=1:0.3:7;>>polyval(p3,x)

ans =

1.0e+008 *

Columns 1 through 4

0.0000 0.0000 0.0000 0.0000

Columns 5 through 8

Page 16: Resolucion Laboratorio de Matlab !

0.0000 0.0001 0.0003 0.0008

Columns 9 through 12

0.0021 0.0048 0.0105 0.0216

Columns 13 through 16

0.0424 0.0798 0.1446 0.2533

Columns 17 through 20

0.4308 0.7133 1.1529 1.8228

Column 21

2.8248

7. Hallar el desdarrollo del trinomio p(x) =( x3 - ix+2)4 usando los comandos de Matlab

En Matlab:

>> p=[1 0 -i 2];

>> q=conv(p,p)

q =

Columns 1 through 4

1.0000 0 0 - 2.0000i 4.0000

Columns 5 through 7

-1.0000 0 - 4.0000i 4.0000

>> r=conv(q,p)

r =

Columns 1 through 4

Page 17: Resolucion Laboratorio de Matlab !

1.0000 0 0 - 3.0000i 6.0000

Columns 5 through 8

-3.0000 0 -12.0000i 12.0000 + 1.0000i -6.0000

Columns 9 through 10

0 -12.0000i 8.0000

>>conv(r,p)

ans =

Columns 1 through 4

1.0000 0 0 - 4.0000i 8.0000

Columns 5 through 8

-6.0000 0 -24.0000i 24.0000 + 4.0000i -24.0000

Columns 9 through 12

1.0000 -48.0000i 32.0000 + 8.0000i -24.0000 0 -32.0000i

Column 13

16.0000

9. Hallar el resto de la división de p(x) por d(x)= x3 - ix+2, siendo p(x) el polinomioCaracterístico de la matriz A.

1 1 1 1 11 3 5 3 7

A= 1 3 6 1 11 4 1 2 30 2 3 4 5

Page 18: Resolucion Laboratorio de Matlab !

En Matlab:

>> A=[1,1,1,1,1;1,3,5,3,7;1,3,6,1,1;1,4,1,2,3;0,2,3,4,5]

A =

1 1 1 1 1 1 3 5 3 7 1 3 6 1 1 1 4 1 2 3 0 2 3 4 5

>>poly(A)

ans =

Columns 1 through 4

1.0000 -17.0000 47.0000 -4.0000

Columns 5 through 6

203.0000 -196.0000

>> P=poly(A);>> [Q,R]=deconv(P,[1,0,-i,2])

Q =

Columns 1 through 2

1.0000 -17.0000

Column 3

47.0000 + 1.0000i

R =

1.0e+002 *

Page 19: Resolucion Laboratorio de Matlab !

Columns 1 through 2

0 0

Columns 3 through 4

0 -0.0600 - 0.1700i

Columns 5 through 6

2.3600 + 0.4700i -2.9000 - 0.0200i

10. Hallar la derivada del cociente de la división de P(x)=x10+ x+ 1 por D(x)= x3-i x+ 2

En Matlab:

>> p=[1,0,0,0,0,0,0,0,0,1,1];>> d=[1,0,-i,2];>> [Q,R]=deconv(p,d)

Q =

Columns 1 through 2

1.0000 0

Columns 3 through 4

0 + 1.0000i -2.0000

Columns 5 through 6

-1.0000 0 - 4.0000i

Columns 7 through 8

4.0000 - 1.0000i 6.0000

R =

Columns 1 through 2

Page 20: Resolucion Laboratorio de Matlab !

0 0

Columns 3 through 4

0 0

Columns 5 through 6

0 0

Columns 7 through 8

0 0

Columns 9 through 10

1.0000 +12.0000i -7.0000 + 8.0000i

Column 11

-11.0000 >> D=polyder(Q)

D =

Columns 1 through 2

7.0000 0

Columns 3 through 4

0 + 5.0000i -8.0000

Columns 5 through 6

-3.0000 0 - 8.0000i

Column 7

4.0000 - 1.0000i

Page 21: Resolucion Laboratorio de Matlab !

11. Hallar las raíces de la función f(x)= xsenx+2 cercanas a – 6, – 4, 4 y 6.

En Matlab:

>>fzero('x.*sin(x)+2',-6)

ans =

-5.9398

>>fzero('x.*sin(x)+2',-4)

ans =

-3.7108

>>fzero('x.*sin(x)+2',6)

ans =

5.9398

>>fzero('x.*sin(x)+2',4)

ans =

3.7108

Page 22: Resolucion Laboratorio de Matlab !

CAPITULO 5: GRÁFICOS BIDIMENSIONALES (2-D)

1.- Graficar usando este archivo la 1ra columna versus la 2da columna a través de una poligonal linea

Graficamos:

Page 23: Resolucion Laboratorio de Matlab !

2. Grafique: a) x2− y2=3b) ⟦x ⟧+|y|=10

Para

a) x2− y2=3

graficando:

Page 24: Resolucion Laboratorio de Matlab !

b¿ ⟦x ⟧+|y|=10

Page 25: Resolucion Laboratorio de Matlab !

4. Grafique las funciones polares:

a) R1=2sen (3θ), θ=0 :πI 20 :2π

B) R2= 5θ ,θ=0: π I 20 :2 π

C) R3=2-SENθ ,θ=0: π I 20 :2 π

%GRAFIQUE LAS FUNCIONES POLARES

%Atheta=0:pi/20:2*pi;r=2*sin(3*theta);polar(theta,r);

0.5

1

1.5

2

30

210

60

240

90

270

120

300

150

330

180 0

Page 26: Resolucion Laboratorio de Matlab !

%Btheta=0:pi/20:2*pi;r=5*theta;polar(theta,r);

%Ctheta=0:pi/20:2*pi;r=2-sin(theta);polar(theta,r);

10

20

30

40

30

210

60

240

90

270

120

300

150

330

180 0

1

2

3

30

210

60

240

90

270

120

300

150

330

180 0

Page 27: Resolucion Laboratorio de Matlab !

%At=0:0.1:2*pix1=sin(t)y1=sin(2*t)%PRIMERA FIGURAsubplot(1,2,1)plot(t,x1)%SEGUNDA FIGURAsubplot(1,2,2)plot(t,y1)

0 2 4 6 8-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

0 2 4 6 8-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 28: Resolucion Laboratorio de Matlab !

%B

t=0:0.1:2*pix1=sin(t)y1=cos(t)%PRIMERA FIGURAsubplot(1,2,1)plot(t,x1)%SEGUNDA FIGURAsubplot(1,2,2)plot(t,y1)

0 2 4 6 8-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

0 2 4 6 8-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 29: Resolucion Laboratorio de Matlab !

CAPITULO 7: PROGRAMACIÓN EN MATLAB

1. Sea A una matriz de mxn y B una matriz de pxq. Implemente la función bloque(A,B) la cual genere la matriz bloque diagonal (m+p)x(n+q).

Solución:

function bloq=bloque(A,B);

%bloque(A,B) halla la matriz bloque diagonal (m+p)x(n+q)

%donde la matriz A es del orden (mxn) y B del orden (pxq)

bloq=blkdiag(A,B);

end

En la ventana de comandos:

>> A=[4 11 2;6 9 1;2 3 7]

A =

4 11 2 6 9 1 2 3 7

>> B=[5 9;2 5]

B =

5 9 2 5

>> bloque(A,B)

ans =

Page 30: Resolucion Laboratorio de Matlab !

4 11 2 0 0 6 9 1 0 0 2 3 7 0 0 0 0 0 5 9 0 0 0 2 5

2. Crear la función (programa) r = mmedad(n) que al ingresar la edad de la persona „n‟, determine si es 1=„mayor de edad‟ o 0=„menor de edad‟ sin usar el comando if. Por ejemplo cuando ejecute en la ventana de comandos:

>>r = mmedad(17)

debe dar:

r = 0

Solución:

function r = mmedad(n);

%mmedad determina si la persona es mayor de edad

r=all(n>=18);

end

En la ventana de comandos:

>> r = mmedad(15)

;

r =

0

Page 31: Resolucion Laboratorio de Matlab !

>> r = mmedad(22)

r =

1

3. Escribir una función (programa) para calcular el mayor y menor lado de un triángulo sin usar la sentencia if.

Solución:

function [ladomayor,ladomenor]= trian(a,b,c) ladomayor=sort([a,b,c]);ladomayor=ladomayor(1,3); ladomenor=sort([a,b,c]);ladomenor=ladomenor(1,1); end

En la ventana de comandos:

>> [ladomayor,ladomenor]= trian(1,8,4)

ladomayor =

8

ladomenor =

1

Page 32: Resolucion Laboratorio de Matlab !

4. Implemente la función polysum(p,q) la cual sume los dos polinomios .

Solución:

function suma = polysum(p,q) %polysum halla la suma de los polinomios p y q suma=p+q; end

En la ventana de comandos:

>> p=[1 5 6]

p =

1 5 6

>> q=[8 5 4]

q =

8 5 4

>> suma=polysum(p,q)

suma =

9 10 10

Page 33: Resolucion Laboratorio de Matlab !