Académique Documents
Professionnel Documents
Culture Documents
Mery Rodríguez
NRC: 3806
1
Metodos Numericos
Deber 1
1. EJERCICIO: Escribir un programa en Matlab que le pida al usuario un número entero
positivo menor que un millón (1000000) y devuelva a pantalla el número e indique si es par
o impar.
1 % E j e r c i c i o 1 Deber
2 function [ Resp ]= Ingrese_numero (num)
3 i f num <= 1000000
4 i f mod(num , 2 ) == 0
5 input ( ’ Numero� par ’ )
6 e l s e input ( ’ Numero� impar ’ )
7 end
8 e l s e input ( ’ El �numero� i n g r e s a d o � e s � i n v a l i d o ’ )
9 end
10 end
Corrida
>> Ingrese_numero ( 5 )
Numero impar
>> Ingrese_numero ( 1 0 0 0 0 6 )
Numero pa r
>> Ingrese_numero ( 1 0 0 0 0 0 0 0 )
El numero i n g r e s a d o e s i n v a l i d o
6 a l p h a = 0 : pi / 1 0 0 : 2 ⇤ pi ;
7 x = r ⇤ cos ( a l p h a ) + h ;
8 y = r ⇤ sin ( a l p h a ) + k ;
9 plot ( x , y )
10 axis s q u a r e
2 Mery Rodriguez
Metodos Numericos
1 % E j e r c i c i o 3 Deber
2 x1 = 15:0.01: 1;
3 x2 = 1:0.01:1;
4 x3 = 1 : 0 . 0 1 : 1 5 ;
5
6 f 1 = exp( x1 )
7 f 2 = abs ( x2 ) ;
8 f 3 = x3 .^2+1;
9
10 d f 1 = d i f f ( f 1 ) . / d i f f ( x1 ) ;
11 d f 2 = d i f f ( f 2 ) . / d i f f ( x2 ) ;
12 d f 3 = d i f f ( f 3 ) . / d i f f ( x3 ) ;
13
14 hold on
15 plot ( x1 , f1 , ’ cyan ’ ) ;
16 plot ( x2 , f2 , ’ cyan ’ ) ;
17 plot ( x3 , f3 , ’ cyan ’ ) ;
18
19 plot ( x1 ( 2 : end ) , df1 , ’ b l u e ’ ) ;
20 plot ( x2 ( 2 : end ) , df2 , ’ b l u e ’ ) ;
21 plot ( x3 ( 2 : end ) , df3 , ’ b l u e ’ ) ;
3 Mery Rodriguez
Metodos Numericos
22 axis ( [ 5 , 5 , 5 ,5]) ;
23 hold o f f
24 grid on
4. EJERCICIO: Elabore un programa que permita calcular las raíces reales de un polinomio
de segundo grado de la forma x2 + bx + c = 0. Tenga en cuenta que dependiendo del valor del
discriminante (D = b2 4ac), el sistema puede dar raíces reales (D 0) o raíces complejas
(D < 0). El programa debe informar al usuario si el sistema no tiene raíces reales. Además
se debe indicar si las raíces reales son iguales (D = 0), o diferentes (D > 0).
1 % E j e r c i c i o 4 Deber
2 function [ x]= Raices_ecuacion_cuad ( a , b , c )
3 d i s c = b^2 4⇤a⇤ c ;
4
5 i f d i s c > 0 %r a i c e s d i f e r e n t e s
6 input ( ’ Ecuacion � de � dos � r a i c e s � d i f e r e n t e s : ’ )
7 x ( 1 ) =0.5⇤( b+sqrt ( d i s c ) ) ;
8 x ( 2 ) =0.5⇤( b sqrt ( d i s c ) ) ;
9 else
10 i f d i s c == 0 %r a i c e s i g u a l e s
11 input ( ’ Ecuacion � de �una� s o l a � r a i z ’ )
12 x ( 1 )= 0.5⇤( b ) ;
13 else % raices imaginarias
14 input ( ’ Ecuacion �no� p o s e e � r a i c e s � r e a l e s ’ )
15 input ( ’ Las � r a i c e s � i m a g i n a r i a s � son : ’ )
16 x ( 1 ) =0.5⇤( b+sqrt ( d i s c ) ) ;
17 x ( 2 ) =0.5⇤( b sqrt ( d i s c ) ) ;
18 end
19 end
20 end
4 Mery Rodriguez
Metodos Numericos
Corrida
>> Raices_ecuacion_cuad ( 2 , 4 , 2 )
Ecuacion de una s o l a r a i z
ans =
F = 95 C + 32
K = C + 273, 15
8
R = 10 C
Construir un programa en Matlab que permita pasar de una escala a otra. El programa debe
solicitar al usuario el ingreso de la información necesaria para su funcionamiento. Además,
el programa siempre esperará información hasta que se pulse la tecla s.
1 % E j e r c i c i o 5 Deber
2 disp ( ’ C o n v e r s i o n � de � Temperatura ’ )
3 a=input ( ’ I n g r e s e � l a � l e t r a � s � para � c o n t i n u a r : �� ’ , ’ s ’ ) ;
4 i f a==’ s ’
5 disp ( ’ E s c o j a �una� o p c i o n ’ )
6 disp ( ’ 1. � C e l c i u s �a� F a h r e n h e i t ’ )
7 disp ( ’ 2. � C e l c i u s �a� K e l v i n ’ )
8 disp ( ’ 3. � C e l c i u s �a� Rankine ’ )
9 n=input ( ’ I n g r e s e � l a � o p c i o n : � ’ ) ;
10 switch n
11 case 1
12 x=input ( ’ I n g r e s e � l a � t em p e ra tu r a � en � C e l s i u s : � ’ ) ;
13 f =((9/5) ⇤x ) +32;
14 disp ( ’ La� t em p er at u r a � en � e s c a l a � F a h r e n h e i t � e s : ’ )
15 disp ( f )
16 case 2
17 x=input ( ’ I n g r e s e � l a � t em p e ra tu r a � en � C e l s i u s : � ’ ) ;
18 k=x + 2 7 3 . 1 5 ;
19 disp ( ’ La� t em p er at u r a � en � e s c a l a � K e l v i n � e s : ’ )
20 disp ( k )
21 case 3
22 x=input ( ’ I n g r e s e � l a � t em p e ra tu r a � en � C e l s i u s : � ’ ) ;
23 r =(8/10) ⇤x ;
24 disp ( ’ La� t em p er at u r a � en � e s c a l a � Rankin � e s : ’ )
25 disp ( r )
26 otherwise
27 disp ( ’NO�EXISTE�ESA�OPCION ’ )
28 end
29 else
30 disp ( ’ L e t r a � i n c o r r e c t a ’ )
31 end
5 Mery Rodriguez
Metodos Numericos
Corrida
>> E j e r c i c i o 5 D e b e r
C o n v e r s i o n de Temperatura
I n g r e s e l a l e t r a s para c o n t i n u a r : s
E s c o j a una o p c i o n
1. Celcius a Fahrenheit
2. Celcius a Kelvin
3. C e l c i u s a Rankine
Ingrese la opcion : 1
I n g r e s e l a te mp er at ur a en C e l s i u s : 12
La t e m p e rat u r a en e s c a l a F a h r e n h e i t e s :
53.6000
6. EJERCICIO: Crear una función m.file con la que se pueda graficar la función paramétrica
siguiente:
8
< x(t) = (R + r)cos(t) rcos( R+r
r
)t
f (x) =
:
y(t) = (R + r)sen(t) rsen( R+r
r
)t
Los parámetros de entrada deben ser R,r,a,b y el número n de puntos. t 2 [a, b] y a,b 2 R.
1 % E j e r c i c i o 6 Deber
2 function f_param (R, r , a , b , n )
3 t = linspace ( a , b , n ) ;
4
6 Mery Rodriguez
Metodos Numericos
7 Mery Rodriguez
Metodos Numericos
25 c o n t a d o r=c o n t a d o r +1;
26 case ’ s ’
27 disp ( ’ F i n a l i z a d o ’ )
28 break ;
29 otherwise
30 disp ( ’ E l i j a � c o r r e c t a m e n t e ’ )
31 end
32 end
Corrida
>> E j e r c i c i o 7 D e b e r
E s c r i b a <a> para i n g r e s a r una s u s t a n c i a
E s c r i b a <s> para s a l i r
E l i j a una o p c i o n . . . a
I n g r e s e e l numero de l a s u s t a n c i a : 1
I n g r e s e e l p e s o : 12
I n g r e s e e l p e s o : 13
I n g r e s e e l p e s o : 14
I n g r e s e e l p e s o : 26
===DATOS====
Numero de muestra : 1
Numero de s u s t a n c i a : 1
=PESOS=
Peso N_1 : 12
Peso N_2 : 13
Peso N_3 : 14
Peso N_4 : 26
Peso e l i m i n a d o : 12
Peso promedio : 1 . 7 6 6 6 6 7 e+01
E s c r i b a <a> para i n g r e s a r una s u s t a n c i a
E s c r i b a <s> para s a l i r
E l i j a una o p c i o n . . . s
Finalizado
1 P 1
1P µ)T |
fx (x1 , ..., xn ) = (2⇡)n/2 | |1/2
e( 2
(x (x µ))
P
Para
✓ el caso
◆ n = 2, usar los siguientes valores de las constantes µ = (10, 10) ,
T
=
3 1 P P
. El símbolo | | significa el determinante de la matriz .
1 4
1 % E j e r c i c i o 8 Deber
2 syms x1 ;
3 syms x2 ;
4 x=[ x1 ; x2 ] ;
5 u=t r a n s p o s e ( [ 1 0 1 0 ] ) ;
6 E=[3 1; 1 4 ] ;
7 a=2⇤pi ⇤ sqrt ( det (E) ) ;
8 Mery Rodriguez
Metodos Numericos
9. EJERCICIO: Escriba un programa que busque los números amigables y diferentes entre 1
y 1000. Dos números son amigables si el primero es la suma de los divisores del segundo y
viceversa.
1 % E j e r c i c i o 9 Deber
2 inicio = 0;
3 f i n = 1000;
4 numeros = i n i c i o : f i n ; tamano = length ( numeros ) ;
5 f o r i = 1 : tamano
6 a = sum( d i v i s o r s ( numeros ( i ) ) ) numeros ( i ) ;
7 f o r j = ( i +1) : ( tamano )
8 b = sum( d i v i s o r s ( numeros ( j ) ) ) numeros ( j ) ;
9 i f a == numeros ( j )
10 i f b == numeros ( i )
11 f p r i n t f ( ’ %d�y� %d� son � amigos \n ’ , numeros ( i ) , numeros ( j ) )
12 end
13 end
14 end
15 end
Corrida
9 Mery Rodriguez
Metodos Numericos
>> E j e r 9
numeros_amigos
220 y 284 son amigos
10. EJERCICIO: Realizar una función tal que ingresado un numero N, se impriman por pan-
talla todos los números primos de 1 hasta N. Ej: Ingreso el número 13 y se imprimen por
pantalla: 1, 3, 5, 7, 11, 13
1 % E j e r c i c i o 10 Deber
2 function E j e r c 1 0 (num)
3 n=2;
4 a =1;
5 numero=num ;
6 while n<=numero
7 i f n>=a
8 a=a +1;
9 i f (mod( n , a )==0 && ( n~=a ) ) n=n+1; a =1;
10 end
11 i f (mod( n , a )==0 && ( n==a ) )
12 disp ( n ) ; n=n+1; a =1;
13 end
14 end
15 end
Corrida
>> E j e r c 1 0 ( 1 3 )
1
11
13
11. EJERCICIO: En cada uno de los casos siguientes, halle el error absoluto Ez y el error
relativo Rz y determine el número de cifras significativas de la aproximación.
a) x = 2, 71828182, x̂ = 2, 7182
b) y = 98350, ŷ = 98000
c) z = 0, 000068, ẑ = 0, 00006
1 % E j e r c i c i o 11 Deber
2 x =2.71828182;
3 x1 = 2 . 7 1 8 2 ;
4 y =98350;
5 y1 =98000;
10 Mery Rodriguez
Metodos Numericos
6 z =0.000068;
7 z1 = 0 . 0 0 0 0 6 ;
8
9 eax=abs ( x x1 ) ;
10 e r x=abs ( ( x x1 ) /x ) ;
11 c s x=f l o o r ( ( log ( 2 ⇤ e r x ) / log ( 1 0 ) ) ) ;
12 disp ( ’ x =2.71828182 ’ )
13 f p r i n t f ( ’ E r r o r � a b s o l u t o : � %d\n ’ , eax )
14 f p r i n t f ( ’ E r r o r � r e l a t i v o : � %d\n ’ , e r x )
15 f p r i n t f ( ’ C i f r a s � s i g n i f i c a t i v a s : � %d\n\n ’ , c s x )
16
17 eay=abs ( y y1 ) ;
18 e r y=abs ( ( y y1 ) /y ) ;
19 c s y=f l o o r ( ( log ( 2 ⇤ e r y ) / log ( 1 0 ) ) ) ;
20 disp ( ’ y=98350 ’ )
21 f p r i n t f ( ’ E r r o r � a b s o l u t o : � %d\n ’ , eay )
22 f p r i n t f ( ’ E r r o r � r e l a t i v o : � %d\n ’ , e r y )
23 f p r i n t f ( ’ C i f r a s � s i g n i f i c a t i v a s : � %d\n\n ’ , c s y )
24
25 e a z=abs ( z z1 ) ;
26 e r z=abs ( ( z z1 ) / z ) ;
27 c s z=f l o o r ( ( log ( 2 ⇤ e r z ) / log ( 1 0 ) ) ) ;
28 disp ( ’ z =0.000068 ’ )
29 f p r i n t f ( ’ E r r o r � a b s o l u t o : � %d\n ’ , e a z )
30 f p r i n t f ( ’ E r r o r � r e l a t i v o : � %d\n ’ , e r z )
31 f p r i n t f ( ’ C i f r a s � s i g n i f i c a t i v a s : � %d\n ’ , c s z )
Corrida
>> E j e r c i c i o 1 1 D e b e r
x =2.71828182
E r r o r a b s o l u t o : 8 . 1 8 2 0 0 0 e 05
E r r o r r e l a t i v o : 3 . 0 0 9 9 9 0 e 05
Cifras significativas : 4
y=98350
E r r o r a b s o l u t o : 350
E r r o r r e l a t i v o : 3 . 5 5 8 7 1 9 e 03
Cifras significativas : 2
z =0.000068
E r r o r a b s o l u t o : 8 . 0 0 0 0 0 0 e 06
E r r o r r e l a t i v o : 1 . 1 7 6 4 7 1 e 01
Cifras significativas : 0
12. EJERCICIO: Encuentre los desarrollos de Taylor, para las funciones siguientes: 1 1 h y
cos(h), con órdenes de aproximación O(h4 ) y O(h6 ), respectívamente. Determine el orden
de aproximación de su suma y de su producto
1
f (h) =
1 h
11 Mery Rodriguez
Metodos Numericos
p(h) = 1 h + h2 h3 + O(h4 )
g(h) = cos(h)
h2 h4
p2(h) = 1 + + + O(h6 )
2! 4!
SUMA
f (h) + g(h) = p(h) + p2(h) + O(hr )
3h2 h4
f (h) + g(h) = 2 h + h3 + + O(h4 )
2 24
PRODUCTO
f (h) ⇤ g(h) = p(h) ⇤ p2(h) + O(hr )
h2 h3
f (h) ⇤ g(h) = 1 h + + O(h4 )
2 2
13. EJERCICIO: Encuentre una raíz positiva, con los métodos de la biseción y newton, a
mano, de la ecuación
x2 4xSen(x) + (2Sen(x))2 = 0;
que sea exacta hasta la segunda cifra significativa, solo con el empleo de una calculadora.
Método de la bisección
f (x) = x2 4xSen(x) + (2Sen(x))2
a=1.6
b=2
f (a)f (b) < 0
0, 58471 < 0
12 Mery Rodriguez
Metodos Numericos
X5 = 1,8896
14. EJERCICIO: Con los programas: método de la bisección, newton y método de la secante,
encuentre una raíz de
f (x) = x tang(x),
38 %MÃ c todo de l a s e c a n t e
39 xo =1;
13 Mery Rodriguez
Metodos Numericos
40 f x 3=@( x ) ( x tan ( x ) ) ;
41 x1 =2;
42 while ( abs ( x1 xo ) >10^ 5)x2=x1 ( f x 3 ( x1 ) ⇤ ( x1 xo ) ) / ( f x 3 ( x1 ) f x 3 ( xo ) ) ;
43 xo=x1 ;
44 x1=x2 ;
45 end
46 disp ( x1 ) ;
Corrida
>> E j e r c i c i o 1 4 D e b e r
a =
La r a i z e s
1.5708
1 . 5 9 5 0 e 05
2 . 4 4 4 7 e 05
1 % E j e r c i c i o 15 Deber
2 x = 10:0.1:10;
3 y=sqrt ( 3 ) ⇤ sin ( x ) cos ( x ) ;
4 x0=input ( ’ I n g r e s e � e l � v a l o r � en �x : � ’ ) ;
5 f x=@( x ) ( ( 3 ^ 0 . 5 ) ⇤ sin ( x ) cos ( x ) ) ;
6 gx=@( x ) ( ( 3 ^ 0 . 5 ) ⇤ cos ( x )+sin ( x ) ) ;
7 x1=x0 ( f x ( x0 ) / gx ( x0 ) ) ;
8 while abs ( x x0 ) >(10^( 5) )
9 x2=x1 ( f x ( x1 ) / gx ( x1 ) ) ;
10 x0=x1 ; x1=x2 ;
11 end
12 disp ( x1 )
Corrida
>> E j e r c i c i o 1 5 D e b e r
I n g r e s e e l v a l o r en x : 4
3.6521
14 Mery Rodriguez
Metodos Numericos
1 % E j e r c i c i o 16 Deber
2 f x=@( x ) ( cosh ( x )+cos ( x ) 3) ;
3 a= 10;b=10; i =0;
4 while abs ( a b ) >0.000001 c=(a+b ) / 2 ; i=i +1;
5 i f f x ( a ) ⇤ f x ( c )<0b=c ;
6 e l s e a=c ;
7 end
8 end
9 disp ( ’ El � v a l o r � de � l a � r a i z � e s ’ )
10 disp ( c )
11 disp ( ’ El �numero� de � i t e r a c i o n � son : ’ )
12 disp ( i )
Corrida
>> E j e r c i c i o 1 6 D e b e r
El v a l o r de l a r a i z e s
1.8579
El numero de i t e r a c i o n son :
25
17. EJERCICIO: Un objeto está situado en un plano cuya pendiente varía a una tasa constante
!. La posición del objeto, al instante t, está dada por la fórmula
g
s(t, !) = 2! 2
[senh(!t) sen(!t)] ,
10 plot ( x , y , ’ b l u e ’ , ’ l i n e w i d t h ’ , 2 )
11 axis s q u a r e ;
12 grid on ;
13 i f ( f x ( a ) ⇤ f x ( b ) )>0
14 l i m i t e d i s p ( ’ L i m i t e s �� i n c o r r e c t o s ’ ) ; disp ( ’ \n�No� e x i s t e � r a i z ’ ) ;
15 return ;
16 end
17 while abs ( b (a ) ) >(10^ 5)
18 c=(a+b ) / 2 ;
15 Mery Rodriguez
Metodos Numericos
19 i f f x ( c )==0
20 break
21 end
22 i f f x ( a ) ⇤ f x ( c )<0
23 b=c ;
24 e l s e a=c ;
25 end
26 count=count +1;
27 end
28 disp ( ’ El � v a l o r � de �w� e s ’ )
29 disp ( c ) ;
30 disp ( ’ El �numero� de � i n t e r a c c i o n e s � e s ’ )
31 disp ( count )
Corrida
>> E j e r c i c i o 1 7 D e b e r
El v a l o r de w e s
0.6121
El numero de i n t e r a c c i o n e s e s
14
18. EJERCICIO: Escriba e implemente un programa en Matlab para calcular la raiz cuadrada
de un número positivo a, basado en el método de Newton.
1 % E j e r c i c i o 18 Deber
x0
2 =1;
a=input
3 ( " i n g r e s e e l numero p o s i t i v o " ) ;
i f ( a>0)
4
5 f x=@( x ) ( x.^2 a) ;
6 gx=@( x ) (2⇤ x ) ;
7 x1=x0 f x ( x0 ) / gx ( x0 ) ;
while
8 abs ( x1 x0 ) >0.00001
9 x2=x1 f x ( x1 ) / gx ( x1 ) ;
10 x0=x1 ;
11 x1=x2 ;
end
12
disp ( ’ l a � r a i z � e s : ’ ) ;
13
disp ( x1 ) ;
14
else
15
disp ( ’ u s t e d � i n g r e s o �un�numero� n e g a t i v o ’ ) ;
16
end
17
Corrida
>> E j e r 1 8
i n g r e s e e l numero p o s i t i v o 5 4
la raiz es :
7.3485
16 Mery Rodriguez