Evidencia 1 - Métodos Numéricos
Alumno: Kristhyan Andree Kurt Lazarte Zubia
A través de la presente evaluación se realizó un trabajo de investigación para dominar la
herramienta Jupyter y también poder generar tablas que sean visualmente atractivas y fáciles
de entender
Ejercicio 1- Bisección y falsa posición
Siendo la ecuación:
Procederemos a despejar la función y graficar con los valores g=9.81, c=15, v=35.7, t=10
Despejando m y usando el intervalo
𝑉 = (1 − )
𝑔𝑚
𝑐 𝑒−(𝑐/𝑚)𝑡
[59, 60]
In [1]: #Importamos las librerias necesarias
from math import *
import matplotlib.pyplot as plt
import numpy as np
import sympy
x, y = sympy.symbols('x y')
sympy init_printing(use_unicode=True)
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
1 of 18 9/12/21, 03:13
In [2]:
In [3]:
Declaramos la función a evaluar y el algoritmo de la bisección
In [4]:
x1=np.linspace(1,70,500)
y1=((9.81*x1/15)*(1-np.exp((15/x1)*-10)))-35.7
#y1=np.arccos(x1)
#8=(2²*arccos((2-x)/2)-(2-x)*sqrt(2*2*x-x²))*5
plt.plot(x1,y1,'-', color="blue", label='$f(x)$')
plt.grid(True)
plt.xlabel('x'); plt.ylabel('y')
plt.legend(loc='best')
plt show()
x1=np.linspace(58,60,500)
y1=((9.81*x1/15)*(1-np.exp((15/x1)*-10)))-35.7
#y1=np.arccos(x1)
#8=(2²*arccos((2-x)/2)-(2-x)*sqrt(2*2*x-x²))*5
plt.plot(x1,y1,'-', color="blue", label='$f(x)$')
plt.grid(True)
plt.xlabel('x'); plt.ylabel('y')
plt.legend(loc='best')
plt show()
def fx(x):
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
2 of 18 9/12/21, 03:13
In [5]:
In [6]:
In [7]:
iter a b xk f(xk) ERA
1 59 60 59.5 0.08523 0.84034
2 59 59.5 59.25 -0.03217 0.42194
3 59.25 59.5 59.375 0.02657 0.21053
4 59.25 59.375 59.3125 -0.00279 0.10537
5 59.3125 59.375 59.34375 0.0119 0.05266
6 59.3125 59.34375 59.328125 0.00455 0.02634
7 59.3125 59.328125 59.320312 0.00088 0.01317
8 59.3125 59.320312 59.316406 -0.00095 0.00659
9 59.316406 59.320312 59.318359 -3e-05 0.00329
10 59.318359 59.320312 59.319336 0.00042 0.00165
11 59.318359 59.319336 59.318848 0.00019 0.00082
Out[6]: (59.31884765625,  0.000194784909652412)
return ((9.81*x/15)*(1-exp((15/x)*-10)))-35.7
#%%
# Programa Bisección
# a: limite inferior del intervalo
# b: limite superior del intervalo
# ck: punto medio #xk
# fx: función a obtener la raiz
# tol: tolerancia
# ea: error aproximado
# ERA: error relativo aproximado (en %)
# nk: número de iteraciones
def bisec(a,b,fx,tol,i):
m_old=b
print("iter","a"," ","b"," ", "xk"," ", "f(xk)"," ", "ERA" )
for i in range(1,i):
m = (a+b)/2
ERA = 100*abs(m - m_old)/abs(m) # error relativo aproximado
print(i,round(a,6)," ",round(b,6)," ",round(m,6)," ", round(fx(
if(fx(a)*fx(m)<0):
b=m
else: a=m
m_old =m
if(ERA<=tol):
break
# xr=(a+b)/2 # raiz aproximada
aux = fx(m) # función evaluada en la raíz
return(m,aux)
bisec(59 60 fx 0.001 80)
from IPython.display import Markdown as md
strOut = ""
esp = " | "
def bisec2(a,b,fx,tol,i):
global strOut
strOut = """
| Iter | a | b | xk | f(xk) | E_{RA} |
| --- | --- | --- | --- | --- | --- |
"""
m_old=b
#print("iter","a"," ","b"," ", "xk"," ", "f(xk)"," ", "ERA" )
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
3 of 18 9/12/21, 03:13
In [8]:
Por lo tanto la respuesta es: 59.3188476562
Falsa posición
Ya que hemos definido la función con anterioridad, procederemos a declarar y ejecutar el
algoritmo
In [9]:
Out[8]: Iter a b xk f(xk) E_{RA}
1 59 60 59.5 0.08523 0.84034
2 59 59.5 59.25 -0.03217 0.42194
3 59.25 59.5 59.375 0.02657 0.21053
4 59.25 59.375 59.3125 -0.00279 0.10537
5 59.3125 59.375 59.34375 0.0119 0.05266
6 59.3125 59.34375 59.328125 0.00455 0.02634
7 59.3125 59.328125 59.3203125 0.00088 0.01317
8 59.3125 59.320312 59.31640625 -0.00095 0.00659
9 59.316406 59.320312 59.318359375 -3e-05 0.00329
10 59.318359 59.320312 59.3193359375 0.00042 0.00165
11 59.318359 59.319336 59.3188476562 0.00019 0.00082
for j in range(1,i+1):
m = (a+b)/2
ERA = 100*abs(m - m_old)/abs(m) # error relativo aproximado
strOut+=str(j)+esp+str(round(a,6))+esp+str(round(b,6))+esp+str(
if(fx(a)*fx(m)<0):
b=m
else: a=m
m_old =m
if(ERA<=tol):
break
return md(strOut)
bisec2(59 60 fx 0.001 80)
from IPython.display import Markdown as md
strOut = ""
esp = " | "
def falsa_posi2(a,b,fx,tol,i):
global strOut
strOut = """
| Iter | a | b | xi | f(ck) | E_{RA} |
| --- | --- | --- | --- | --- | --- |
"""
x0 = b
for j in range(0,i):
xi = (a*fx(b) - b*fx(a))/(fx(b) - fx(a))
ERA = 100*abs(xi - x0)/abs(xi) # error relativo aproximado
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
4 of 18 9/12/21, 03:13
In [10]:
Por lo tanto la respuesta es: 59.318433
Ejercicio 2 - Falsa Posicion y Newton
Raphson
Siendo la ecuación:
Procederemos a despejar la funcion y graficar con los valores r=2, L=5 y V=0.8 con
ERA=0.001%
Despejando h
𝑉 = [  𝑟² 𝑐𝑜   − (𝑟 − ℎ)   ]𝐿
𝑠−1 𝑟−ℎ
𝑟 2𝑟ℎ − ℎ²
⎯ ⎯
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
√
In [11]:
Out[10]: Iter a b xi f(ck) E_{RA}
1 59 60 59.319733 0.0006109296 1.14678
2 59 59.319733 59.318436 1.1627e-06 0.00219
3 59 59.318436 59.318433 2.2e-09 0.0
strOut+=str(j+1)+esp+str(round(a,10))+esp+str(round(b,6))+esp+str
if(fx(a)*fx(xi)<0):
b=xi
else: a=xi
x0=xi
if(ERA<tol):
break
return md(strOut)
falsa_posi2(59 60 fx 0.001 80)
#Importamos las librerias necesarias
from math import *
import matplotlib.pyplot as plt
import numpy as np
import sympy
x, y = sympy.symbols('x y')
sympy init_printing(use_unicode=True)
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
5 of 18 9/12/21, 03:13
In [12]:
Graficamos la funcion con un intervalo más pequeño [0.7, 0.8]
In [13]:
x1=np.linspace(0,4,500)
y1=(2**2*np.arccos((2-x1)/2.0)-(2-x1)*np.sqrt(2*2*x1-x1**2))*5-8
#y1=np.arccos(x1)
#8=(2²*arccos((2-x)/2)-(2-x)*sqrt(2*2*x-x²))*5
plt.plot(x1,y1,'-', color="blue", label='$f(x)$')
plt.grid(True)
plt.xlabel('x'); plt.ylabel('y')
plt.legend(loc='best')
plt show()
#Graficamos la funcion
x1=np.linspace(0.7,0.8,500)
y1=(2**2*np.arccos((2-x1)/2.0)-(2-x1)*np.sqrt(2*2*x1-x1**2))*5-8
#y1=np.arccos(x1)
#8=(2²*arccos((2-x)/2)-(2-x)*sqrt(2*2*x-x²))*5
plt.plot(x1,y1,'-', color="blue", label='$f(x)$')
plt.grid(True)
plt.xlabel('x'); plt.ylabel('y')
plt.legend(loc='best')
plt.show()
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
6 of 18 9/12/21, 03:13
Procedemos a declarar la ecuación y el valor de ERA
In [14]:
Declaramos el algoritmo de falsa posicion
In [15]:
Ejecutaremos el algoritmo con el intervalo [0.73, 0.75]
In [16]:
Generación de tabla
En la siguiente celda procederemos a generar una tabla correctamente identada para poder
visualizar de mejor manera los datos.
In [17]:
iter a b xi f(ck) E_RA
1 0.73 0.75 0.739989 0.0004056 1.35284
2 0.739989 0.75 0.740015 1.1e-06 0.00352
3 0.740015 0.75 0.740015 0.0 1e-05
Out[16]: (0.740015217880838,   − 2.71970534981847 ⋅ )
10−9
strFx="(2**2*acos((2-x)/2.0)-(2-x)*sqrt(2*2*x-x**2))*5-8"
exec("def fx(x):ntreturn {}".format(strFx))
ERA=0.001
def falsa_posi(a,b,fx,tol,i):
print("iter","a"," ","b"," ", "xi"," ","f(ck)"," ", "E_RA")
x0 = b
for j in range(0,i):
xi = (a*fx(b) - b*fx(a))/(fx(b) - fx(a))
ERA = 100*abs(xi - x0)/abs(xi) # error relativo aproximado
print(j+1,round(a,6)," ",round(b,6)," ",round(xi,6)," ",
abs(round(fx(xi),7))," ",round(ERA,5))
if(fx(a)*fx(xi)<0):
b=xi
else: a=xi
x0=xi
if(ERA<tol):
break
return(xi fx(xi))
falsa_posi(0.73,0.75,fx,ERA,20)
from IPython.display import Markdown as md
strOut = ""
esp = " | "
def falsa_posi2(a,b,fx,tol,i):
global strOut
strOut = """
| Iter | a | b | xi | f(ck) | E_{RA} |
| --- | --- | --- | --- | --- | --- |
"""
x0 = b
for j in range(0,i):
xi = (a*fx(b) - b*fx(a))/(fx(b) - fx(a))
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
7 of 18 9/12/21, 03:13
In [18]:
Por lo tanto el resultado es 0.7400151504
Newton Raphson
Ya que en el punto anterior hemos definido la función y el valor de E_{RA} solo agregaré el
algoritmo correspondiente y lo ejecutaremos.
Primeramente, encontraremos la derivada de la función.
In [19]:
In [20]:
Agregamos el código del algoritmo y mostraremos la tabla que genera
In [21]:
Out[18]: Iter a b xi f(ck) E_{RA}
1 0.73 0.75 0.739989 0.0004056252 1.35284
2 0.7399891025 0.75 0.740015 1.0503e-06 0.00352
3 0.7400151504 0.75 0.740015 2.7e-09 1e-05
La derivada de (2**2*acos((2-x)/2.0)-(2-x)*sqrt(2*2*x-x**2))*5-8 es:
Out[19]:
− + 5 +
5(2 − 𝑥)2
− + 4𝑥
𝑥2
⎯ ⎯
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
√
− + 4𝑥
𝑥2
⎯ ⎯
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
√
10.0
1 − (1.0 − 0.5𝑥)2
⎯ ⎯
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
√
ERA = 100*abs(xi - x0)/abs(xi) # error relativo aproximado
strOut+=str(j+1)+esp+str(round(a,10))+esp+str(round(b,6))+esp+str
if(fx(a)*fx(xi)<0):
b=xi
else: a=xi
x0=xi
if(ERA<tol):
break
return md(strOut)
falsa_posi2(0.73 0.75 fx ERA 20)
import sympy
x, y = sympy.symbols('x y')
sympy.init_printing(use_unicode=True)
print("La derivada de", strFx, "es:")
sympy diff(strFx x)
strDfx=str(sympy.diff(strFx,x))
exec("def dfx(x):ntreturn {}".format(strDfx))
from IPython.display import Markdown as md
strOut = ""
esp = " | "
def newtonr2(x0,Nmax,tol,fx,dfx):
global strOut
strOut = """
| Iter | xk | fx(xk) | EA | ERA |
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
8 of 18 9/12/21, 03:13
In [22]:
Por lo tanto El resultado es: 0.7400152181
Ejercicio 3 - Newton Raphson y secante
Para encontrar la raiz positiva primero usaremos el método Newton-Raphson con
con una tolerancia de
Acto seguido, con el método de la secante con los valores iniciales y con
Newton-Raphson
En primer lugar graficaremos la función
𝑓(𝑥) = 𝑠𝑖𝑛(𝑥) − 𝑐𝑜𝑠(1 + 𝑥²) − 1
= 0.5
𝑥0
𝐸𝑅𝐴 < 0.001
= 0.5
𝑥𝑖−1 = 0.7
𝑥𝑖
𝐸𝑅𝐴 < 0.001
In [23]:
In [24]:
Out[22]: Iter xk fx(xk) EA ERA
1 0.7404579036 0.0068766 0.0404579 5.46390327
2 0.7400152692 7.9e-07 0.00044263 0.05981423
3 0.7400152181 0.0 5e-08 6.91e-06
| --- | --- | --- | --- | --- |
"""
ERA = 100
iter = 0
#print("iter", " ", "xk", " ", "fx(xk)", " ", "EA", " ", "ERA" )
while(ERA>tol):
xk = x0 - ( fx(x0)/dfx(x0) )
iter += 1
EA = abs(xk-x0)
ERA= 100*(EA/abs(xk))
x0 = xk
strOut+=str(iter)+esp+str(round(xk,10))+esp+str(round(fx(xk),8))
if(iter > Nmax):
break
return md(strOut)
newtonr2(0.70, 20, ERA, fx, dfx)
#Importamos las librerias necesarias
from math import *
import matplotlib.pyplot as plt
import numpy as np
import sympy
x, y = sympy.symbols('x y')
sympy.init_printing(use_unicode=True)
#Graficamos la funcion
x1=np.linspace(0.8,2,500)
y1 = np.sin(x1)-np.cos(1+x1**2)-1
plt.plot(x1,y1,'-', color="blue", label='$f(x)$')
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
9 of 18 9/12/21, 03:13
Procederemos a declarar la función y obtener la derivada de la misma
In [25]:
Declaramos la derivada de la función y los valores para el algoritmo
In [26]:
Agregamos el código del algoritmo y procedemos a ejecutarlo
In [27]:
Derivada de sin(x)-cos(1+x**2)-1:
Out[25]: 2𝑥 sin ( + 1) + cos (𝑥)
𝑥2
plt.grid(True)
plt.xlabel('x'); plt.ylabel('y')
plt.legend(loc='best')
plt.show()
strFx = "sin(x)-cos(1+x**2)-1"
exec("def fx(x):treturn {}".format(strFx))
print("Derivada de", strFx+":")
sympy diff(strFx x)
def dfx(x):
return 2*x*sin(x**2+1)+cos(x)
x0=0.5
ERA=0.001
# metodo de N-R
# x0: raiz inicial (valor inicial)
# Nmax: número de iteraciones máxima
# tol: tolerancia (ERA)
# fx: función a encontrar la raíz
# dfx: derivada de la función fx
# NOTA: la función evaluada en la raíz aproximada, fx(xk),
# es mostrada en valor absoluto
def newtonr(x0,Nmax,tol,fx,dfx):
ERA = 100
iter = 0
print("iter", " ", "xk", " ", "fx(xk)", " ", "EA", " ", "ERA" )
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
10 of 18 9/12/21, 03:13
In [28]:
Generación de tabla
En la siguiente celda procederemos a generar una tabla correctamente identada para poder
visualizar de mejor manera los datos.
In [29]:
In [30]:
iter xk fx(xk) EA ERA
1 0.9576327 0.1572175 0.45763267 47.78791352
2 0.8914928 0.000106 0.06613985 7.41899927
3 0.891448 0.0 4.479e-05 0.00502408
4 0.891448 0.0 0.0 2e-08
Out[28]: (0.891448040560734,  2.22044604925031 ⋅ )
10−16
Out[30]: Iter xk fx(xk) EA ERA
while(ERA>tol):
xk = x0 - ( fx(x0)/dfx(x0) )
iter += 1
EA = abs(xk-x0)
ERA= 100*(EA/abs(xk))
x0 = xk
print( iter," ",round(xk,7)," ",round(fx(xk),8)," ", round(EA,8
" ", round(ERA,8) )
if(iter > Nmax):
break
aux = abs(fx(xk))
return(xk,aux)
newtonr(x0, 20, ERA, fx, dfx)
from IPython.display import Markdown as md
strOut = ""
esp = " | "
def newtonr2(x0,Nmax,tol,fx,dfx):
global strOut
strOut = """
| Iter | xk | fx(xk) | EA | ERA |
| --- | --- | --- | --- | --- |
"""
ERA = 100
iter = 0
#print("iter", " ", "xk", " ", "fx(xk)", " ", "EA", " ", "ERA" )
while(ERA>tol):
xk = x0 - ( fx(x0)/dfx(x0) )
iter += 1
EA = abs(xk-x0)
ERA= 100*(EA/abs(xk))
x0 = xk
strOut+=str(iter)+esp+str(round(xk,10))+esp+str(round(fx(xk),8))
if(iter > Nmax):
break
return md(strOut)
newtonr2(x0 10 ERA fx dfx)
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
11 of 18 9/12/21, 03:13
Por lo tanto la respuesta es: 0.8914480406
Secante
Ya que hemos graficado y declarado la función; procederemos a implementar el algoritmo y
ejecutarlo.
In [31]:
In [32]:
Generación de tabla
En la siguiente celda procederemos a generar la tabla, tal como en el algoritmo anterior
Iter xk fx(xk) EA ERA
1 0.9576326742 0.1572175 0.45763267 47.78791352
2 0.8914928279 0.000106 0.06613985 7.41899927
3 0.8914480408 0.0 4.479e-05 0.00502408
iter xk fx(xk) EA ERA
1 0.9185699 0.064331307 0.21856992 23.79458736
2 0.8904943 -0.002256978 0.02807562 3.15281269
3 0.8914459 -5.03e-06 0.00095161 0.10674902
4 0.891448 0.0 2.13e-06 0.00023845
Out[32]: (0.891448040761649,  4.75502304198017 ⋅ )
10−10
#%%
# metodo de la secante
# x0=x_(0)
# x1=x_(1)
# tol: tolerancia en porcentaje
# Nmax: número de iteraciones
# fx: función f(x) a encontrar la raíz
def secante(x0,x1,Nmax,tol,fx):
ERA = 100
iter = 1
print("iter", " ", "xk", " ", "fx(xk)", " ", "EA", " ", "ERA" )
while(ERA>tol):
# xk: genera la raiz
xk = x1 - (fx(x1)*(x1 - x0))/(fx(x1)-fx(x0))
EA = abs(xk-x1)
ERA = abs((xk-x1)/xk)*100
x0 = x1
x1 = xk
if(iter > Nmax):
break
print(iter," ",round(xk,7)," ",round(fx(xk),9)," ",round(EA,8),
iter = iter + 1
aux = abs(fx(xk))
return(xk,aux)
secante(0.5 0.7 20 ERA fx)
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
12 of 18 9/12/21, 03:13
In [33]:
In [34]:
Por lo tanto la respuesta es: 0.8914480408
Ejercicio 4 - Punto Fijo
Para comprobar la continuidad de la funcion en el intervalo procederemos,
primeramente, a graficarla. Acto seguido, propondremos 2 o 3 ecuaciones g(x) y probaremos
su convergencia.
El primer paso para encontrar la convengencia es encontrar la derivada de cada
𝑓(𝑥) = 2 ∗ 𝑠𝑒𝑛(𝑝𝑖 ∗ 𝑥) + 𝑥
[𝑎, 𝑏]
(𝑥) = 2 ∗ 𝑠𝑒𝑛(𝑝𝑖 ∗ 𝑥) + 2𝑥
𝑔1
(𝑥) = −2 ∗ 𝑠𝑒𝑛(𝑝𝑖 ∗ 𝑥)
𝑔2
(𝑥) = 𝑝𝑖 ∗ 𝑎𝑟𝑐𝑠𝑒𝑛(−𝑥/2)
𝑔3
𝑔(𝑥)
Out[34]: Iter xk fx(xk) EA ERA
1 0.9185699228 0.064331307 0.21856992 23.79458736
2 0.8904943053 -0.002256978 0.02807562 3.15281269
3 0.8914459151 -5.03e-06 0.00095161 0.10674902
4 0.8914480408 0.0 2.13e-06 0.00023845
from IPython.display import Markdown as md
esp = " | "
def secante2(x0,x1,Nmax,tol,fx):
strOut = """
| Iter | xk | fx(xk) | EA | ERA |
| --- | --- | --- | --- | --- |
"""
ERA = 100
iter = 1
#print("iter", " ", "xk", " ", "fx(xk)", " ", "EA", " ", "ERA" )
while(ERA>tol):
# xk: genera la raiz
xk = x1 - (fx(x1)*(x1 - x0))/(fx(x1)-fx(x0))
EA = abs(xk-x1)
ERA = abs((xk-x1)/xk)*100
x0 = x1
x1 = xk
strOut+=str(iter)+esp+str(round(xk,10))+esp+str(round(fx(xk),9))
if(iter > Nmax):
break
iter = iter + 1
return md(strOut)
secante2(0.5, 0.7, 20, ERA, fx)
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
13 of 18 9/12/21, 03:13
In [35]:
In [36]:
Tal como se ve en el gráfico, la función es continua en el intervalo [1, 2]
In [37]:
In [38]:
Derivada de 2 * sin(pi*x) + 2*x:
Out[38]: 2𝜋 cos (𝜋𝑥) + 2
#Importamos las librerias necesarias
from math import *
import matplotlib.pyplot as plt
import numpy as np
import sympy
#Graficamos la funcion
x1=np.linspace(0.8,2.2,500)
y1 = 2*np.sin(pi*x1)+x1
plt.plot(x1,y1,'-', color="blue", label='$f(x)$')
plt.grid(True)
plt.xlabel('x'); plt.ylabel('y')
plt.legend(loc='best')
plt show()
#Declaramos la funcion, los posibles g(x), el punto medio del intervalo [1,2]
m = (1+2)/2
x0 = 1
ERA = 0.01
x, y = sympy.symbols('x y')
sympy.init_printing(use_unicode=True)
strFx = "2*sin(pi*x)+x"
strGx1 = "2 * sin(pi*x) + 2*x"
strGx2 = "-2 * sin(pi*x)"
strGx3 = "pi * asin(-x/2)"
print("Derivada de", strGx1+":")
sympy diff(strGx1 x)
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
14 of 18 9/12/21, 03:13
In [39]:
In [40]:
Evaluamos los posibles si son convergentes
Para con x=1.5
Vemos que no es convergente
(𝑥)
𝑔′
2𝜋 𝑐𝑜𝑠(𝜋𝑥) + 2
In [41]:
Para con x=1.5
Vemos que es 1.15e-15 y es convergente
−2𝜋𝑐𝑜𝑠(𝜋𝑥)
In [42]:
Para con x=1.5
Vemos que no es convergente
−𝜋/(2 )
1 − 𝑥²/4
⎯ ⎯
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
√
In [43]:
In [44]:
Derivada de -2 * sin(pi*x):
Out[39]: −2𝜋 cos (𝜋𝑥)
Derivada de pi * asin(-x/2):
Out[40]: −
𝜋
2 1 − 𝑥2
4
⎯ ⎯
⎯⎯⎯⎯⎯⎯⎯⎯⎯
√
1.999999999999999
1.1542024162330739e-15
-2.3748208234474517
print("Derivada de", strGx2+":")
sympy diff(strGx2 x)
print("Derivada de", strGx3+":")
sympy.diff(strGx3, x)
print(2*pi*cos(pi*m)+2)
print(-2*pi*cos(pi*m))
print(-pi/(2*(1-m**2/4)**0.5))
#Declaramos como funcion (def) a nuestra funcion fx y nuestro g(x) que es conv
exec("def fx(x):treturn {}".format(strFx))
def gx(x):
return -2*pi*cos(pi*x)
def puntofijo(fx,x0,tol,i):
print("iter","x0"," ","xk"," ","ERA", " ", "f(xk)" )
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
15 of 18 9/12/21, 03:13
In [45]:
Generación de tabla
En la siguiente celda procederemos a generar una tabla correctamente identada para poder
visualizar de mejor manera los datos. Se realizarán 150 iteraciones e imprimiremos cada 10.
In [46]:
iter x0 xk ERA f(xk)
1 1 6.283185 84.084506 7.83689
2 6.283185 -3.956407 258.810389 3.68336
3 -3.956407 -6.224354 36.43667 7.52016
4 -6.224354 -4.786037 30.052368 6.03143
5 -4.786037 4.916362 197.349164 5.43585
6 4.916362 6.06753 18.972605 6.48866
7 6.06753 -6.142316 198.782452 7.00702
8 -6.142316 -5.665582 8.414562 3.93013
9 -5.665582 -3.123032 81.412892 2.36911
10 -3.123032 5.819664 153.663433 4.74623
11 5.819664 -5.301514 209.77362 3.67791
12 -5.301514 3.668939 244.497215 1.94407
13 3.668939 -3.180353 215.362614 2.10683
14 -3.180353 5.301328 159.991634 3.6784
15 5.301328 3.671922 44.374729 1.95662
16 3.671922 -3.231006 213.646402 1.90365
17 -3.231006 4.699927 168.745887 6.31823
18 4.699927 3.691994 27.300497 2.04491
19 3.691994 -3.564159 203.586676 1.60465
20 -3.564159 -1.257894 183.343409 0.19095
21 -1.257894 4.331349 129.041621 6.05713
22 4.331349 -3.175461 236.400612 2.12799
23 -3.175461 5.352534 159.326317 3.56335
24 5.352534 2.807852 90.627349 3.94316
25 2.807852 5.17275 45.718391 4.13983
26 5.17275 5.380367 3.858786 3.51997
27 5.380367 2.306258 133.294212 3.94709
28 2.306258 -3.592516 164.196179 1.6764
29 -3.592516 -1.800596 99.518184 0.62806
30 -1.800596 -5.090109 64.625593 4.53147
Out[45]: (−5.09010926172804,   − 4.531467829699)
for i in range(1,i+1):
xk = gx(x0)
ERA = 100*abs(xk-x0)/abs(xk) # xk diferente de cero
print(i, round(x0,6), " ",round(xk,6), " ", round(ERA,6)," ", round
x0=xk # actualizando
if(ERA<tol):
break
return(xk,fx(xk))
#Ejecutamos el algoritmo de punto fijo
puntofijo(fx,x0,ERA, 30)
from IPython.display import Markdown as md
strOut = ""
esp = " | "
def puntofijo2(fx,x0,tol,i):
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
16 of 18 9/12/21, 03:13
In [47]:
Tal como podemos apreciar, a pesar de que hemos dado 150 iteraciones, no hemos podido dar
con un valor dentro de la tolerancia pero nuestro resultado es 2.070607 con y = 6.91915
Out[47]: Iter x0 xk ERA f(xk)
10 -3.123032 5.819664 153.663433 4.74623
20 -3.564159 -1.257894 183.343409 0.19095
30 -1.800596 -5.090109 64.625593 4.53147
40 4.407854 -1.793587 345.75641 0.58566
50 2.215363 -4.899103 145.219771 5.52249
60 -3.3618 2.643061 227.193434 4.44444
70 -4.480823 -0.378312 1084.425431 2.23393
80 -4.314898 -3.451291 25.022749 1.47466
90 -6.268147 -4.182514 49.865514 5.26747
100 -4.019495 -6.271405 35.907595 7.77745
110 -2.99806 6.283069 147.716494 7.83631
120 -6.281523 -3.981843 57.754149 3.86782
130 1.206561 5.006011 75.897764 4.96825
140 -6.183225 -5.270692 17.313331 3.7676
150 2.070607 -6.12924 133.782443 6.91915
--- --- --- --- ---
150 2.070607 -6.12924 133.782443 6.91915
global strOut
strOut = """
| Iter | x0 | xk | ERA | f(xk) |
| --- | --- | --- | --- | --- |
"""
#print("iter","x0"," ","xk"," ","ERA", " ", "f(xk)" )
for j in range(1,i+1):
xk = gx(x0)
ERA = 100*abs(xk-x0)/abs(xk) # xk diferente de cero
#print(i, round(x0,6), " ",round(xk,6), " ", round(ERA,6)," ", round(a
if(j%10==0):
strOut+=str(j)+esp+str(round(x0,6))+esp+str(round(xk,6))+esp
if(j==i):
strOut+="| --- | --- | --- | --- | --- |n"
strOut+=str(j)+esp+str(round(x0,6))+esp+str(round(xk,6))+esp
x0=xk # actualizando
if(ERA<tol):
break
return md(strOut)
puntofijo2(fx,x0,ERA, 150)
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
17 of 18 9/12/21, 03:13
KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa...
18 of 18 9/12/21, 03:13

Kristhyan kurtlazartezubia evidencia1-metodosnumericos

  • 1.
    Evidencia 1 -Métodos Numéricos Alumno: Kristhyan Andree Kurt Lazarte Zubia A través de la presente evaluación se realizó un trabajo de investigación para dominar la herramienta Jupyter y también poder generar tablas que sean visualmente atractivas y fáciles de entender Ejercicio 1- Bisección y falsa posición Siendo la ecuación: Procederemos a despejar la función y graficar con los valores g=9.81, c=15, v=35.7, t=10 Despejando m y usando el intervalo 𝑉 = (1 − ) 𝑔𝑚 𝑐 𝑒−(𝑐/𝑚)𝑡 [59, 60] In [1]: #Importamos las librerias necesarias from math import * import matplotlib.pyplot as plt import numpy as np import sympy x, y = sympy.symbols('x y') sympy init_printing(use_unicode=True) KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 1 of 18 9/12/21, 03:13
  • 2.
    In [2]: In [3]: Declaramos la funcióna evaluar y el algoritmo de la bisección In [4]: x1=np.linspace(1,70,500) y1=((9.81*x1/15)*(1-np.exp((15/x1)*-10)))-35.7 #y1=np.arccos(x1) #8=(2²*arccos((2-x)/2)-(2-x)*sqrt(2*2*x-x²))*5 plt.plot(x1,y1,'-', color="blue", label='$f(x)$') plt.grid(True) plt.xlabel('x'); plt.ylabel('y') plt.legend(loc='best') plt show() x1=np.linspace(58,60,500) y1=((9.81*x1/15)*(1-np.exp((15/x1)*-10)))-35.7 #y1=np.arccos(x1) #8=(2²*arccos((2-x)/2)-(2-x)*sqrt(2*2*x-x²))*5 plt.plot(x1,y1,'-', color="blue", label='$f(x)$') plt.grid(True) plt.xlabel('x'); plt.ylabel('y') plt.legend(loc='best') plt show() def fx(x): KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 2 of 18 9/12/21, 03:13
  • 3.
    In [5]: In [6]: In [7]: iter a bxk f(xk) ERA 1 59 60 59.5 0.08523 0.84034 2 59 59.5 59.25 -0.03217 0.42194 3 59.25 59.5 59.375 0.02657 0.21053 4 59.25 59.375 59.3125 -0.00279 0.10537 5 59.3125 59.375 59.34375 0.0119 0.05266 6 59.3125 59.34375 59.328125 0.00455 0.02634 7 59.3125 59.328125 59.320312 0.00088 0.01317 8 59.3125 59.320312 59.316406 -0.00095 0.00659 9 59.316406 59.320312 59.318359 -3e-05 0.00329 10 59.318359 59.320312 59.319336 0.00042 0.00165 11 59.318359 59.319336 59.318848 0.00019 0.00082 Out[6]: (59.31884765625,  0.000194784909652412) return ((9.81*x/15)*(1-exp((15/x)*-10)))-35.7 #%% # Programa Bisección # a: limite inferior del intervalo # b: limite superior del intervalo # ck: punto medio #xk # fx: función a obtener la raiz # tol: tolerancia # ea: error aproximado # ERA: error relativo aproximado (en %) # nk: número de iteraciones def bisec(a,b,fx,tol,i): m_old=b print("iter","a"," ","b"," ", "xk"," ", "f(xk)"," ", "ERA" ) for i in range(1,i): m = (a+b)/2 ERA = 100*abs(m - m_old)/abs(m) # error relativo aproximado print(i,round(a,6)," ",round(b,6)," ",round(m,6)," ", round(fx( if(fx(a)*fx(m)<0): b=m else: a=m m_old =m if(ERA<=tol): break # xr=(a+b)/2 # raiz aproximada aux = fx(m) # función evaluada en la raíz return(m,aux) bisec(59 60 fx 0.001 80) from IPython.display import Markdown as md strOut = "" esp = " | " def bisec2(a,b,fx,tol,i): global strOut strOut = """ | Iter | a | b | xk | f(xk) | E_{RA} | | --- | --- | --- | --- | --- | --- | """ m_old=b #print("iter","a"," ","b"," ", "xk"," ", "f(xk)"," ", "ERA" ) KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 3 of 18 9/12/21, 03:13
  • 4.
    In [8]: Por lo tantola respuesta es: 59.3188476562 Falsa posición Ya que hemos definido la función con anterioridad, procederemos a declarar y ejecutar el algoritmo In [9]: Out[8]: Iter a b xk f(xk) E_{RA} 1 59 60 59.5 0.08523 0.84034 2 59 59.5 59.25 -0.03217 0.42194 3 59.25 59.5 59.375 0.02657 0.21053 4 59.25 59.375 59.3125 -0.00279 0.10537 5 59.3125 59.375 59.34375 0.0119 0.05266 6 59.3125 59.34375 59.328125 0.00455 0.02634 7 59.3125 59.328125 59.3203125 0.00088 0.01317 8 59.3125 59.320312 59.31640625 -0.00095 0.00659 9 59.316406 59.320312 59.318359375 -3e-05 0.00329 10 59.318359 59.320312 59.3193359375 0.00042 0.00165 11 59.318359 59.319336 59.3188476562 0.00019 0.00082 for j in range(1,i+1): m = (a+b)/2 ERA = 100*abs(m - m_old)/abs(m) # error relativo aproximado strOut+=str(j)+esp+str(round(a,6))+esp+str(round(b,6))+esp+str( if(fx(a)*fx(m)<0): b=m else: a=m m_old =m if(ERA<=tol): break return md(strOut) bisec2(59 60 fx 0.001 80) from IPython.display import Markdown as md strOut = "" esp = " | " def falsa_posi2(a,b,fx,tol,i): global strOut strOut = """ | Iter | a | b | xi | f(ck) | E_{RA} | | --- | --- | --- | --- | --- | --- | """ x0 = b for j in range(0,i): xi = (a*fx(b) - b*fx(a))/(fx(b) - fx(a)) ERA = 100*abs(xi - x0)/abs(xi) # error relativo aproximado KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 4 of 18 9/12/21, 03:13
  • 5.
    In [10]: Por lo tantola respuesta es: 59.318433 Ejercicio 2 - Falsa Posicion y Newton Raphson Siendo la ecuación: Procederemos a despejar la funcion y graficar con los valores r=2, L=5 y V=0.8 con ERA=0.001% Despejando h 𝑉 = [  𝑟² 𝑐𝑜   − (𝑟 − ℎ)   ]𝐿 𝑠−1 𝑟−ℎ 𝑟 2𝑟ℎ − ℎ² ⎯ ⎯ ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ √ In [11]: Out[10]: Iter a b xi f(ck) E_{RA} 1 59 60 59.319733 0.0006109296 1.14678 2 59 59.319733 59.318436 1.1627e-06 0.00219 3 59 59.318436 59.318433 2.2e-09 0.0 strOut+=str(j+1)+esp+str(round(a,10))+esp+str(round(b,6))+esp+str if(fx(a)*fx(xi)<0): b=xi else: a=xi x0=xi if(ERA<tol): break return md(strOut) falsa_posi2(59 60 fx 0.001 80) #Importamos las librerias necesarias from math import * import matplotlib.pyplot as plt import numpy as np import sympy x, y = sympy.symbols('x y') sympy init_printing(use_unicode=True) KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 5 of 18 9/12/21, 03:13
  • 6.
    In [12]: Graficamos la funcioncon un intervalo más pequeño [0.7, 0.8] In [13]: x1=np.linspace(0,4,500) y1=(2**2*np.arccos((2-x1)/2.0)-(2-x1)*np.sqrt(2*2*x1-x1**2))*5-8 #y1=np.arccos(x1) #8=(2²*arccos((2-x)/2)-(2-x)*sqrt(2*2*x-x²))*5 plt.plot(x1,y1,'-', color="blue", label='$f(x)$') plt.grid(True) plt.xlabel('x'); plt.ylabel('y') plt.legend(loc='best') plt show() #Graficamos la funcion x1=np.linspace(0.7,0.8,500) y1=(2**2*np.arccos((2-x1)/2.0)-(2-x1)*np.sqrt(2*2*x1-x1**2))*5-8 #y1=np.arccos(x1) #8=(2²*arccos((2-x)/2)-(2-x)*sqrt(2*2*x-x²))*5 plt.plot(x1,y1,'-', color="blue", label='$f(x)$') plt.grid(True) plt.xlabel('x'); plt.ylabel('y') plt.legend(loc='best') plt.show() KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 6 of 18 9/12/21, 03:13
  • 7.
    Procedemos a declararla ecuación y el valor de ERA In [14]: Declaramos el algoritmo de falsa posicion In [15]: Ejecutaremos el algoritmo con el intervalo [0.73, 0.75] In [16]: Generación de tabla En la siguiente celda procederemos a generar una tabla correctamente identada para poder visualizar de mejor manera los datos. In [17]: iter a b xi f(ck) E_RA 1 0.73 0.75 0.739989 0.0004056 1.35284 2 0.739989 0.75 0.740015 1.1e-06 0.00352 3 0.740015 0.75 0.740015 0.0 1e-05 Out[16]: (0.740015217880838,   − 2.71970534981847 ⋅ ) 10−9 strFx="(2**2*acos((2-x)/2.0)-(2-x)*sqrt(2*2*x-x**2))*5-8" exec("def fx(x):ntreturn {}".format(strFx)) ERA=0.001 def falsa_posi(a,b,fx,tol,i): print("iter","a"," ","b"," ", "xi"," ","f(ck)"," ", "E_RA") x0 = b for j in range(0,i): xi = (a*fx(b) - b*fx(a))/(fx(b) - fx(a)) ERA = 100*abs(xi - x0)/abs(xi) # error relativo aproximado print(j+1,round(a,6)," ",round(b,6)," ",round(xi,6)," ", abs(round(fx(xi),7))," ",round(ERA,5)) if(fx(a)*fx(xi)<0): b=xi else: a=xi x0=xi if(ERA<tol): break return(xi fx(xi)) falsa_posi(0.73,0.75,fx,ERA,20) from IPython.display import Markdown as md strOut = "" esp = " | " def falsa_posi2(a,b,fx,tol,i): global strOut strOut = """ | Iter | a | b | xi | f(ck) | E_{RA} | | --- | --- | --- | --- | --- | --- | """ x0 = b for j in range(0,i): xi = (a*fx(b) - b*fx(a))/(fx(b) - fx(a)) KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 7 of 18 9/12/21, 03:13
  • 8.
    In [18]: Por lo tantoel resultado es 0.7400151504 Newton Raphson Ya que en el punto anterior hemos definido la función y el valor de E_{RA} solo agregaré el algoritmo correspondiente y lo ejecutaremos. Primeramente, encontraremos la derivada de la función. In [19]: In [20]: Agregamos el código del algoritmo y mostraremos la tabla que genera In [21]: Out[18]: Iter a b xi f(ck) E_{RA} 1 0.73 0.75 0.739989 0.0004056252 1.35284 2 0.7399891025 0.75 0.740015 1.0503e-06 0.00352 3 0.7400151504 0.75 0.740015 2.7e-09 1e-05 La derivada de (2**2*acos((2-x)/2.0)-(2-x)*sqrt(2*2*x-x**2))*5-8 es: Out[19]: − + 5 + 5(2 − 𝑥)2 − + 4𝑥 𝑥2 ⎯ ⎯ ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ √ − + 4𝑥 𝑥2 ⎯ ⎯ ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ √ 10.0 1 − (1.0 − 0.5𝑥)2 ⎯ ⎯ ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ √ ERA = 100*abs(xi - x0)/abs(xi) # error relativo aproximado strOut+=str(j+1)+esp+str(round(a,10))+esp+str(round(b,6))+esp+str if(fx(a)*fx(xi)<0): b=xi else: a=xi x0=xi if(ERA<tol): break return md(strOut) falsa_posi2(0.73 0.75 fx ERA 20) import sympy x, y = sympy.symbols('x y') sympy.init_printing(use_unicode=True) print("La derivada de", strFx, "es:") sympy diff(strFx x) strDfx=str(sympy.diff(strFx,x)) exec("def dfx(x):ntreturn {}".format(strDfx)) from IPython.display import Markdown as md strOut = "" esp = " | " def newtonr2(x0,Nmax,tol,fx,dfx): global strOut strOut = """ | Iter | xk | fx(xk) | EA | ERA | KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 8 of 18 9/12/21, 03:13
  • 9.
    In [22]: Por lo tantoEl resultado es: 0.7400152181 Ejercicio 3 - Newton Raphson y secante Para encontrar la raiz positiva primero usaremos el método Newton-Raphson con con una tolerancia de Acto seguido, con el método de la secante con los valores iniciales y con Newton-Raphson En primer lugar graficaremos la función 𝑓(𝑥) = 𝑠𝑖𝑛(𝑥) − 𝑐𝑜𝑠(1 + 𝑥²) − 1 = 0.5 𝑥0 𝐸𝑅𝐴 < 0.001 = 0.5 𝑥𝑖−1 = 0.7 𝑥𝑖 𝐸𝑅𝐴 < 0.001 In [23]: In [24]: Out[22]: Iter xk fx(xk) EA ERA 1 0.7404579036 0.0068766 0.0404579 5.46390327 2 0.7400152692 7.9e-07 0.00044263 0.05981423 3 0.7400152181 0.0 5e-08 6.91e-06 | --- | --- | --- | --- | --- | """ ERA = 100 iter = 0 #print("iter", " ", "xk", " ", "fx(xk)", " ", "EA", " ", "ERA" ) while(ERA>tol): xk = x0 - ( fx(x0)/dfx(x0) ) iter += 1 EA = abs(xk-x0) ERA= 100*(EA/abs(xk)) x0 = xk strOut+=str(iter)+esp+str(round(xk,10))+esp+str(round(fx(xk),8)) if(iter > Nmax): break return md(strOut) newtonr2(0.70, 20, ERA, fx, dfx) #Importamos las librerias necesarias from math import * import matplotlib.pyplot as plt import numpy as np import sympy x, y = sympy.symbols('x y') sympy.init_printing(use_unicode=True) #Graficamos la funcion x1=np.linspace(0.8,2,500) y1 = np.sin(x1)-np.cos(1+x1**2)-1 plt.plot(x1,y1,'-', color="blue", label='$f(x)$') KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 9 of 18 9/12/21, 03:13
  • 10.
    Procederemos a declararla función y obtener la derivada de la misma In [25]: Declaramos la derivada de la función y los valores para el algoritmo In [26]: Agregamos el código del algoritmo y procedemos a ejecutarlo In [27]: Derivada de sin(x)-cos(1+x**2)-1: Out[25]: 2𝑥 sin ( + 1) + cos (𝑥) 𝑥2 plt.grid(True) plt.xlabel('x'); plt.ylabel('y') plt.legend(loc='best') plt.show() strFx = "sin(x)-cos(1+x**2)-1" exec("def fx(x):treturn {}".format(strFx)) print("Derivada de", strFx+":") sympy diff(strFx x) def dfx(x): return 2*x*sin(x**2+1)+cos(x) x0=0.5 ERA=0.001 # metodo de N-R # x0: raiz inicial (valor inicial) # Nmax: número de iteraciones máxima # tol: tolerancia (ERA) # fx: función a encontrar la raíz # dfx: derivada de la función fx # NOTA: la función evaluada en la raíz aproximada, fx(xk), # es mostrada en valor absoluto def newtonr(x0,Nmax,tol,fx,dfx): ERA = 100 iter = 0 print("iter", " ", "xk", " ", "fx(xk)", " ", "EA", " ", "ERA" ) KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 10 of 18 9/12/21, 03:13
  • 11.
    In [28]: Generación de tabla Enla siguiente celda procederemos a generar una tabla correctamente identada para poder visualizar de mejor manera los datos. In [29]: In [30]: iter xk fx(xk) EA ERA 1 0.9576327 0.1572175 0.45763267 47.78791352 2 0.8914928 0.000106 0.06613985 7.41899927 3 0.891448 0.0 4.479e-05 0.00502408 4 0.891448 0.0 0.0 2e-08 Out[28]: (0.891448040560734,  2.22044604925031 ⋅ ) 10−16 Out[30]: Iter xk fx(xk) EA ERA while(ERA>tol): xk = x0 - ( fx(x0)/dfx(x0) ) iter += 1 EA = abs(xk-x0) ERA= 100*(EA/abs(xk)) x0 = xk print( iter," ",round(xk,7)," ",round(fx(xk),8)," ", round(EA,8 " ", round(ERA,8) ) if(iter > Nmax): break aux = abs(fx(xk)) return(xk,aux) newtonr(x0, 20, ERA, fx, dfx) from IPython.display import Markdown as md strOut = "" esp = " | " def newtonr2(x0,Nmax,tol,fx,dfx): global strOut strOut = """ | Iter | xk | fx(xk) | EA | ERA | | --- | --- | --- | --- | --- | """ ERA = 100 iter = 0 #print("iter", " ", "xk", " ", "fx(xk)", " ", "EA", " ", "ERA" ) while(ERA>tol): xk = x0 - ( fx(x0)/dfx(x0) ) iter += 1 EA = abs(xk-x0) ERA= 100*(EA/abs(xk)) x0 = xk strOut+=str(iter)+esp+str(round(xk,10))+esp+str(round(fx(xk),8)) if(iter > Nmax): break return md(strOut) newtonr2(x0 10 ERA fx dfx) KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 11 of 18 9/12/21, 03:13
  • 12.
    Por lo tantola respuesta es: 0.8914480406 Secante Ya que hemos graficado y declarado la función; procederemos a implementar el algoritmo y ejecutarlo. In [31]: In [32]: Generación de tabla En la siguiente celda procederemos a generar la tabla, tal como en el algoritmo anterior Iter xk fx(xk) EA ERA 1 0.9576326742 0.1572175 0.45763267 47.78791352 2 0.8914928279 0.000106 0.06613985 7.41899927 3 0.8914480408 0.0 4.479e-05 0.00502408 iter xk fx(xk) EA ERA 1 0.9185699 0.064331307 0.21856992 23.79458736 2 0.8904943 -0.002256978 0.02807562 3.15281269 3 0.8914459 -5.03e-06 0.00095161 0.10674902 4 0.891448 0.0 2.13e-06 0.00023845 Out[32]: (0.891448040761649,  4.75502304198017 ⋅ ) 10−10 #%% # metodo de la secante # x0=x_(0) # x1=x_(1) # tol: tolerancia en porcentaje # Nmax: número de iteraciones # fx: función f(x) a encontrar la raíz def secante(x0,x1,Nmax,tol,fx): ERA = 100 iter = 1 print("iter", " ", "xk", " ", "fx(xk)", " ", "EA", " ", "ERA" ) while(ERA>tol): # xk: genera la raiz xk = x1 - (fx(x1)*(x1 - x0))/(fx(x1)-fx(x0)) EA = abs(xk-x1) ERA = abs((xk-x1)/xk)*100 x0 = x1 x1 = xk if(iter > Nmax): break print(iter," ",round(xk,7)," ",round(fx(xk),9)," ",round(EA,8), iter = iter + 1 aux = abs(fx(xk)) return(xk,aux) secante(0.5 0.7 20 ERA fx) KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 12 of 18 9/12/21, 03:13
  • 13.
    In [33]: In [34]: Por lo tantola respuesta es: 0.8914480408 Ejercicio 4 - Punto Fijo Para comprobar la continuidad de la funcion en el intervalo procederemos, primeramente, a graficarla. Acto seguido, propondremos 2 o 3 ecuaciones g(x) y probaremos su convergencia. El primer paso para encontrar la convengencia es encontrar la derivada de cada 𝑓(𝑥) = 2 ∗ 𝑠𝑒𝑛(𝑝𝑖 ∗ 𝑥) + 𝑥 [𝑎, 𝑏] (𝑥) = 2 ∗ 𝑠𝑒𝑛(𝑝𝑖 ∗ 𝑥) + 2𝑥 𝑔1 (𝑥) = −2 ∗ 𝑠𝑒𝑛(𝑝𝑖 ∗ 𝑥) 𝑔2 (𝑥) = 𝑝𝑖 ∗ 𝑎𝑟𝑐𝑠𝑒𝑛(−𝑥/2) 𝑔3 𝑔(𝑥) Out[34]: Iter xk fx(xk) EA ERA 1 0.9185699228 0.064331307 0.21856992 23.79458736 2 0.8904943053 -0.002256978 0.02807562 3.15281269 3 0.8914459151 -5.03e-06 0.00095161 0.10674902 4 0.8914480408 0.0 2.13e-06 0.00023845 from IPython.display import Markdown as md esp = " | " def secante2(x0,x1,Nmax,tol,fx): strOut = """ | Iter | xk | fx(xk) | EA | ERA | | --- | --- | --- | --- | --- | """ ERA = 100 iter = 1 #print("iter", " ", "xk", " ", "fx(xk)", " ", "EA", " ", "ERA" ) while(ERA>tol): # xk: genera la raiz xk = x1 - (fx(x1)*(x1 - x0))/(fx(x1)-fx(x0)) EA = abs(xk-x1) ERA = abs((xk-x1)/xk)*100 x0 = x1 x1 = xk strOut+=str(iter)+esp+str(round(xk,10))+esp+str(round(fx(xk),9)) if(iter > Nmax): break iter = iter + 1 return md(strOut) secante2(0.5, 0.7, 20, ERA, fx) KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 13 of 18 9/12/21, 03:13
  • 14.
    In [35]: In [36]: Tal como seve en el gráfico, la función es continua en el intervalo [1, 2] In [37]: In [38]: Derivada de 2 * sin(pi*x) + 2*x: Out[38]: 2𝜋 cos (𝜋𝑥) + 2 #Importamos las librerias necesarias from math import * import matplotlib.pyplot as plt import numpy as np import sympy #Graficamos la funcion x1=np.linspace(0.8,2.2,500) y1 = 2*np.sin(pi*x1)+x1 plt.plot(x1,y1,'-', color="blue", label='$f(x)$') plt.grid(True) plt.xlabel('x'); plt.ylabel('y') plt.legend(loc='best') plt show() #Declaramos la funcion, los posibles g(x), el punto medio del intervalo [1,2] m = (1+2)/2 x0 = 1 ERA = 0.01 x, y = sympy.symbols('x y') sympy.init_printing(use_unicode=True) strFx = "2*sin(pi*x)+x" strGx1 = "2 * sin(pi*x) + 2*x" strGx2 = "-2 * sin(pi*x)" strGx3 = "pi * asin(-x/2)" print("Derivada de", strGx1+":") sympy diff(strGx1 x) KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 14 of 18 9/12/21, 03:13
  • 15.
    In [39]: In [40]: Evaluamos los posiblessi son convergentes Para con x=1.5 Vemos que no es convergente (𝑥) 𝑔′ 2𝜋 𝑐𝑜𝑠(𝜋𝑥) + 2 In [41]: Para con x=1.5 Vemos que es 1.15e-15 y es convergente −2𝜋𝑐𝑜𝑠(𝜋𝑥) In [42]: Para con x=1.5 Vemos que no es convergente −𝜋/(2 ) 1 − 𝑥²/4 ⎯ ⎯ ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ √ In [43]: In [44]: Derivada de -2 * sin(pi*x): Out[39]: −2𝜋 cos (𝜋𝑥) Derivada de pi * asin(-x/2): Out[40]: − 𝜋 2 1 − 𝑥2 4 ⎯ ⎯ ⎯⎯⎯⎯⎯⎯⎯⎯⎯ √ 1.999999999999999 1.1542024162330739e-15 -2.3748208234474517 print("Derivada de", strGx2+":") sympy diff(strGx2 x) print("Derivada de", strGx3+":") sympy.diff(strGx3, x) print(2*pi*cos(pi*m)+2) print(-2*pi*cos(pi*m)) print(-pi/(2*(1-m**2/4)**0.5)) #Declaramos como funcion (def) a nuestra funcion fx y nuestro g(x) que es conv exec("def fx(x):treturn {}".format(strFx)) def gx(x): return -2*pi*cos(pi*x) def puntofijo(fx,x0,tol,i): print("iter","x0"," ","xk"," ","ERA", " ", "f(xk)" ) KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 15 of 18 9/12/21, 03:13
  • 16.
    In [45]: Generación de tabla Enla siguiente celda procederemos a generar una tabla correctamente identada para poder visualizar de mejor manera los datos. Se realizarán 150 iteraciones e imprimiremos cada 10. In [46]: iter x0 xk ERA f(xk) 1 1 6.283185 84.084506 7.83689 2 6.283185 -3.956407 258.810389 3.68336 3 -3.956407 -6.224354 36.43667 7.52016 4 -6.224354 -4.786037 30.052368 6.03143 5 -4.786037 4.916362 197.349164 5.43585 6 4.916362 6.06753 18.972605 6.48866 7 6.06753 -6.142316 198.782452 7.00702 8 -6.142316 -5.665582 8.414562 3.93013 9 -5.665582 -3.123032 81.412892 2.36911 10 -3.123032 5.819664 153.663433 4.74623 11 5.819664 -5.301514 209.77362 3.67791 12 -5.301514 3.668939 244.497215 1.94407 13 3.668939 -3.180353 215.362614 2.10683 14 -3.180353 5.301328 159.991634 3.6784 15 5.301328 3.671922 44.374729 1.95662 16 3.671922 -3.231006 213.646402 1.90365 17 -3.231006 4.699927 168.745887 6.31823 18 4.699927 3.691994 27.300497 2.04491 19 3.691994 -3.564159 203.586676 1.60465 20 -3.564159 -1.257894 183.343409 0.19095 21 -1.257894 4.331349 129.041621 6.05713 22 4.331349 -3.175461 236.400612 2.12799 23 -3.175461 5.352534 159.326317 3.56335 24 5.352534 2.807852 90.627349 3.94316 25 2.807852 5.17275 45.718391 4.13983 26 5.17275 5.380367 3.858786 3.51997 27 5.380367 2.306258 133.294212 3.94709 28 2.306258 -3.592516 164.196179 1.6764 29 -3.592516 -1.800596 99.518184 0.62806 30 -1.800596 -5.090109 64.625593 4.53147 Out[45]: (−5.09010926172804,   − 4.531467829699) for i in range(1,i+1): xk = gx(x0) ERA = 100*abs(xk-x0)/abs(xk) # xk diferente de cero print(i, round(x0,6), " ",round(xk,6), " ", round(ERA,6)," ", round x0=xk # actualizando if(ERA<tol): break return(xk,fx(xk)) #Ejecutamos el algoritmo de punto fijo puntofijo(fx,x0,ERA, 30) from IPython.display import Markdown as md strOut = "" esp = " | " def puntofijo2(fx,x0,tol,i): KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 16 of 18 9/12/21, 03:13
  • 17.
    In [47]: Tal como podemosapreciar, a pesar de que hemos dado 150 iteraciones, no hemos podido dar con un valor dentro de la tolerancia pero nuestro resultado es 2.070607 con y = 6.91915 Out[47]: Iter x0 xk ERA f(xk) 10 -3.123032 5.819664 153.663433 4.74623 20 -3.564159 -1.257894 183.343409 0.19095 30 -1.800596 -5.090109 64.625593 4.53147 40 4.407854 -1.793587 345.75641 0.58566 50 2.215363 -4.899103 145.219771 5.52249 60 -3.3618 2.643061 227.193434 4.44444 70 -4.480823 -0.378312 1084.425431 2.23393 80 -4.314898 -3.451291 25.022749 1.47466 90 -6.268147 -4.182514 49.865514 5.26747 100 -4.019495 -6.271405 35.907595 7.77745 110 -2.99806 6.283069 147.716494 7.83631 120 -6.281523 -3.981843 57.754149 3.86782 130 1.206561 5.006011 75.897764 4.96825 140 -6.183225 -5.270692 17.313331 3.7676 150 2.070607 -6.12924 133.782443 6.91915 --- --- --- --- --- 150 2.070607 -6.12924 133.782443 6.91915 global strOut strOut = """ | Iter | x0 | xk | ERA | f(xk) | | --- | --- | --- | --- | --- | """ #print("iter","x0"," ","xk"," ","ERA", " ", "f(xk)" ) for j in range(1,i+1): xk = gx(x0) ERA = 100*abs(xk-x0)/abs(xk) # xk diferente de cero #print(i, round(x0,6), " ",round(xk,6), " ", round(ERA,6)," ", round(a if(j%10==0): strOut+=str(j)+esp+str(round(x0,6))+esp+str(round(xk,6))+esp if(j==i): strOut+="| --- | --- | --- | --- | --- |n" strOut+=str(j)+esp+str(round(x0,6))+esp+str(round(xk,6))+esp x0=xk # actualizando if(ERA<tol): break return md(strOut) puntofijo2(fx,x0,ERA, 150) KristhyanKurtLazarteZubia-Evidencia1 - Jupyter Notebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 17 of 18 9/12/21, 03:13
  • 18.
    KristhyanKurtLazarteZubia-Evidencia1 - JupyterNotebook http://localhost:8888/notebooks/Jupyter/KristhyanKurtLa... 18 of 18 9/12/21, 03:13