Deber
Deber
Deber
MÉTODOS NUMÉRICOS
DEBER N.-1
Sangolquı́ 2019
Ejercicio 1
Considere la función f (x) = cosh(x) + cos(x) − γ, con γ = 1, 2, 3. En-
cuentre un intervalo que contenga un cero f para cada valor de γ y calcule
con el método de la bisección.
Solución:
γ=1 :
Código
clc
clear all
syms x ;
y = cosh ( x ) + cos ( x ) -1;
fun = inline ( y ) ;
display ( y ) ;
esc = -5:0.1:5;
ezplot ( fun , esc ) ;
grid on
a = -5;
b =5;
tol =0.01;
fa = feval ( fun , a ) ;
fb = feval ( fun , b ) ;
k =1;
if ( fa * fb ) >0;
disp ( ' No existe raiz en ese intervalo ' ) ;
return ;
end
fprintf ( ' k \ t \ t \ ta \ t \ t \ tb \ t \ t \ tc \ t \ t \ terror \ n ' ) ;
while abs (b - a ) > tol
c =( a + b ) /2;
fc = feval ( fun , c ) ;
error = abs (b - a ) ;
fprintf ( ' %i \ t \ t %.3f \ t \ t %.3f \ t \ t %.3f \ t \ t
%.3f \ n ' ,k ,a ,b ,c , error ) ;
if fc ==0
1
a=c;
b=c;
disp ( c ) ;
return
end
if ( fa * fc ) <0
b=c;
fb = fc ;
else
a=c;
fa = fc ;
end
k = k +1;
end
fprintf ( ' Su raiz es :\ t %.3f \ n ' , c ) ;
fprintf ( ' Numero de iteraciones %i \ n ' ,k -1)
Corrida
Fig.1 Corrida
2
Fig.2 Gráfico
γ=2 :
Código
clc
clear all
syms x ;
y = cosh ( x ) + cos ( x ) -2;
fun = inline ( y ) ;
display ( y ) ;
esc = -5:0.1:5;
ezplot ( fun , esc ) ;
grid on
a = -5;
b =5;
tol =0.01;
fa = feval ( fun , a ) ;
fb = feval ( fun , b ) ;
k =1;
if ( fa * fb ) >0;
3
disp ( ' No existe raiz en ese intervalo ' ) ;
return ;
end
fprintf ( ' k \ t \ t \ ta \ t \ t \ tb \ t \ t \ tc \ t \ t \ terror \ n ' ) ;
while abs (b - a ) > tol
c =( a + b ) /2;
fc = feval ( fun , c ) ;
error = abs (b - a ) ;
fprintf ( ' %i \ t \ t %.3f \ t \ t %.3f \ t \ t %.3f \ t \ t
%.3f \ n ' ,k ,a ,b ,c , error ) ;
if fc ==0
a=c;
b=c;
disp ( c ) ;
return
end
if ( fa * fc ) <0
b=c;
fb = fc ;
else
a=c;
fa = fc ;
end
k = k +1;
end
fprintf ( ' Su raiz es :\ t %.3f \ n ' , c ) ;
fprintf ( ' Numero de iteraciones %i \ n ' ,k -1)
Corrida
Fig.3 Corrida
4
Fig.4 Gráfico
γ=3 :
clc
clear all
syms x ;
y = sinh ( x ) + cos ( x ) -3;
fun = inline ( y ) ;
display ( y ) ;
esc = -5:0.1:5;
ezplot ( fun , esc ) ;
grid on
a =0;
b =3;
tol =0.01;
fa = feval ( fun , a ) ;
fb = feval ( fun , b ) ;
k =1;
if ( fa * fb ) >0;
disp ( ' No existe raiz en ese intervalo ' ) ;
5
return ;
end
fprintf ( ' k \ t \ t \ ta \ t \ t \ tb \ t \ t \ tc \ t \ t \ terror \ n ' ) ;
while abs (b - a ) > tol
c =( a + b ) /2;
fc = feval ( fun , c ) ;
error = abs (b - a ) ;
fprintf ( ' %i \ t \ t %.3f \ t \ t %.3f \ t \ t %.3f \ t \ t
%.3f \ n ' ,k ,a ,b ,c , error ) ;
if fc ==0
a=c;
b=c;
disp ( c ) ;
return
end
if ( fa * fc ) <0
b=c;
fb = fc ;
else
a=c;
fa = fc ;
end
k = k +1;
end
fprintf ( ' Su raiz es :\ t %.3f \ n ' , c ) ;
fprintf ( ' Numero de iteraciones %i \ n ' ,k -1)
hold on
plot (c , fun ( c ) , ' * ' )
Corrida
6
Fig.5 Corrida
Fig.6 Gráfico
7
Ejercicio 2
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, ω) = [sinh(ωt) − sin(ωt)]
2ω 2
donde g = 9,8m/s2 es la aceleracón de la gravedad. Asumiendo que el objeto
se ha desplazado 1 metros en 1 segundo, calcule el valor de ω, usando el
método de la bisección, con una tolerancia de 10−5 . ¿Cuántas iteraciones se
requieren para alcanzar la tolerancia?
Solución:
s(1, ω) = 1[m]
9,8
1= [sinh(ωt) − sin(ωt)]
2ω 2
9,8[sinh(ωt) − sin(ωt)] − 2ω 2 = 0
Código
clear all
clc
close all
y = input ( ' Ingrese la funcion : ' , ' s ' ) ;
fun = inline ( y ) ;
a = input ( ' Ingrese el valor de a : ' ) ;
b = input ( ' Ingrese el valor de b : ' ) ;
tol = input ( ' Ingrese la tolerancia : ' ) ;
ite =( log (b - a ) - log ( tol ) ) / log (2) +1;
ezplot ( fun )
grid on
xlabel ( ' x ' ) ;
ylabel ( ' f ( x ) ' ) ;
while fun ( a ) * fun ( b ) >0
disp ( ' No existe raiz en el intervalo ' ) ;
a = input ( ' Ingrese el valor de a : ' ) ;
b = input ( ' Ingrese el valor de b : ' ) ;
end
i =1;
fprintf ( ' \ tit \ t \ ta \ t \ t \ tb \ t \ t \ tc \ t \ terror \ n ' ) ;
while abs (b - a ) > tol
8
c =( a + b ) /2;
fprintf ( ' \ t %i \ t %3.5 f \ t \ t %f \ t %3.5 f \ t \ t %f \ n ' , i , a ,
b , c , (b - a ) ) ;
if fun ( a ) * fun ( c ) <=0
b=c;
else
a=c;
end
i = i +1;
end
c =( a + b ) /2;
fprintf ( ' \ t %i \ t %3.5 f \ t \ t %f \ t %3.5 f \ t \ t %f \ n ' , i , a , b ,
c , (b - a ) ) ;
fprintf ( ' El numero de iteraciones es : %0.2 f \ n ' , round
( ite ) )
fprintf ( ' La raiz es %0.2 f \ n ' ,c )
figure ;
ezplot ( fun )
grid on
xlabel ( ' x ' ) ;
ylabel ( ' f ( x ) ' ) ;
hold on
plot (c , fun ( c ) , ' * ' )
Corrida
9
Fig.7 Gráfica de la función
10
Fig.8 corrida del programa
Ejercicio 3
Escriba e implemente un programa en MATLAB para calcular la raı́z
cuadrada de un número positivo a, basado en el método de Newton.
Solución:
Código
clear all
clc
close all
syms x
num = input ( ' Ingrese el numero del que desea obtener
raiz cuadrada : ' ) ;
fun = inline ( x .^2 - num ) ;
deri = diff ( x .^2 - num ) ;
11
der = inline ( deri ) ;
x0 = input ( ' Ingrese el valor de x_0 : ' ) ;
tol = input ( ' Ingrese la tolerancia : ' ) ;
ezplot ( fun )
grid on
xlabel ( ' x ' ) ;
ylabel ( ' f ( x ) ' ) ;
i =1;
fprintf ( ' \ tit \ tx_0 \ t \ t \ tx_1 \ t \ t \ t \ terror \ n ' ) ;
while abs ( fun ( x0 ) ) >= tol
x1 = x0 -( fun ( x0 ) ) /( der ( x0 ) ) ;
fprintf ( ' \ t %i \ t %3.5 f \ t \ t %f \ t \ t %3.5 f \ n ' , i , x0 , x1
, abs ( fun ( x0 ) ) ) ;
x0 = x1 ;
i = i +1;
end
x1 = x0 -( fun ( x0 ) ) /( der ( x0 ) ) ;
fprintf ( ' \ t %i \ t %3.5 f \ t \ t %f \ t \ t %3.5 f \ n ' , i , x0 , x1 ,
abs ( fun ( x0 ) ) ) ;
fprintf ( ' La raiz es %3.5 f \ n ' , x0 )
figure ;
ezplot ( fun )
grid on
xlabel ( ' x ' ) ;
ylabel ( ' f ( x ) ' ) ;
hold on
plot ( x0 , fun ( x0 ) , ' * ' )
Corrida
12
Fig.10 Gráfica de la función
13
Fig.12 Gráfica de la función y la raı́z
Ejercicio 4
Un proyectil es lanzado con velocidad inicial v0 y un ángulo α en un
túnel de altura
p h. El proyectil llega a su alcance máximo cuando α es tal
que sin(α) = 2gh/v02 , donde g = 9,8m/s2 es la aceleración de la gravedad.
Calule α usando el método de Newton, asumiendo que v0 = 10m/s y h = 1m.
Solución:
Reemplazando los valores de h, v0 y g se tiene:
r
2 ∗ 9,8 ∗ 1
sin(α) =
102
49
f (x) = sin2 (α) −
250
Código
clear all
clc
14
close all
syms x
y = input ( ' Ingrese la funcion : ' ) ;
fun = inline ( y ) ;
deri = diff ( y ) ;
der = inline ( deri ) ;
x0 = input ( ' Ingrese el valor de x_0 : ' ) ;
tol = input ( ' Ingrese la tolerancia : ' ) ;
ezplot ( fun )
grid on
xlabel ( ' x ' ) ;
ylabel ( ' f ( x ) ' ) ;
i =1;
fprintf ( ' \ tit \ tx_0 \ t \ t \ tx_1 \ t \ t \ t \ terror \ n ' ) ;
while abs ( fun ( x0 ) ) >= tol
x1 = x0 -( fun ( x0 ) ) /( der ( x0 ) ) ;
fprintf ( ' \ t %i \ t %3.5 f \ t \ t %f \ t \ t %3.5 f \ n ' , i , x0 , x1
, abs ( fun ( x0 ) ) ) ;
x0 = x1 ;
i = i +1;
end
x1 = x0 -( fun ( x0 ) ) /( der ( x0 ) ) ;
fprintf ( ' \ t %i \ t %3.5 f \ t \ t %f \ t \ t %3.5 f \ n ' , i , x0 , x1 ,
abs ( fun ( x0 ) ) ) ;
fprintf ( ' La raiz es %3.5 f \ n ' , x0 )
figure ;
ezplot ( fun )
grid on
xlabel ( ' x ' ) ;
ylabel ( ' f ( x ) ' ) ;
hold on
plot ( x0 , fun ( x0 ) , ' * ' )
Corrida
15
Fig.13 Gráfica de la función
16
Fig.15 Gráfica de la función y la raı́z
Ejercicio 5
Aplicando el método de Newton encontrar el cero de la función:
1 1
f (x) = − ln(x) + e−x −
2 5
Solución:
Código
clear all
clc
close all
syms x
y = input ( ' Ingrese la funcion : ' ) ;
fun = inline ( y ) ;
deri = diff ( y ) ;
der = inline ( deri ) ;
x0 = input ( ' Ingrese el valor de x_0 : ' ) ;
17
tol = input ( ' Ingrese la tolerancia : ' ) ;
ezplot ( fun )
grid on
xlabel ( ' x ' ) ;
ylabel ( ' f ( x ) ' ) ;
i =1;
fprintf ( ' \ tit \ tx_0 \ t \ t \ tx_1 \ t \ t \ t \ terror \ n ' ) ;
while abs ( fun ( x0 ) ) >= tol
x1 = x0 -( fun ( x0 ) ) /( der ( x0 ) ) ;
fprintf ( ' \ t %i \ t %3.5 f \ t \ t %f \ t \ t %3.5 f \ n ' , i , x0 , x1
, abs ( fun ( x0 ) ) ) ;
x0 = x1 ;
i = i +1;
end
x1 = x0 -( fun ( x0 ) ) /( der ( x0 ) ) ;
fprintf ( ' \ t %i \ t %3.5 f \ t \ t %f \ t \ t %3.5 f \ n ' , i , x0 , x1 ,
abs ( fun ( x0 ) ) ) ;
fprintf ( ' La raiz es %3.5 f \ n ' , x0 )
figure ;
ezplot ( fun )
grid on
xlabel ( ' x ' ) ;
ylabel ( ' f ( x ) ' ) ;
hold on
plot ( x0 , fun ( x0 ) , ' * ' )
Corrida
18
Fig.16 Gráfica de la función
19
Fig.18 Gráfica de la función y la raı́z
Ejercicio 6
Utilizando el método de la bisección para la solución aproximada de raı́ces,
hallar la solución aproximada para la ecuación
1
− 2x = 0
2
en el intervalo [0.5, 1] con una exactitud de 10−2 . Realizar los cálculos
con 4 decimales significativos correctos.
Código
clc
clear all
close all
funcion = input ( ' Ingrese f ( x ) : ' , ' s ' ) ;
f = inline ( funcion ) ;
a = input ( ' Ingrese el limite inferior : ' ) ;
20
b = input ( ' Ingrese el limite superior : ' ) ;
limInf = a ;
limSup = b ;
tol = input ( ' Ingrese el error ( tolerancia ) : ' ) ;
i =0;
ezplot ( f ) ;
figure
absoluto =100;
fprintf ( ' \ t \ t \ t \ t \ t \ t \ t \ t \ tTABLA DE VALORES Y
ERRORES \ n ' )
fprintf ( ' \ t \ t \ ti \ t \ t \ t a \ t \ t \ t \ t \ t \ tb \ t \ t \ t \ t \ tc
\ t \ t \ t \ tError (b - a ) \ t \ t \ n ' ) ;
while ( f ( a ) * f ( b ) >0)
fprintf ( ' No hay raiz en ese intervalo \ n ' ) ;
a = input ( ' Ingrese el limite inferior : ' ) ;
b = input ( ' Ingrese el limite superior : ' ) ;
limSup = a ;
limInf = b ;
end
while abs (b - a ) > tol
c =( a + b ) /2;
fprintf ( ' \ t \ t \ t %d \ t \ t %10.4 f \ t \ t %10.4 f \ t
\ t %10.4 f \ t \ t %10.4 f \ t \ t \ n ' ,i +1 ,a ,b ,c ,
abs (b - a ) )
if ( f ( a ) * f ( c ) <=0)
b=c;
else
a=c;
end
i = i +1;
end
c =( a + b ) /2;
fprintf ( ' \ t \ t \ t %d \ t \ t %10.4 f \ t \ t %10.4 f \ t \ t %10.4
f \ t \ t %10.4 f \ t \ t \ n ' ,i +1 ,a ,b ,c , abs (b - a ) )
X = sprintf ( ' La raiz es : %10.4 f ' ,c ) ;
disp ( X )
ezplot ( f ) ;
hold on
grid on
plot (c , f ( c ) , ' * ' )
21
Corrida
22
Fig.21 Gráfica de la función y su raı́z
Ejercicio 7
Aplicar el método de Newton-Raphson para calcular la raı́z de la ecuación
Código
clc
clear all
syms x
funcion = input ( ' Ingrese la funcion f ( x ) : ' ) ;
f = inline ( funcion ) ;
fp = diff ( funcion ) ;
fprima = inline ( fp ) ;
23
x0 = input ( ' Ingrese el punto de partida : ' ) ;
tol = input ( ' Ingrese la tolerancia : ' ) ;
fprintf ( ' \ t \ t \ t \ t \ t \ t \ tTABLA DE VALORES Y ERRORES \ n '
)
fprintf ( ' \ t \ t \ tit \ t \ t \ t X0 \ t \ t \ t \ t X1 \ t \ t \ t \
tError \ t \ t \ n ' ) ;
i =0;
error =50;
while ( error > tol )
if ( f ( x0 ) ==0)
fprintf ( ' La raiz es : %10.5 f ' , x0 ) ;
break
else
x1 = x0 -( f ( x0 ) / fprima ( x0 ) ) ;
error = abs ( x1 - x0 ) ;
fprintf ( ' \ t \ t \ t %d \ t \ t %10.5 f \ t \ t %10.5 f \ t
\ t %10.5 f \ t \ t \ n ' ,i +1 , x0 , x1 , abs ( x1 - x0 ) )
x0 = x1 ;
i = i +1;
end
end
x1 = x0 -( f ( x0 ) / fprima ( x0 ) ) ;
fprintf ( ' \ t \ t \ t %d \ t \ t %10.5 f \ t \ t %10.5 f \ t \ t %10.5
f \ t \ t \ n ' ,i +1 , x0 , x1 , abs ( f ( x0 ) ) )
fprintf (1 , ' La raiz aproximada es %10.5 f \ n ' , x1 ) ;
ezplot ( f )
hold on
grid on
plot ( x1 , f ( x1 ) , ' * ' )
Corrida
24
Fig 22. Corrida del programa
Ejercicio 8
Resuelva la siguiente ecuación:
x log(x) − 10 = 0
por el método de la secante.
Como el ejercicio no da un intervalo establecido, nosotros consideramos
el intervalo [8,15] con una tolerancia de 10−4 .
Código
close all
clc
clear all
funcion = input ( ' Ingrese la funcion f ( x ) : ' , ' s ' ) ;
25
f = inline ( funcion ) ;
x0 = input ( ' Ingrese el primer punto : ' ) ;
x1 = input ( ' Ingrese el segundo punto : ' ) ;
tol = input ( ' Ingrese la tolerancia : ' ) ;
it =0;
ezplot ( f ) ;
26
Fig 25. Gráfica de la función
27