|
SmtC: Show me the Code
Ole Peter Smith
Instituto de Matemática e Estatística
Universidade Federal de Goiás
http://www.olesmith.com.br
|
|
Quando Pedro me fala sobre Paulo
Sei mais do Pedro do que do Paulo
Sigmund Freud
|
Atividades 4
- Implemente as defs das funções no item anterior.
Implemente também suas coeficientes de Taylor,
\(a_n.\)
Salvar em Arquivo N01.py.
Python Listing: N01.py.
from math import *
def Factorial(n):
if (n==0): return 1
elif (n==1): return 1
else: return n*Factorial(n-1)
def f1(x):
return e**x
def Text1():
return 'e**x'
def a1(n):
return 1.0/(1.0*Factorial(n))
def f2(x):
return cos(x)
def Text2():
return 'cos(x)'
def a2(n):
if ( (n%2)==1): return 0.0
p=n/2
return (-1.0)**p/(1.0*Factorial(n))
|
Output from: /usr/bin/python N01.py
|
-
Para
\(x=0.5\)
e as funções no item anterior, calcule os valores
do polinômio de Taylor de ordem
\(0,1,...,10.\)
Salvar em Arquivo N02.py.
Python Listing: N02.py.
from math import *
from Polynomial import *
from Residual import *
from N01 import *
def Calc_Taylor_Pol(a,n):
P=[]
for i in range(n+1):
P.append( a(i) )
return P
fs=[f1,f2]
ass=[a1,a2]
texts=[Text1,Text2]
x=0.5
for i in range( len(fs) ):
f=fs[i]
a=ass[i]
text=texts[i]
print text()
print "x\t\tn\tAnalytical\tApprox\t\tAbsolute\tRelative"
for n in range(10):
P=Calc_Taylor_Pol(a,n)
#Polynomia_Print(P)
f_ana=f(x)
f_tay=Polynomia_Calc(P,x)
print "%.6f\t%d\t%.6f\t%.6f\t%.6e\t%.6e" % (x,n,f_ana,f_tay,R_Abs(f_ana,f_tay),R_Rel(f_ana,f_tay))
|
e**x
x n Analytical Approx Absolute Relative
0.500000 0 1.648721 1.000000 6.487213e-01 3.934693e-01
0.500000 1 1.648721 1.500000 1.487213e-01 9.020401e-02
0.500000 2 1.648721 1.625000 2.372127e-02 1.438768e-02
0.500000 3 1.648721 1.645833 2.887937e-03 1.751623e-03
0.500000 4 1.648721 1.648438 2.837707e-04 1.721156e-04
0.500000 5 1.648721 1.648698 2.335403e-05 1.416494e-05
0.500000 6 1.648721 1.648720 1.652645e-06 1.002380e-06
0.500000 7 1.648721 1.648721 1.025454e-07 6.219691e-08
0.500000 8 1.648721 1.648721 5.664166e-09 3.435490e-09
0.500000 9 1.648721 1.648721 2.818770e-10 1.709670e-10
cos(x)
x n Analytical Approx Absolute Relative
0.500000 0 0.877583 1.000000 1.224174e-01 1.394939e-01
0.500000 1 0.877583 1.000000 1.224174e-01 1.394939e-01
0.500000 2 0.877583 0.875000 2.582562e-03 2.942814e-03
0.500000 3 0.877583 0.875000 2.582562e-03 2.942814e-03
0.500000 4 0.877583 0.877604 2.160478e-05 2.461851e-05
0.500000 5 0.877583 0.877604 2.160478e-05 2.461851e-05
0.500000 6 0.877583 0.877582 9.661260e-08 1.100895e-07
0.500000 7 0.877583 0.877582 9.661260e-08 1.100895e-07
0.500000 8 0.877583 0.877583 2.686054e-10 3.060742e-10
0.500000 9 0.877583 0.877583 2.686054e-10 3.060742e-10
Output from: /usr/bin/python N02.py
|
|
Messages:
0 secs.
|