Anda di halaman 1dari 37

SOLUCIONARIO DE EJERCICIOS

PROPUESTOS

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

-7

-1

dimensiones
>> size(A)
ans =
3

Traza
>> A(1,1)+A(2,2)+A(3,3)
ans =
6
Determinante
>> det(A)
ans =
-32

Mtodos Numricos

Pgina 2

La inversa
>> B=1\A
B=
2

-7

-1

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
2
1

3
1
2

-7
-1
3

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

C=

-7

Mtodos Numricos

Pgina 3

3. Crear una matriz M de 3 columnas:


Primera columna con la diagonal de A
Segunda columna con la diagonal inferior de B
Tercera 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

10

11

10

11

12

10

11

12

13

10

11

12

13

14

10

11

12

13

14

15

10

11

12

13

14

15

16

10

11

12

13

14

15

16

17

10

11

12

13

14

15

16

17

18

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

Mtodos Numricos

Pgina 4

>> B=magic(12)
144

3 141 140

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

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

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

42

43 101 100

41

46

47

97

40 106 107

8 138 139

4 142 143

37

>> C=pascal(12)
1

10

10

15

21

28

36

45

55

10

20

35

56

84

120

165

220

5
1365

15

35

70

126

210

330

495

715

6
4368

21

56

126

252

462

792

1287

2002

7
12376

28

84

210

462

924

1716

3003

5005

11

12
1

66

78
1

286

364

1001

3003

8008

Mtodos Numricos

Pgina 5

8
31824

36

120

330

792

1716

3432

6435

11440

9
75582

45

165

495

1287

3003

6435 12870

24310

55

220

715

2002

5005

11440

24310 48620

92378

10
167960

1
184756

11
352716

66

286

1001

3003

8008

19448

43758

1
352716

12
705432

78

364

1365

4368

12376 31824 75582 167960

19448

43758

92378

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


M=
1

13

3 119

39

10

93

35

65

126

11

67

462

13

91

1716

15

41

6435

17

117 24310

19

15

92378

21

143

352716

Mtodos Numricos

Pgina 6

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=

-4

-4

-4

-4

-4

-4

-4

-4

-4

-4

-4

-4

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

-4

-4

-4

-4

-4

-4

-4

-4

-4

-4

-4

-4

Mtodos Numricos

Pgina 7

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=

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

2 3 -4
1 -2 1
1 -7 14
b=

>> X=A\b

3
0
2
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,1;3,-1,-1,2;-1,2,3,-1]

A=

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

1 1 0 3
2 1 -1 1
3 -1 -1 2
-1 2 3 -1
b=

Mtodos Numricos

Pgina 8

4
1
-3
4
X=

>> X=A\b

-1.0000
2.0000
-0.0000
1.0000

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

Siendo b ==>

( )

( )

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

A=

>> b1=[1 0 1 0]'

1
2
-1
1
b1 =

>>x1=A\b1

1
0
1
0
x1 =

>> b2=[1 2 3 4]'

-0.4444
-0.1111
-0.3333
1.6667
b2 =

2
4
-5
5

0
1
0
2

1
1
0
1

1
2
3
Mtodos Numricos

Pgina 9

>> x2=A\b2

4
x2 =

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

-0.2222
-0.5556
2.3333
2.3333
b3 =

>> x3=A\b3

-1
0
2
-3
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=

>> b1=[1 0 1 0]'

-1
2
0
5
b1 =

>>x4=B\b1

1
0
1
0
x4 =

>> b2=[1 2 3 4]'

0.8333
0.0556
1.3333
-2.0000
b2 =

>> x5=B\b2

1
2
3
4
x5 =

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

5.1667
0.6111
Mtodos Numricos

Pgina 10

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

4.6667
-10.0000
b3 =

>> x6=B\b3

-1
0
2
-3
x6 =
6.6667
1.4444
4.6667
-16.0000

4. Factorice A=
[

por Gauss Doolitle y tambin 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]

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

A=
1
1
1
1
0
L=

1
3
3
4
2

1
5
6
1
3

1
3
1
2
4

1
7
1
3
5

1.0000
0
0
0
0
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
Mtodos Numricos

Pgina 11

0
3.5000

P=

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

1
0
0
0
0
Q=

0
0
0
0
1

0.5000
0.3780
0.5000
0.5000
0.5000
0
0.3780

0
0
1
0
0

0
1
0
0
0

0
0
0
1
0

-0.5916 -0.1280 0.4907 0.0845


0.0845
0.4226
0.6761

0.3231
0.5364
-0.7314
0.2377

0.2590
-0.5588
-0.1908
0.5861

0.7559
-0.3780
0.0000
-

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(2,:)=A(2,:)-A(1,:)

Mtodos Numricos

A=
1
1
1
1
0
A=

1
3
3
4
2

1
5
6
1
3

1
3
1
2
4

1
7
1
3
5

1
0
1
1
0

1
2
3
4
2

1
4
6
1
3

1
2
1
2
4

1
6
1
3
5
Pgina 12

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

A=
1
2
2
4
2

1
4
5
1
3

1
2
0
2
4

1
6
0
3
5

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

1
0
0
1
0
A=
1
0
0
0
0

1
2
2
3
2

1
4
5
0
3

1
2
0
1
4

1
6
0
2
5

Mtodos Numricos

Pgina 13

CAPITULO 4: POLINOMIOS
1. Evaluar los polinomios en x= 1:0.3:7
(
)
1.1 P1=
1.2 P2=
>> % definimos los polinomios P1yP2
>> P1=[2 0 0 0 3i 6-2i]
P1 =
Columns 1 through 5
2.0000

0 + 3.0000i

Column 6
6.0000 - 2.0000i
>> P2=[1 0 0 0 0 0 0 0 0 1 1]
P2 =
1

>> %ahora evaluamos x en cada polinomio


>> x=1:0.3:7
x=
Columns 1 through 11
1.0000 1.3000
3.7000 4.0000

1.6000

1.9000

2.2000

2.5000

2.8000

3.1000

3.4000

4.9000

5.2000

5.5000

5.8000

6.1000

6.4000

6.7000

C
olumns 12 through 21
4.3000
7.0000

4.6000

>> Y=polyval(P1,x)
Y=

Mtodos Numricos

Pgina 14

1.0e+004 *
Columns 1 through 5
0.0008 + 0.0001i
0.0005i

0.0013 + 0.0002i

0.0027 + 0.0003i

0.0056 + 0.0004i

0.0109 +

Columns 6 through 10
0.0201 + 0.0006i 0.0350 + 0.0006i 0.0579 + 0.0007i 0.0915 + 0.0008i 0.1393 +
0.0009i
Columns 11 through 15
0.2054 + 0.0010i 0.2946 + 0.0011i 0.4125 + 0.0012i 0.5656 + 0.0013i 0.7610 +
0.0014i
Columns 16 through 20
1.0072 + 0.0014i 1.3133 + 0.0015i 1.6898 + 0.0016i 2.1481 + 0.0017i 2.7009 +
0.0018i
Column 21
3.3620 + 0.0019i
>> Y1=polyval(P2,x)
Y1 =
1.0e+008 *
Columns 1 through 10
0.0000
0.0048

0.0000

0.0000

0.0000

0.0000

0.0001

0.0003

0.0008

0.0021

0.0798

0.1446

0.2533

0.4308

0.7133

1.1529

Columns 11 through 20
0.0105
1.8228

0.0216

Mtodos Numricos

0.0424

Pgina 15

Column 21
2.8248
2. Hallar el desarrollo del trinomio
p(x)=(
)

>> %definimos el polinomio Q (x) como el suiguiente


>> Q=[1 0 -i 2 ]
Q=
1.0000

0 - 1.0000i 2.0000

>> %definimos P1(x) como QxQ


>> P1=conv(Q,Q)
P1 =
Columns 1 through 5
1.0000

0 - 2.0000i 4.0000

-1.0000

Columns 6 through 7
0 - 4.0000i 4.0000
>> %entonces P(x) sera igual a P1xP1
>> P=conv(P1,P1)
P=
Columns 1 through 5
1.0000

0 - 4.0000i 8.0000

-6.0000

Columns 6 through 10
0 -24.0000i 24.0000 + 4.0000i -24.0000

1.0000 -48.0000i 32.0000 + 8.0000i

Columns 11 through 13
-24.0000

Mtodos Numricos

0 -32.0000i 16.0000

Pgina 16

3. Calcular las races del polinomio:


P(x)=(
)
(
)
>> %ingresamos el polinomio
>> P=[0.9971-0.2283i ,1 ,-9i, -26.72-0.96i ,27i]
P=
0.9971 - 0.2283i 1.0000

0 - 9.0000i -26.7200 - 0.9600i

0 +27.0000i

>> %calculo de la raiz del polinomio


>> roots(P)
ans =
-2.3829 - 3.5015i
2.3683 + 0.6654i
-1.2886 + 1.2522i
0.3503 + 1.3658i

4. Hallar el resto de la divisin de


( )
p(x)=
>> %hallamos el polinomo caracterstico de la matriz A
>> 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

>> p1=poly(A)
p1 =
Mtodos Numricos

Pgina 17

1.0000 -17.0000 47.0000 -4.0000 203.0000 -196.0000


>> %ingresamos el polinomio d(x)
>> d=[1 0 -i 2]
d=
1.0000

0 - 1.0000i 2.0000

>> %hallamos el resto R(x) y cociente Q(x)de dividir p1(x) con d(x)
>> [Q,R]=deconv(p1,d)
Q=
1.0000

-17.0000

47.0000 + 1.0000i

R=
1.0e+002 *
Columns 1 through 5
0

-0.0600 - 0.1700i 2.3600 + 0.4700i

Column 6
-2.9000 - 0.0200i

5. Hallar la derivada del coviente de la divisin de p(x)=

( )

>> %ingresamos los polinomios a dividir


>> P=[1 0 0 0 0 0 0 0 0 1 1]
P=
1

>> D=[1 0 -1i 2]


D=
1.0000

Mtodos Numricos

0 - 1.0000i 2.0000

Pgina 18

>> %calculo del cociente de la divicion


>> [Q,R]=deconv(P,D)
Q=
Columns 1 through 5
1.0000

0 + 1.0000i -2.0000

-1.0000

Columns 6 through 8
0 - 4.0000i 4.0000 - 1.0000i 6.0000
R=
Columns 1 through 5
0

Columns 6 through 10
0

1.0000 +12.0000i -7.0000 + 8.0000i

Column 11
-11.0000
>> %calculo de la derivada de Q(X)
>> dQ=polyder(Q)
dQ =
Columns 1 through 5
7.0000

0 + 5.0000i -8.0000

-3.0000

Columns 6 through 7
0 - 8.0000i 4.0000 - 1.0000i

Mtodos Numricos

Pgina 19

6. Hallar las races de la funcin f(x)=xsenx+2 cercanas a -6,-4,4 y 6


>> %raiz cercana a -6
>> fzero('x*sin(x)+2',-6)
ans = -5.9398
>> %raiz cercana a -4
>> fzero('x*sin(x)+2',-4)
ans = -3.7108
>> %raiz cercana a 4
>> fzero('x*sin(x)+2',4)
ans = 3.7108
>> %raiz cercana a 6
>> fzero('x*sin(x)+2',6)
ans = 5.9398

Mtodos Numricos

Pgina 20

CAPITULO 5: GRFICOS BIDIMENSIONALES (2-D)


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

Graficamos:

Mtodos Numricos

Pgina 21

2. Grafique:
a)
b) | |

Para
a)

graficando:

Mtodos Numricos

Pgina 22

| |

Mtodos Numricos

Pgina 23

4. Grafique las funciones polares:


a) R1=2sen (3 ),
B) R2= 5
C) R3=2-SEN

%GRAFIQUE LAS FUNCIONES POLARES


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

90

120

60
1.5
1

150

30

0.5

180

210

330

240

300
270

%B
Mtodos Numricos

Pgina 24

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

90

40

120

60
30
20

150

30

10

180

210

330

240

300
270

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

90

120

60
2

150

30
1

180

210

330

240

300
270

Mtodos Numricos

Pgina 25

%A
t=0:0.1:2*pi
x1=sin(t)
y1=sin(2*t)
%PRIMERA FIGURA
subplot(1,2,1)
plot(t,x1)
%SEGUNDA FIGURA
subplot(1,2,2)
plot(t,y1)

0.8

0.8

0.6

0.6

0.4

0.4

0.2

0.2

-0.2

-0.2

-0.4

-0.4

-0.6

-0.6

-0.8

-0.8

-1

Mtodos Numricos

-1

Pgina 26

%B

t=0:0.1:2*pi
x1=sin(t)
y1=cos(t)
%PRIMERA FIGURA
subplot(1,2,1)
plot(t,x1)
%SEGUNDA FIGURA
subplot(1,2,2)
plot(t,y1)

0.8

0.8

0.6

0.6

0.4

0.4

0.2

0.2

-0.2

-0.2

-0.4

-0.4

-0.6

-0.6

-0.8

-0.8

-1

Mtodos Numricos

-1

Pgina 27

CAPTULO 6: GRFICOS TRIDIMENSIONALES (3D)


EJERCICIOS RESUELTOS
1. Grafique usando mesh, surf, contour y contour3,
>> [x,y]=meshgrid(-3:0.2:3,-3:0.2:3);
>> mesh(x,y,x.^2-2*y.^2)
>> surf(x,y,x.^2-2*y.^2)
>> contour(x,y,x.^2-2*y.^2)
>> contour3(x,y,x.^2-2*y.^2)

2. Grafique usando surf,

( )

( )

>> [x,y]=meshgrid(-3:0.2:3,-3:0.2:3);
>> z=floor(sin(x))+cos(y);
>> surf(x,y,z)

Mtodos Numricos

Pgina 28

EJERCICIOS PROPUESTOS
1. Grafique usando mesh, surf, contour y contour3

Para x=0.25:0.25:5
y=0.25:0.25:5
>> [x,y]=meshgrid(-5:0.25:5,-5:0.25:5);
>> z3=(2*x*y)./(x.^2+y.^2);
>> subplot(2,2,1);
>> surf(x,y,z3)
>> subplot(2,2,2);
>> mesh(x,y,z3)
>> subplot(2,2,3);
>> contour(x,y,z3)
>> subplot(2,2,4);
>> contour3(x,y,z3)

Mtodos Numricos

Pgina 29

2. Grafique x=y=z=

( )

>> a=0:pi/20:2*pi;
>> ezplot3('2-sin(a)','2-sin(a)','2-sin(a)')

Mtodos Numricos

Pgina 30

3. Use subplot para dividir la ventana en 1x2 para luego graficar en cada
una de ellas las siguientes curvas paramtricas (R R3).
) {

) {

()
( )
()
()
()

>> t=0:0.1:2*pi;
>> x=sin(t);
>> y=sin(2*t);
>> z=2*t;
>> subplot(1,2,1)
>> plot3(x,y,z)
>> grid
>> x=sin(t);
>> y=cos(t);
>> z=cos(t);
>> subplot(1,2,2)
>> plot3(x,y,z)
>> grid

Mtodos Numricos

Pgina 31

4. Haga la grfica de la parte de la figura con radio = 5

>> [u,v]=meshgrid(pi/3:pi/20:7*pi/4,0:pi/20:pi/2);
>> x=5*sin(v).*cos(u);
>> y=5*sin(v).*sin(u);
>> z=5*cos(v);
>> mesh(x,y,z)

Mtodos Numricos

Pgina 32

5. Grafique el toroide
{

( )(
( )(

( ))
( ))
( )

>> [t,v]=meshgrid(0:pi/20:2*pi,0:pi/20:2*pi);
>> x=cos(t).*(4-cos(v));
>> y=sin(t).*(4-cos(v));
>> z=sin(v);
>> mesh(x,y,z)

Mtodos Numricos

Pgina 33

CAPITULO 7: PROGRAMACIN EN MATLAB


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

Solucin:
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
6
2

11
9
3

2
1
7

>> B=[5 9;2 5]


B=
5
2

9
5

>> bloque(A,B)
ans =
4
6
2
0
0

11
9
3
0
0

2
1
7
0
0

0
0
0
5
2

Mtodos Numricos

0
0
0
9
5

Pgina 34

2. Crear la funcin (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
Solucin:
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=

>> r = mmedad(22)
r=
1

Mtodos Numricos

Pgina 35

3. Escribir una funcin (programa) para calcular el mayor y menor lado de un


tringulo sin usar la sentencia if.

Solucin:
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 =

Mtodos Numricos

Pgina 36

4. Implemente la funcin polysum(p,q) la cual sume los dos polinomios .

Solucin:
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

>> q=[8 5 4]
q=
8

>> suma=polysum(p,q)

suma =

10

10

Mtodos Numricos

Pgina 37

Anda mungkin juga menyukai