Professional Documents
Culture Documents
0.1
0.1.1
0.1.2
Usando las relaciones de recurrencia Pn+1 = 2x Pn (x) Pn1 (x). Se tomarn los
polinmios P0 (x) = 1 y P1 (x) = 2x
1.1
Interpolacin
In [131]: def Chevi2(n):
x = symbols("x")
P1 = 1
P2 = 2*x
if n==0:
return 1
else:
if n<0:
return "n debe ser mayor o igual a cero"
else:
if n==1:
P=P1
else:
if n==2:
P=P2
else:
for i in range (2,n):
P= 2*x*P2-P1
P2=P
P1=P2
return P
1
1.0.1
Definir Polinomio
In [53]: P=Chevi2(4)
expand(P)
Out[53]:
8x3 4x2 2x + 1
1.0.2
1.2
Interpolacin
In [54]: x = symbols("x")
X=np.linspace(-1,1,11)
Y=np.linspace(-1,1,11)
for i in range (0,len(X)):
Y[i]=P.subs({x:X[i]})
fig, ax = plt.subplots()
ax.plot(X, Y, 'b', label='P', linewidth=1.0)
legend = ax.legend(loc=1, shadow=True,fontsize=9)
ax.set_xlabel('x')
ax.set_ylabel('y')
Out[54]: <matplotlib.text.Text at 0x7fd82d543a90>
1.3
Interpolacin
3.1
3.1.1
INTERPOLACIN
LAGRANGE
In [56]: L = symbols("L")
x = symbols("x")
L=0;
for i in range (0,len(X)):
k=1
for j in range(0,len(X)):
if i!=j:
k=k*((x-X[j])/(X[i]-X[j]))
L=L+Y[i]*k
3.1.2
LINEAL SEGMENTARIA
In [57]: M=[]
for i in range(0,len(X)-1):
M.append(0)
In [58]: for i in range (0,len(M)):
M[i] = Y[i]+((Y[i+1]-Y[i])/(X[i+1]-X[i]))*(x-X[i])
In [59]: M
Out[59]:
[24.72x + 15.72,
15.44x + 8.296,
8.08x + 3.88,
2.64x + 1.704,
0.880000000000001x + 1.0,
2.48x + 1.0,
3.1.3
CUADRATICA SEGMENTARIA
In [60]: c=[]
for i in range(0,len(X)-2):
c.append(0)
1.4
Interpolacin
4.0.1
In [63]: Y1=np.linspace(-1,1,200)
X1=np.linspace(-1,1,200)
for i in range (0,len(X1)):
Y1[i]=L.subs({x:X1[i]})
fig, ax = plt.subplots()
ax.plot(X1, Y1, 'b:', label='P lagrange', linewidth=1.0)
ax.plot(Xex, Yex, 'g', label='P exact', linewidth=1.0)
legend = ax.legend(loc=1, shadow=True,fontsize=9)
ax.set_xlabel('x')
ax.set_ylabel('y')
Out[63]: <matplotlib.text.Text at 0x7fd825bb5908>
17.6x2 7.92x
4.0.2
In [64]: num=2000
X2=np.linspace(-1,1,num)
Y2=np.zeros(len(X2))
a=int(np.round(num/len(M)))
for i in range (0,len(M)):
for j in range (0,a):
Y2[i*a+j]=M[i].subs({x:X2[i*a+j]})
fig, ax = plt.subplots()
ax.plot(X2, Y2, 'b', label='Spline Lineal', linewidth=1.0)
ax.plot(Xex, Yex, 'g', label='P exact', linewidth=1.0)
legend = ax.legend(loc=1, shadow=True,fontsize=9)
ax.set_xlabel('x')
ax.set_ylabel('y')
Out[64]: <matplotlib.text.Text at 0x7fd82620b1d0>
4.0.3
In [65]: num=2000
X3=np.linspace(-1,1,num)
Y3=np.zeros(len(X3))
a=int(np.round(num/(len(c)+1)))
for i in range (0,len(c)):
for j in range (0,a):
if i==len(c)-1:
Y3[i*a+j]=c[i].subs({x:X3[i*a+j]})
Y3[i*a+j+a]=c[i].subs({x:X3[i*a+j+a]})
else:
Y3[i*a+j]=c[i].subs({x:X3[i*a+j]})
fig, ax = plt.subplots()
ax.plot(X3, Y3, 'b', label='Spline Cuadrtica', linewidth=1.0)
ax.plot(X, Y, 'bo', label='Spline Cuadrtica', linewidth=1.0)
ax.plot(Xex, Yex, 'g', label='P exact', linewidth=1.0)
legend = ax.legend(loc=1, shadow=True,fontsize=9)
ax.set_xlabel('x')
ax.set_ylabel('y')
Out[65]: <matplotlib.text.Text at 0x7fd825b7a898>
6
1.5
Interpolacin
5.0.1
In [66]: x = symbols("x")
X=np.linspace(-1,1,11)
Y=np.linspace(-1,1,11)
error=np.linspace(-1,1,11)
for i in range (0,len(X)):
Y[i]=P.subs({x:X[i]})
e=np.max(np.abs(Y))/5
for i in range (0,len(X)):
error[i]=e*(np.random.rand(1)[0]-0.5)
Yerror=np.linspace(-1,1,11)
Yerror=Y+error
fig, ax = plt.subplots()
ax.plot(X, Yerror, 'bo', label='P + error', linewidth=1.3)
5.0.2
Prueba Lagrange
In [67]: Le = symbols("Le")
x = symbols("x")
Le=0;
for i in range (0,len(X)):
k=1
for j in range(0,len(X)):
if i!=j:
k=k*((x-X[j])/(X[i]-X[j]))
Le=Le+Yerror[i]*k
Y1=np.linspace(-1,1,200)
X1=np.linspace(-1,1,200)
for i in range (0,len(X1)):
Y1[i]=Le.subs({x:X1[i]})
fig, ax = plt.subplots()
ax.plot(X, Yerror, 'bo', label='P + error', linewidth=1.3)
ax.plot(X1, Y1, 'b', label='Aprox', linewidth=1.0)
ax.plot(Xex, Yex, 'g', label='P exact', linewidth=1.0)
legend = ax.legend(loc='best', shadow=True,fontsize=9)
ax.set_xlabel('x')
ax.set_ylabel('y')
Out[67]: <matplotlib.text.Text at 0x7fd8260715f8>
5.0.3
In [68]: Me=[]
for i in range(0,len(X)-1):
Me.append(0)
for i in range (0,len(Me)):
Me[i] = Yerror[i]+((Yerror[i+1]-Yerror[i])/(X[i+1]-X[i]))*(x-X[i])
num=2000
X2=np.linspace(-1,1,num)
Y2=np.zeros(len(X2))
a=int(np.round(num/len(Me)))
5.0.4
In [180]: ce=[]
for i in range(0,len(X)-2):
ce.append(0)
10
num=200
X10=np.linspace(-1,1,num)
Y10=np.zeros(len(X10))
a=int(np.round(num/(len(ce)+1)))
for i in range (0,len(ce)):
for j in range (0,a):
if i==len(c)-1:
Y10[i*a+j]=c[i].subs({x:X10[i*a+j]})
Y10[i*a+j+a]=c[i].subs({x:X10[i*a+j+a]})
else:
Y10[i*a+j]=ce[i].subs({x:X10[i*a+j]})
fig, ax = plt.subplots()
ax.plot(X, Yerror, 'bo', label='P + error', linewidth=1.3)
ax.plot(X10, Y10, 'b', label='Aprox', linewidth=1.0)
ax.plot(Xex, Yex, 'g', label='P exact', linewidth=1.0)
legend = ax.legend(loc='best', shadow=True,fontsize=9)
ax.set_xlabel('x')
ax.set_ylabel('y')
Out[180]: <matplotlib.text.Text at 0x7fd825db7780>
11
1.6
Interpolacin
6.0.1
6.0.2
The interp1d class in scipy.interpolate is a convenient method to create a function based on fixed
data points which can be evaluated anywhere within the domain defined by the given data using
linear interpolation.
In [70]: from scipy.interpolate import interp1d
x1 = X
y = Yerror
f = interp1d(x1, y)
f2 = interp1d(x1, y, kind='cubic')
12
6.1
INTEGRACIN
(m)
Dentro del intervalo de definicion de su polinomio calcule los valores de los integrales In =
Rb
a W (x)Pn (x)Pn+1 (x)dx, para n = 1 a 5, m =0 a 5 usando cuadraturas de Trapecios, de Simpson y
de Gauss con seleccion de paso de integracion automtico en la base del algoritmo de Runge.
1.1
Integracin
7.0.1
TRAPECIOS
7.0.3
Funcin
13
7.1
7.1.1
SIMPSON
Funcin Simpson
for i in range(len(X3)):
n=len(X3)
if (i==0)or(i==len(X3)-1):
I=I+Y3[i]
else:
if ((i+2)%3==0)or((i+1)%3==0):
I=I+3*Y3[i]
else:
I=I+2*Y3[i]
I1=(3/8)*((b-a)/(n-1))*I
return I1,X3,Y3
In [143]: Ix,X5,Y5=simpson38(-1,1,200,Px)
Ix
Out[143]:
0.00108820176413
7.1.2
CUADRATURA GAUSS
1.2
Integracin
8.0.1
Trapecios
In [144]: X7,Y7=evalf(-1,1,11,Px)
import scipy.integrate as integrate
integrate.trapz(Y7,X7)
Out[144]:
5.55111512313e 17
8.0.3
Simpson
In [145]: integrate.simps(Y7,X7)
Out[145]:
0.0
15
8.0.4
Gauss
In [149]: init_printing(use_latex=True)
expand(Px)
Out[149]:
8x3 x2 + 1
0.5
2x x2 + 1
0.5
1.3
Integracin
Se comprob que al utilizar los algoritmos propios o las libreras correspondientes de python
para esos mismos mtodos los resultados obtenidos son similares con un error menor al 1%
9.1
10
1.1
11
1.2
1)
11.0.1
Mtodo de biseccin
In [191]: n=3
P=Chevi2(n+1)
In [185]: def bis(a,b,P):
i=0
while i == 0:
c=(a+b)/2
m=P.subs({x:c})
if abs(m)<0.00001:
r=c
i=1
else:
if (P.subs({x:c}))*(P.subs({x:a}))<0:
b=c
else:
a=c
return r
In [192]: x = symbols("x")
a=-1
b=0
Xex5,Yex5=evalf(a,b,500,P)
fig, ax = plt.subplots()
ax.plot(bis(a,b,P),P.subs({x:bis(a,b,P)}), 'bo',label='raz', linewidth=1
ax.plot(Xex5, Yex5, 'g:', label='P exact', linewidth=1.0)
legend = ax.legend(loc=1, shadow=True,fontsize=9)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True)
bis(-1,0,P)
Out[192]:
0.5
17
11.0.3
Mtodo de Newton-Raphson
fig, ax = plt.subplots()
ax.plot(newton(P,pnt),P.subs({x:newton(P,pnt)}), 'bo',label='raz', linew
ax.plot(Xex5, Yex5, 'g:', label='P exact', linewidth=1.0)
legend = ax.legend(loc=1, shadow=True,fontsize=9)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True)
r
Out[227]:
0.5009192158879139
12
1.3
In [219]: expand(P)
Out[219]:
8x3 4x2 2x + 1
19
0.49999999,
0.50000001])
Asegrese que las races del polinomio Pn (x) se ubican entre las races del polinomio Pn+1 (x) y
las races del polinomio Pn+1 (x) se ubican entre las races del polinomio Pn+2 (x)
12.0.3
Para P5 (x)
In [251]: expand(Chevi2(5))
Out[251]:
16x4 16x3 + 4x 1
In [264]: #programa propio
ceros(-10,10,1000,Chevi2(5))
Out[264]:
([0.510510510511,
0.49049049049] ,
2)
0.50000212 +3.67585920e-06j,
0.49999576 +0.00000000e+00j])
Para P6 (x)
In [254]: expand(Chevi2(6))
Out[254]:
32x5 48x4 + 16x3 + 8x2 6x + 1
In [265]: #programa propio
ceros(-10,10,1000,Chevi2(6))
Out[265]:
([0.510510510511] ,
1)
12.0.5
Para P7 (x)
In [256]: expand(Chevi2(7))
Out[256]:
64x6 128x5 + 80x4 20x2 + 8x 1
In [266]: #programa propio
ceros(-10,10,1000,Chevi2(7))
Out[266]:
([0.510510510511,
0.49049049049] ,
2)
0.50025458+0.00018523j,
0.49990251+0.00029893j,
0.49968583+0.j
])
In [259]: expand(Chevi2(10))
Out[259]:
512x9 1792x8 + 2560x7 1792x6 + 448x5 + 224x4 224x3 + 80x2 14x + 1
In [269]: #programa propio
ceros(-10,10,1000,Chevi2(10))
Out[269]:
([0.510510510511] ,
1)
21
0.50601453+0.j
,
0.50431021-0.00423207j,
0.50007155-0.00609521j,
0.49569025-0.00437563j,
0.49384145+0.j
12.0.7
In [261]: expand(Chevi2(11))
Out[261]:
1024x10 4096x9 + 6912x8 6144x7 + 2688x6 672x4 + 384x3 108x2 + 16x 1
In [270]: #programa propio
ceros(-10,10,1000,Chevi2(11))
Out[270]:
([0.510510510511,
0.49049049049] ,
2)
0.50846515+0.00295909j,
0.50481232+0.00774775j,
0.49869975+0.00926231j,
0.49282637+0.00628835j,
0.49039283+0.j
])
In [272]: expand(Chevi2(12))
Out[272]:
2048x11 9216x10 +17920x9 19200x8 +11520x7 2688x6 1344x5 +1440x4 600x3 +140x2 18x+1
In [273]: #programa propio
ceros(-10,10,1000,Chevi2(12))
Out[273]:
([0.510510510511] ,
1)
0.52187862+0.00727659j,
0.51310613+0.01874054j,
0.49950072+0.02256568j,
0.48658484+0.01779069j,
0.47892969+0.00668902j,
13
1.4
Se observa que todos los polinomios tienen las mismas races reales, los polinomios impares tienen
2 races reales y los pares una sola, los programas propios predicen las misma races pero nicamente las reales, con una precicin aceptable.
23