O dia que bosta valesse ouro.
Os pobres todos nascerão sem c*.
Grafitti.
|
Cálculo Numérico, Semestre 2017.2, Prova I
PDF
- N01.py:
Python Listing: N01.py.
from math import *
from Vector import *
from Matrix import *
from Gauss import *
#Question a
vs=[
[-2.0,1.0,1.0,1.0,],
[1.0,-2.0,1.0,1.0,],
[1.0,1.0,-2.0,1.0,],
[1.0,1.0,1.0,-2.0,],
]
V=[
[-2.0,1.0,1.0,1.0,],
[1.0,-2.0,1.0,1.0,],
[1.0,1.0,-2.0,1.0,],
[1.0,1.0,1.0,-2.0,],
]
#Question d
I=Matrix_Identity(4)
VV=Matrix_Copy(V)
V_inv=Inverse(VV)
print "V^{-1}=",
Matrix_Print(V_inv)
VxV=Matrices_Mult(V,V_inv)
R=Matrices_Sub(I,VxV)
print "R=",
Matrix_Print(R)
print "residual=",Matrix_Norm(R)/Matrix_Norm(V)
#Question c
for i in range( len(vs) ):
VV=Matrix_Copy(V)
xi=list(vs[i])
Gauss(VV,xi)
print "x_%d=" % i,
Vector_Print(xi)
vv=Matrix_Mult_Vector(V,xi)
vv=Vectors_Sub(vv,vs[i])
print "vv=",
Vector_Print(vv)
print "residual=%.6e" % ( Vector_Norm(vv)/Vector_Norm(vs[i]) )
|
Gauss Forward, det -27.0
Matrix_Gauss_Test, Residual: 8.326673e-17
V^{-1}= [
[-0.000000,0.333333,0.333333,0.333333]
[0.333333,0.000000,0.333333,0.333333]
[0.333333,0.333333,0.000000,0.333333]
[0.333333,0.333333,0.333333,0.000000]
]
R= [
[0.000000,0.000000,0.000000,0.000000]
[0.000000,0.000000,0.000000,0.000000]
[0.000000,0.000000,0.000000,0.000000]
[0.000000,0.000000,0.000000,0.000000]
]
residual= 3.14718645319e-17
Gauss Forward, det -27.0
x_0= [1.000000,0.000000,0.000000,0.000000]
vv= [0.000000,0.000000,0.000000,0.000000]
residual=0.000000e+00
Gauss Forward, det -27.0
x_1= [0.000000,1.000000,0.000000,0.000000]
vv= [0.000000,0.000000,0.000000,0.000000]
residual=0.000000e+00
Gauss Forward, det -27.0
x_2= [0.000000,0.000000,1.000000,0.000000]
vv= [0.000000,0.000000,0.000000,0.000000]
residual=0.000000e+00
Gauss Forward, det -27.0
x_3= [0.000000,0.000000,0.000000,1.000000]
vv= [0.000000,0.000000,0.000000,0.000000]
residual=0.000000e+00
Output from: /usr/bin/python N01.py
|
- N02.py:
Python Listing: N02.py.
from math import *
from Polynomial import *
#Question a
Ps=[
[-1.0, 1.0],
[ 1.0,-1.0, 1.0],
[-1.0, 1.0,-1.0, 1.0],
[ 1.0,-1.0, 1.0,-1.0, 1.0],
[-1.0, 1.0,-1.0, 1.0,-1.0,1.0],
]
xs=[
-5.0,-4.0,-3.0,-2.0,-1.0,
0.0,
1.0,2.0,3.0,4.0,5.0,
]
def Question_AA():
text=["i"]
spec=["l"]
for j in range( len(xs) ):
text.append( "%.6f" %xs[j] )
spec.append("l")
print "\\begin{tabular}{"+"".join(spec)+"}"
print " & ".join(text)+"\\\\"+"\\hline"
for i in range( len(Ps) ):
Pi=Ps[i]
text=["%d" %i]
for j in range( len(xs) ):
text.append( "%.6f" % Polynomia_Calc(Pi,xs[j]) )
print " & ".join(text)+"\\\\"
print "\\end{tabular}"
def Question_A():
text=["i"]
for j in range( len(xs) ):
text.append( "%.6f" %xs[j] )
print "\t".join(text)
for i in range( len(Ps) ):
Pi=Ps[i]
text=["%d" %i]
for j in range( len(xs) ):
text.append( "%.6f" % Polynomia_Calc(Pi,xs[j]) )
print "\t".join(text)
def Question_B():
for i in range( len(Ps) ):
Pi=Ps[i]
for j in range( len(Ps) ):
Pj=Ps[j]
Qij=Polynomias_Mult(Pi,Pj)
print "Q_%d_%d=" % (i,j),
Polynomia_Print(Qij)
def Polynomia_Derive(P):
Q=[]
for i in range( 1,len(P) ):
Q.append(i*P[i])
return Q
def Question_C():
for i in range( len(Ps) ):
Pi=Ps[i]
dPi=Polynomia_Derive(Pi)
Polynomia_Print(dPi)
Question_A()
Question_B()
Question_C()
|
i -5.000000 -4.000000 -3.000000 -2.000000 -1.000000 0.000000 1.000000 2.000000 3.000000 4.000000 5.000000
0 -6.000000 -5.000000 -4.000000 -3.000000 -2.000000 -1.000000 0.000000 1.000000 2.000000 3.000000 4.000000
1 31.000000 21.000000 13.000000 7.000000 3.000000 1.000000 1.000000 3.000000 7.000000 13.000000 21.000000
2 -156.000000 -85.000000 -40.000000 -15.000000 -4.000000 -1.000000 0.000000 5.000000 20.000000 51.000000 104.000000
3 781.000000 341.000000 121.000000 31.000000 5.000000 1.000000 1.000000 11.000000 61.000000 205.000000 521.000000
4 -3906.000000 -1365.000000 -364.000000 -63.000000 -6.000000 -1.000000 0.000000 21.000000 182.000000 819.000000 2604.000000
Q_0_0= 1.000000x**2 -2.000000x +1.000000
Q_0_1= 1.000000x**3 -2.000000x**2 +2.000000x -1.000000
Q_0_2= 1.000000x**4 -2.000000x**3 +2.000000x**2 -2.000000x +1.000000
Q_0_3= 1.000000x**5 -2.000000x**4 +2.000000x**3 -2.000000x**2 +2.000000x -1.000000
Q_0_4= 1.000000x**6 -2.000000x**5 +2.000000x**4 -2.000000x**3 +2.000000x**2 -2.000000x +1.000000
Q_1_0= 1.000000x**3 -2.000000x**2 +2.000000x -1.000000
Q_1_1= 1.000000x**4 -2.000000x**3 +3.000000x**2 -2.000000x +1.000000
Q_1_2= 1.000000x**5 -2.000000x**4 +3.000000x**3 -3.000000x**2 +2.000000x -1.000000
Q_1_3= 1.000000x**6 -2.000000x**5 +3.000000x**4 -3.000000x**3 +3.000000x**2 -2.000000x +1.000000
Q_1_4= 1.000000x**7 -2.000000x**6 +3.000000x**5 -3.000000x**4 +3.000000x**3 -3.000000x**2 +2.000000x -1.000000
Q_2_0= 1.000000x**4 -2.000000x**3 +2.000000x**2 -2.000000x +1.000000
Q_2_1= 1.000000x**5 -2.000000x**4 +3.000000x**3 -3.000000x**2 +2.000000x -1.000000
Q_2_2= 1.000000x**6 -2.000000x**5 +3.000000x**4 -4.000000x**3 +3.000000x**2 -2.000000x +1.000000
Q_2_3= 1.000000x**7 -2.000000x**6 +3.000000x**5 -4.000000x**4 +4.000000x**3 -3.000000x**2 +2.000000x -1.000000
Q_2_4= 1.000000x**8 -2.000000x**7 +3.000000x**6 -4.000000x**5 +4.000000x**4 -4.000000x**3 +3.000000x**2 -2.000000x +1.000000
Q_3_0= 1.000000x**5 -2.000000x**4 +2.000000x**3 -2.000000x**2 +2.000000x -1.000000
Q_3_1= 1.000000x**6 -2.000000x**5 +3.000000x**4 -3.000000x**3 +3.000000x**2 -2.000000x +1.000000
Q_3_2= 1.000000x**7 -2.000000x**6 +3.000000x**5 -4.000000x**4 +4.000000x**3 -3.000000x**2 +2.000000x -1.000000
Q_3_3= 1.000000x**8 -2.000000x**7 +3.000000x**6 -4.000000x**5 +5.000000x**4 -4.000000x**3 +3.000000x**2 -2.000000x +1.000000
Q_3_4= 1.000000x**9 -2.000000x**8 +3.000000x**7 -4.000000x**6 +5.000000x**5 -5.000000x**4 +4.000000x**3 -3.000000x**2 +2.000000x -1.000000
Q_4_0= 1.000000x**6 -2.000000x**5 +2.000000x**4 -2.000000x**3 +2.000000x**2 -2.000000x +1.000000
Q_4_1= 1.000000x**7 -2.000000x**6 +3.000000x**5 -3.000000x**4 +3.000000x**3 -3.000000x**2 +2.000000x -1.000000
Q_4_2= 1.000000x**8 -2.000000x**7 +3.000000x**6 -4.000000x**5 +4.000000x**4 -4.000000x**3 +3.000000x**2 -2.000000x +1.000000
Q_4_3= 1.000000x**9 -2.000000x**8 +3.000000x**7 -4.000000x**6 +5.000000x**5 -5.000000x**4 +4.000000x**3 -3.000000x**2 +2.000000x -1.000000
Q_4_4= 1.000000x**10 -2.000000x**9 +3.000000x**8 -4.000000x**7 +5.000000x**6 -6.000000x**5 +5.000000x**4 -4.000000x**3 +3.000000x**2 -2.000000x +1.000000
1.000000
2.000000x -1.000000
3.000000x**2 -2.000000x +1.000000
4.000000x**3 -3.000000x**2 +2.000000x -1.000000
5.000000x**4 -4.000000x**3 +3.000000x**2 -2.000000x +1.000000
Output from: /usr/bin/python N02.py
|
- N03.py:
Python Listing: N03.py.
from math import *
from Lagrange import *
xs=[-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0]
ys=[5.0,1.0,-4.0,-1.0,3.0,2.0,1.0]
#Question a
print "Question a"
Hs=[]
for i in range( len(xs) ):
Hi=Lagrange_H_P(i,xs)
print "H_"+str(i)+"=",
Polynomia_Print(Hi)
Hs.append(Hi)
#Question b
print "Question b"
text=["x" ]
for j in range( len(xs) ):
text.append( "%.6f" % xs[j] )
print "\t".join(text)
for i in range( len(Hs) ):
Hi=Hs[i]
text=["H_"+str(i) ]
for j in range( len(xs) ):
value=Polynomia_Calc(Hi,xs[j])
text.append( "%.6f" % value )
print "\t".join(text)
#Question c
print "Question c"
P=Lagrange_P(xs,ys)
text=["x" ]
for j in range( len(xs) ):
text.append( "%.6f" % xs[j] )
print "\t".join(text)
text=["y" ]
for j in range( len(xs) ):
value=Polynomia_Calc(P,xs[j])
text.append( "%.6f" % value )
print "\t".join(text)
|
Question a
H_0= 0.001389x**6 -0.004167x**5 -0.006944x**4 +0.020833x**3 +0.005556x**2 -0.016667x
H_1= -0.008333x**6 +0.016667x**5 +0.083333x**4 -0.166667x**3 -0.075000x**2 +0.150000x
H_2= 0.020833x**6 -0.020833x**5 -0.270833x**4 +0.270833x**3 +0.750000x**2 -0.750000x
H_3= -0.027778x**6 +0.388889x**4 -1.361111x**2 +1.000000
H_4= 0.020833x**6 +0.020833x**5 -0.270833x**4 -0.270833x**3 +0.750000x**2 +0.750000x
H_5= -0.008333x**6 -0.016667x**5 +0.083333x**4 +0.166667x**3 -0.075000x**2 -0.150000x
H_6= 0.001389x**6 +0.004167x**5 -0.006944x**4 -0.020833x**3 +0.005556x**2 +0.016667x
Question b
x -3.000000 -2.000000 -1.000000 0.000000 1.000000 2.000000 3.000000
H_0 1.000000 -0.000000 -0.000000 0.000000 0.000000 0.000000 0.000000
H_1 -0.000000 1.000000 -0.000000 0.000000 0.000000 0.000000 -0.000000
H_2 0.000000 0.000000 1.000000 0.000000 0.000000 0.000000 0.000000
H_3 -0.000000 0.000000 0.000000 1.000000 0.000000 0.000000 -0.000000
H_4 0.000000 0.000000 0.000000 0.000000 1.000000 0.000000 0.000000
H_5 -0.000000 -0.000000 -0.000000 -0.000000 -0.000000 1.000000 -0.000000
H_6 0.000000 0.000000 0.000000 0.000000 -0.000000 -0.000000 1.000000
Question c
x -3.000000 -2.000000 -1.000000 0.000000 1.000000 2.000000 3.000000
y 5.000000 1.000000 -4.000000 -1.000000 3.000000 2.000000 1.000000
Output from: /usr/bin/python N03.py
|
- N04.py:
Python Listing: N04.py.
from math import *
from Integration import *
#Question a
def f_0(x):
return e**(-x)
def f_1(x):
return x*e**(-x)
def f_2(x):
return x*x*e**(-x)
def f_3(x):
return x*x*x*e**(-x)
def f_4(x):
return x*x*x*x*e**(-x)
#Question b
def F_0(x):
return -e**(-x)
def F_1(x):
return -(x+1.0)*e**(-x)
def F_2(x):
return -(x**2+2.0*x+2.0)*e**(-x)
def F_3(x):
return -(x**3+3.0*x**2+6.0*x+6.0)*e**(-x)
def F_4(x):
return -(x**4+4.0*x**3+12.0*x**2+24.0*x+24.0)*e**(-x)
fs=[f_0,f_1,f_2,f_3,f_4]
Fs=[F_0,F_1,F_2,F_3,F_4]
a=0.0
b=1.0
ni=10
for n in range( len(fs) ):
fn=fs[n]
Fn=Fs[n]
print "f_"+str(n)+":"
text=[
"ni",
"Ana",
"Trapeziod",
"r",
"Simpson",
"r",
]
print "\t".join(text)
analytical=Fn(b)-Fn(a)
for ni in range(1,11):
trapeziod=Trapezoids_R(fn,a,b,10*ni)
simpson=Simpsons_R(fn,a,b,5*ni)
text=[
"%d" % (10*ni),
"%.6f" % analytical,
"%.6f" % trapeziod,
"%.6e" % abs( (trapeziod-analytical)/analytical ),
"%.6f" % simpson,
"%.6e" % abs( (simpson-analytical)/analytical ),
]
print "\t".join(text)
|
f_0:
ni Ana Trapeziod r Simpson r
10 0.632121 0.632647 8.331945e-04 0.632121 5.548949e-07
20 0.632121 0.632252 2.083247e-04 0.632121 3.471189e-08
30 0.632121 0.632179 9.259088e-05 0.632121 6.857803e-09
40 0.632121 0.632153 5.208279e-05 0.632121 2.169977e-09
50 0.632121 0.632142 3.333311e-05 0.632121 8.888463e-10
60 0.632121 0.632135 2.314804e-05 0.632121 4.286551e-10
70 0.632121 0.632131 1.700674e-05 0.632121 2.313796e-10
80 0.632121 0.632129 1.302080e-05 0.632121 1.356313e-10
90 0.632121 0.632127 1.028804e-05 0.632121 8.467472e-11
100 0.632121 0.632126 8.333319e-06 0.632121 5.555448e-11
f_1:
ni Ana Trapeziod r Simpson r
10 0.264241 0.263408 3.152496e-03 0.264240 4.751650e-06
20 0.264241 0.264033 7.883470e-04 0.264241 2.973914e-07
30 0.264241 0.264149 3.503948e-04 0.264241 5.875911e-08
40 0.264241 0.264189 1.971007e-04 0.264241 1.859343e-08
50 0.264241 0.264208 1.261455e-04 0.264241 7.616186e-09
60 0.264241 0.264218 8.760145e-05 0.264241 3.673012e-09
70 0.264241 0.264224 6.436043e-05 0.264241 1.982627e-09
80 0.264241 0.264228 4.927604e-05 0.264241 1.162190e-09
90 0.264241 0.264231 3.893421e-05 0.264241 7.255538e-10
100 0.264241 0.264233 3.153673e-05 0.264241 4.760378e-10
f_2:
ni Ana Trapeziod r Simpson r
10 0.160603 0.160909 1.903980e-03 0.160606 1.941701e-05
20 0.160603 0.160679 4.769074e-04 0.160603 1.216635e-06
30 0.160603 0.160637 2.120340e-04 0.160603 2.404355e-07
40 0.160603 0.160622 1.192839e-04 0.160603 7.608777e-08
50 0.160603 0.160615 7.634610e-05 0.160603 3.116792e-08
60 0.160603 0.160611 5.301978e-05 0.160603 1.503144e-08
70 0.160603 0.160609 3.895404e-05 0.160603 8.113801e-09
80 0.160603 0.160608 2.982455e-05 0.160603 4.756238e-09
90 0.160603 0.160607 2.356527e-05 0.160603 2.969328e-09
100 0.160603 0.160606 1.908799e-05 0.160603 1.948193e-09
f_3:
ni Ana Trapeziod r Simpson r
10 0.113929 0.114543 5.390801e-03 0.113925 3.611625e-05
20 0.113929 0.114082 1.345996e-03 0.113929 2.272130e-06
30 0.113929 0.113997 5.980801e-04 0.113929 4.493611e-07
40 0.113929 0.113967 3.363924e-04 0.113929 1.422411e-07
50 0.113929 0.113953 2.152829e-04 0.113929 5.827341e-08
60 0.113929 0.113946 1.494989e-04 0.113929 2.810553e-08
70 0.113929 0.113941 1.098346e-04 0.113929 1.517164e-08
80 0.113929 0.113939 8.409143e-05 0.113929 8.893708e-09
90 0.113929 0.113937 6.644224e-05 0.113929 5.552457e-09
100 0.113929 0.113935 5.381800e-05 0.113929 3.643040e-09
f_4:
ni Ana Trapeziod r Simpson r
10 0.087836 0.088756 1.047122e-02 0.087836 3.277019e-06
20 0.087836 0.088066 2.617686e-03 0.087836 1.603420e-07
30 0.087836 0.087939 1.163407e-03 0.087836 3.003663e-08
40 0.087836 0.087894 6.544145e-04 0.087836 9.322396e-09
50 0.087836 0.087873 4.188247e-04 0.087836 3.784044e-09
60 0.087836 0.087862 2.908503e-04 0.087836 1.815847e-09
70 0.087836 0.087855 2.136859e-04 0.087836 9.772070e-10
80 0.087836 0.087851 1.636032e-04 0.087836 5.716976e-10
90 0.087836 0.087848 1.292667e-04 0.087836 3.564255e-10
100 0.087836 0.087846 1.047060e-04 0.087836 2.336200e-10
Output from: /usr/bin/python N04.py
|
|
Messages:
0 secs.
|