1-Evaluación Numérica
1-Evaluación Numérica
1-Evaluación Numérica
Muchas de las funciónes trascendentales (seno, coseno, logaritmo, etc) requieren evaluación
numérica. La evaluación numerica generalmente se hacía utilizando mediciones o tablas sin
embargo hay técnicas y algoritmos que permiten obtener evaluaciones con mayor exactitud.
In [1]: import numpy as np
In [2]: import matplotlib.pyplot as plt
In [3]: %matplotlib inline
Número π
El número π aparece en muchos modelos y funciones que involucran periodos, circunferencias y
trigonometría. En muchas aplicaciones la exactitud es necesaria para esto se requiere poder
hacer el cómputo de várias cifras decimales.
In [4]: np.pi
Out[4]: 3.141592653589793
Usaremos una serie conocida como GregoryLeibniz
π = 4
∞ −
(
∑ 2n + 1
1)
n
n=0
In [5]: def pi(terminos=0):
suma=np.array(0.,dtype=np.float64)
for n in np.arange(terminos):
suma=suma+(((‐1)**(n)/(2*n+1)))
return 4*suma
In [6]: pi(0)
Out[6]: 0.0
In [7]: pi(1)
Out[7]: 4.0
In [8]: pi(10)
Out[8]: 3.0418396189294032
In [9]: pi(100)
Out[9]: 3.1315929035585537
In [10]: pi(1000)
Out[10]: 3.1405926538397941
In [11]: pi(10000)
Out[11]: 3.1414926535900345
In [12]: pi(100000)
Out[12]: 3.1415826535897198
In [13]: n=np.linspace(0,10,11)
n
Out[13]: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
In [14]: serie=[]
for item in n:
serie.append(pi(item))
print(serie[‐1])
3.04183961893
In [15]: plt.plot(n,serie,'r.')
plt.xlabel('n')
plt.ylabel('Serie')
plt.show()
In [16]: plt.plot(n,serie,'ro')
plt.plot(n,serie,'b‐')
plt.xlabel('n')
plt.ylabel('Serie')
plt.show()
Usando una serie propuesta por Newton
∞ n
2 n!
2
π = 2
∑ (2n + 1)!
n=0
In [17]: import math
def pi(error_max=1e‐3):
n=0
suma=((2**n)*math.factorial(n)**2)/math.factorial(2*n+1)
suma=np.array(suma,dtype=np.float64)
while True:
pi_old=2*suma
n=n+1
suma=suma+((2**n)*math.factorial(n)**2)/math.factorial(2*n+1)
pi_new=2*suma
error=abs(pi_new‐pi_old)
if error<error_max:
break
print(n)
return pi_new
In [18]: pi(1e‐1)
4
Out[18]: 3.0984126984126981
In [19]: pi(1e‐2)
7
Out[19]: 3.1371295371295367
In [20]: pi(1e‐3)
10
Out[20]: 3.1411060216013769
In [21]: pi(1e‐4)
13
Out[21]: 3.1415379931734746
In [22]: pi(1e‐5)
16
Out[22]: 3.1415863960370602
In [23]: pi(1e‐10)
32
Out[23]: 3.141592653519746
In [24]: def pi(terminos=0):
suma=0.
for n in np.arange(terminos):
suma=suma+((2**n)*math.factorial(n)**2)/math.factorial(2*n+1)
return 2*suma
In [25]: n=np.linspace(0,20,21)
n
Out[25]: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.,
11., 12., 13., 14., 15., 16., 17., 18., 19., 20.])
In [26]: serie=[]
for item in n:
serie.append(pi(item))
print(serie[‐1])
3.14159192768
In [27]: plt.plot(n,serie,'r.')
plt.xlabel('n')
plt.ylabel('Serie')
plt.show()
In [28]: plt.plot(n,serie,'r.')
plt.plot(n,serie,'b‐')
plt.xlabel('n')
plt.ylabel('Serie')
plt.show()
Usando una serie conocida como la fórmula de Wallis
∞
−
2
(2n)
π = 2
∏ ( (2n 1)(2n + 1) )
n=1
In [29]: def pi(terminos=1):
producto=np.array(1.,dtype=np.float64)
for n in np.arange(1,terminos):
producto=producto*((2*n)**2)/((2*n‐1)*(2*n+1))
return 2*producto
In [30]: pi(1)
Out[30]: 2.0
In [31]: pi(10)
Out[31]: 3.060034547126889
In [32]: pi(100)
Out[32]: 3.1337091459408928
In [33]: pi(1000)
Out[33]: 3.1408069608284515
In [34]: pi(10000)
Out[34]: 3.1415141108281359
In [35]: n=np.linspace(1,20,20)
n
Out[35]: array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.,
12., 13., 14., 15., 16., 17., 18., 19., 20.])
In [36]: serie=[]
for item in n:
serie.append(pi(item))
print(serie[‐1])
3.10157726344
In [37]: plt.plot(n,serie,'r.')
plt.xlabel('n')
plt.ylabel('Serie')
plt.show()
In [38]: plt.plot(n,serie,'r.')
plt.plot(n,serie,'b‐')
plt.xlabel('n')
plt.ylabel('Serie')
plt.show()
Funciones trigonométricas
Las funciones trigonométricas por lo general tambíen tienen forma de evaluarlas usando una
serie. Todas las funciones trigonométricas son periódicas por lo que solo se requiere exactitud
para cierto intervalo.
Seno
sin(x) =
∞ ( −
1)
∑ (2n + 1)!
n
x
2n+1
−≤ ≤
n=0
π x π
In [39]: x=np.linspace(‐np.pi,np.pi,101)
In [40]: plt.plot(x,np.sin(x))
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.ylim(‐1.5,1.5)
plt.show()
In [41]: def sin(x,terminos):
x=np.array(x)
suma=0
for n in range(terminos):
suma=suma+(((‐1)**n)*x**(2*n+1))/math.factorial(2*n+1)
return suma
In [42]: plt.plot(x,np.sin(x))
plt.plot(x,sin(x,1),label='n=1')
plt.plot(x,sin(x,2),label='n=2')
plt.plot(x,sin(x,4),label='n=4')
plt.plot(x,sin(x,6),label='n=6')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.ylim(‐1.5,1.5)
plt.legend(bbox_to_anchor=(1.5,1))
plt.show()
Calculémos el error en la serie comparándola con la de Numpy
In [43]: error=np.sin(x)‐sin(x,6)
print(error)
[ ‐4.45160238e‐04 ‐3.42960064e‐04 ‐2.62786866e‐04 ‐2.00215451e‐04
‐1.51642131e‐04 ‐1.14145316e‐04 ‐8.53674344e‐05 ‐6.34152937e‐05
‐4.67763250e‐05 ‐3.42484754e‐05 ‐2.48817705e‐05 ‐1.79298244e‐05
‐1.28097921e‐05 ‐9.06945738e‐06 ‐6.36032517e‐06 ‐4.41574128e‐06
‐3.03320302e‐06 ‐2.06014381e‐06 ‐1.38258236e‐06 ‐9.16120217e‐07
‐5.98852771e‐07 ‐3.85829251e‐07 ‐2.44758115e‐07 ‐1.52706388e‐07
‐9.35861122e‐08 ‐5.62589491e‐08 ‐3.31219004e‐08 ‐1.90639345e‐08
‐1.07056001e‐08 ‐5.85216298e‐09 ‐3.10590975e‐09 ‐1.59555902e‐09
‐7.90605137e‐10 ‐3.76303322e‐10 ‐1.71210490e‐10 ‐7.40304484e‐11
‐3.02081693e‐11 ‐1.15331078e‐11 ‐4.07618383e‐12 ‐1.31583633e‐12
‐3.81361609e‐13 ‐9.68114477e‐14 ‐2.09832152e‐14 ‐3.71924713e‐15
‐4.99600361e‐16 ‐5.55111512e‐17 0.00000000e+00 ‐2.77555756e‐17
2.77555756e‐17 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 5.55111512e‐17
4.44089210e‐16 3.71924713e‐15 2.09277040e‐14 9.68114477e‐14
3.81250587e‐13 1.31583633e‐12 4.07607281e‐12 1.15329968e‐11
3.02081693e‐11 7.40304484e‐11 1.71210601e‐10 3.76303211e‐10
7.90605137e‐10 1.59555902e‐09 3.10590975e‐09 5.85216298e‐09
1.07056002e‐08 1.90639344e‐08 3.31219006e‐08 5.62589491e‐08
9.35861123e‐08 1.52706387e‐07 2.44758115e‐07 3.85829251e‐07
Para obtener el error total para todos los valores en x se puede usar la norma o magnitud del
error.
E = √⋅ ⎯⎯⎯⎯⎯⎯⎯
e e
In [44]: E_tot=np.sqrt(np.dot(error,error))
Etot=np.linalg.norm(error)
print(E_tot,Etot)
0.000976617280754 0.000976617280754
In [45]: E_tot
Out[45]: 0.0009766172807539374
Coseno
cos(x) =
∞
∑
−
( 1)
n
x
2n
(2n)!
−≤ ≤
n=0
π x π
In [46]: x=np.linspace(‐np.pi,np.pi,101)
In [47]: plt.plot(x,np.cos(x))
plt.xlabel('x')
plt.ylabel('cos(x)')
plt.ylim(‐1.5,1.5)
plt.show()
In [48]: def cos(x,terminos):
x=np.array(x)
suma=0
for n in range(terminos):
suma=suma+(((‐1)**n)*x**(2*n))/math.factorial(2*n)
return suma
In [49]: plt.plot(x,np.cos(x))
plt.plot(x,cos(x,1),label='n=1')
plt.plot(x,cos(x,2),label='n=2')
plt.plot(x,cos(x,4),label='n=4')
plt.plot(x,cos(x,6),label='n=6')
plt.xlabel('x')
plt.ylabel('cos(x)')
plt.ylim(‐1.5,1.5)
plt.legend(bbox_to_anchor=(1.5,1))
plt.show()
In [50]: error=np.cos(x)‐cos(x,6)
print(error)
[ 1.82910401e‐03 1.43833000e‐03 1.12535699e‐03 8.75875232e‐04
6.77980116e‐04 5.21807902e‐04 3.99220231e‐04 3.03531802e‐04
2.29276131e‐04 1.72004787e‐04 1.28115966e‐04 9.47087082e‐05
6.94594232e‐05 5.05177678e‐05 3.64192454e‐05 2.60121896e‐05
1.83970741e‐05 1.28763358e‐05 8.91312219e‐06 6.09757789e‐06
4.11946448e‐06 2.74607302e‐06 1.80453194e‐06 1.16774257e‐06
7.43288614e‐07 4.64766008e‐07 2.85067467e‐07 1.71232743e‐07
1.00541757e‐07 5.75849271e‐08 3.20938378e‐08 1.73568437e‐08
9.07914227e‐09 4.57604621e‐09 2.21234608e‐09 1.02046849e‐09
4.46183424e‐10 1.83464133e‐10 7.02491398e‐11 2.47397658e‐11
7.88646926e‐12 2.22832863e‐12 5.42343948e‐13 1.09356968e‐13
1.72084569e‐14 1.99840144e‐15 2.22044605e‐16 1.11022302e‐16
0.00000000e+00 1.11022302e‐16 0.00000000e+00 1.11022302e‐16
0.00000000e+00 0.00000000e+00 2.22044605e‐16 1.99840144e‐15
1.72084569e‐14 1.09245946e‐13 5.42343948e‐13 2.22832863e‐12
7.88646926e‐12 2.47397658e‐11 7.02491398e‐11 1.83464133e‐10
4.46183535e‐10 1.02046849e‐09 2.21234620e‐09 4.57604615e‐09
9.07914227e‐09 1.73568436e‐08 3.20938378e‐08 5.75849272e‐08
1.00541757e‐07 1.71232743e‐07 2.85067467e‐07 4.64766008e‐07
7.43288614e‐07 1.16774257e‐06 1.80453194e‐06 2.74607302e‐06
In [51]: Etot=np.linalg.norm(error)
print(Etot)
0.00413497678564
Función exponencial
Una de las funciones más conocidas y que aparecen más en modelos de ciencia e ingeniería es
la función exponencial.
x
∞ x
n
e =
∑ n!
n=0
In [52]: x=np.linspace(‐3,3,101)
In [53]: plt.plot(x,np.exp(x))
plt.xlabel('x')
plt.ylabel('exp(x)')
plt.ylim(‐5,25)
plt.show()
In [54]: def exp(x,terminos):
suma=0
for n in range(terminos):
suma=suma+(x**n)/math.factorial(n)
return suma
In [55]: plt.plot(x,np.exp(x))
plt.plot(x,exp(x,1),label='n=1')
plt.plot(x,exp(x,2),label='n=2')
plt.plot(x,exp(x,4),label='n=4')
plt.plot(x,exp(x,6),label='n=6')
plt.xlabel('x')
plt.ylabel('exp(x)')
plt.ylim(‐5,25)
plt.legend(bbox_to_anchor=(1.5,1))
plt.show()
In [56]: error=np.exp(x)‐exp(x,6)
print(error)
[ 6.99787068e‐01 6.23876802e‐01 5.54831835e‐01 4.92161848e‐01
4.35401613e‐01 3.84110263e‐01 3.37870561e‐01 2.96288175e‐01
2.58990950e‐01 2.25628193e‐01 1.95869953e‐01 1.69406308e‐01
1.45946655e‐01 1.25219010e‐01 1.06969302e‐01 9.09606783e‐02
7.69728184e‐02 6.48012440e‐02 5.42566415e‐02 4.51641888e‐02
3.73628882e‐02 3.07049061e‐02 2.50549207e‐02 2.02894764e‐02
1.62963477e‐02 1.29739101e‐02 1.02305222e‐02 7.98391570e‐03
6.16059733e‐03 4.69526098e‐03 3.53021191e‐03 2.61480334e‐03
1.90488628e‐03 1.36227353e‐03 9.54218455e‐04 6.52909741e‐04
4.34982949e‐04 2.81049945e‐04 1.75247320e‐04 1.04804972e‐04
5.96360940e‐05 3.19498940e‐05 1.58884461e‐05 7.18917506e‐06
2.87455103e‐06 9.70681718e‐07 2.56586553e‐07 4.60512720e‐08
4.07715739e‐09 6.42486064e‐11 0.00000000e+00 6.53594956e‐11
4.21937552e‐09 4.84818103e‐08 2.74801405e‐07 1.05757600e‐06
3.18608034e‐06 8.10625863e‐06 1.82255529e‐05 3.72846649e‐05
7.08003905e‐05 1.26583922e‐04 2.15339284e‐04 3.51346858e‐04
5.53237261e‐04 8.44861157e‐04 1.25626094e‐03 1.82475060e‐03
2.59611043e‐03 3.62590367e‐03 4.98092274e‐03 6.74077289e‐03
8.99960190e‐03 1.18679849e‐02 1.54749735e‐02 1.99703203e‐02
2.55268887e‐02 3.23432592e‐02 4.06465465e‐02 5.06954372e‐02
6.27834644e‐02 7.72425329e‐02 9.44467099e‐02 1.14816299e‐01
1.38822211e‐01 1.66990663e‐01 1.99908198e‐01 2.38227085e‐01
2.82671081e‐01 3.34041613e‐01 3.93224381e‐01 4.61196418e‐01
5.39033640e‐01 6.27918909e‐01 7.29150636e‐01 8.44151975e‐01
9.74480624e‐01 1.12183929e+00 1.28808683e+00 1.47525016e+00
1.68553692e+00]
In [57]: Etot=np.linalg.norm(error)
print(Etot)
3.71272565662
In [ ]: