Capitulo 2
Capitulo 2
Capitulo 2
2
A menudo se conocen los valores de la función f(x) en x0, x1, x2,...., xn, pero se
desconoce la expresión de f(x). La estimación de f(x), se conoce como
interpolación.
Tabla 2.1
function InterpDemo()
% Demo Interpolacion
% E. Raffo Lecca
%
x=[0 1 4 6];
y=[1 -1 1 -1];
x0 = 0:0.10:6;
ylin = interp1(x,y,x0,'linear');
yspline = interp1(x,y,x0,'spline');
%ycubic = interp1(x,y,x0,'cubic');
plot(x,y,'o');
hold on;%para colocar varios plot;por default es off
plot(x0,ylin,x0,yspline);
legend('dato','lineal','spline',0);
%pos = 0,1,2,3,4,-1; % 0 = el mejor lugar o automático
title('Interpolación');
x0 x1 x2 … xn
y0 y1 y2 … yn
ϕ ( x ; a0 , a1 ,..., an ) ,
Donde existen (n+1) coeficientes: a0, a1, a2,..., an. El problema radica en
determinar los parámetros ai para los (n+1) pares de números (xi, fi), i = 1,...,
n con xi ≠ xk para i ≠ k , con la condición:
ϕ ( xi ; a0 , a1 ,..., an ) = f i , i = 0,..., n
ϕ ( x ; a0 , a1 ,..., an ) = a0 + a1 x + a2 x 2 + ... + an x n
f(x1)
P(x)
f(x0)
x0 x x1
Figura 2.3: Interpolación lineal
53
f ( x1 ) − f ( x0 ) P ( x) − f ( x0 )
=
x1 − x0 x − x0
( x − x0 )( f ( x1 ) − f ( x0 ))
P ( x) = f ( x0 ) +
( x1 − x0 )
x − x1 x − x0
P ( x) = f ( x0 ) + f ( x1 )
x0 − x1 x1 − x0
x1
x2
P(x)
x0
f(x) xn
x3
Los (n+1) puntos de x, donde existe el valor de f (x), serán utilizados para
construir un polinomio P(x) de grado ≤ n que interpola a f (x) en los puntos x0,
x1, x2,..., xn, y satisface:
= ( x − x0 )( x − x1 )...( x − xk −1 )( x − xk +1 )...( x − xn )
54
Además de:
g k ( xk ) = ∏ ( xk − xi )
i =0
i≠k
g k ( x)
Lk ( x) =
g k ( xk )
x − xi
=∏
i =0 xk − xi
i≠k
1 i=j
Li ( x j ) = δij =
0 i≠j
n n
P( x j ) = ∑ Li ( x j ) yi = ∑ yiδij = y j
i =0 i =0
n
P ( x) = ∑ f ( xk ) Lk ( x)
k =0
xi 0 1 4 6
yi 1 −1 1 −1
( x − x1 )( x − x2 )( x − x3 )
L0 ( x) =
( x0 − x1 )( x0 − x2 )( x0 − x3 )
( x − x0 )( x − x2 )( x − x3 )
L1 ( x) =
( x1 − x0 )( x1 − x2 )( x1 − x3 )
( x − x0 )( x − x1 )( x − x3 )
L2 ( x) =
( x2 − x0 )( x2 − x1 )( x2 − x3 )
( x − x0 )( x − x1 )( x − x2 )
L3 ( x ) =
( x3 − x0 )( x3 − x1 )( x3 − x2 )
i f (xi) Li (x)
0 1 -0.33333
1 -1 1.06667
2 1 0.33333
3 -1 -0.06667
P(x) = -1
i xi yi
0 1.05 1.02470
1 1.10 1.04881
2 1.15 1.07238
3 1.20 1.09544
56
El cálculo
de P(x), se presenta en la tabla:
I f (xi) Li (x)
0 1 -0.25000
1 -1 0.60000
2 1 0.75000
3 -1 -0.10000
i f (xi) Li (x)
0 1.02470 -0.064
1 1.04881 0.672
2 1.07238 0.448
3 1.09544 -0.056
function p=Lagrange(x,y,x0)
% Interpolacion por Lagrange
% E. Raffo Lecca
% Datos
% x =es el vector x
% y =es el vector f(x)
% n =el orden de la matriz
% Resultados
% L =es el polinomio de lagrange
% p =valor interpolado
n = length(x);
L = zeros(n,1);
fprintf(' k x y L\n');
for k=1:n
prod1=1;prod2=1;
for i=1:n
if i~=k
prod1=prod1*(x0-x(i));
prod2=prod2*(x(k)-x(i));
end
end
L(k)=prod1/prod2;
end
p = 0;
for k=1:n
p = p+L(k)*y(k);
fprintf('%5d%10.6f%10.6f%10.6f\n',k,x(k),y(k),L(k));
end
π( x) = ( x − x0 )( x − x1 )( x − x2 )...( x − xn )
π' ( xi ) = ( xi − x0 )( xi − x1 )( xi − x2 )...( xi − xn )
n
Pn ( x) = ∑Lk ( x) f ( xk )
k =0
n
π ( x)
=∑ f ( xk )
k =0 ( x − xk )π ' ( xk )
Donde:
π ( x)
Li ( x ) =
( x − xi )π ' ( xi )
( x − x0 )( x − x1 )...( x − xi −1 )( x − xi +1 )...( x − xn )
=
( xi − x0 )( xi − x1 )...( xi − xi −1 )( xi − xi +1 )...( xi − xn )
h = xi +1 − xi
x = x0 + hs , xi = x0 + hs i
( s − s0 )( s − s1 )...( s − si −1 )( s − si +1 )...( s − sn )
Li ( x) =
( si − s0 )( si − s1 )...( si − si −1 )( si − si +1 )...( si − sn )
( s + m)( s + m −1)...( s − i +1)( s − i −1)...( s − m +1)( s − m)
=
(i + m)( i + m −1)...( 2)(1)( −1)( −2)...( i − m +1)( i − m)
Entonces:
(1 − s 2 )(4 − s 2 )...(m 2 − s 2 )
L0 ( s) = ,y
(m!) 2
Li ( s) =
(− 1)i+1 s( s + i)
(m + i)!(m − i)!
[
(1 − s 2 )(4 − s 2 )...((i − 1) 2 − s 2 )((i + 1) 2 − s 2 )...(m 2 − s 2 ) ]
Li ( −s ) = L−i ( s )
s L−1 ( s ) L0 ( s ) L1 ( s )
0.0 0 1 0 0.0
0.1 -0.045 0.99 0.055 -0.1
0.2 -0.080 0.96 0.120 -0.2
0.3 -0.105 0.91 0.195 -0.3
0.4 -0.120 0.84 0.280 -0.4
0.5 -0.125 0.75 0.375 -0.5
60
h =1.17 −1.16
De x = a + sh , con a =1.16
1.164 −1.16
s= = 0.4
0.01
h =1.16 −1.15
De x = a + sh , con a =1.15
1.142 −1.15
s= = −0.8
0.01
Pn ( x) = b0 + b1 ( x − x0 ) + b2 ( x − x0 )( x − x1 ) + ...
... + bn ( x − x0 )( x − x1 )....( x − xn −1 )
f 0 = Pn ( x0 ) = b0
f1 = Pn ( x1 ) = b0 + b1 ( x − x0 )
f 2 = Pn ( x2 ) = b0 + b1 ( x − x0 ) + b2 ( x − x0 )( x − x1 )
...
Luego se definen:
f1 − f 0
f 01 =
x1 − x0
f 2 − f1
f12 =
x2 − x1
f12 − f 01
f 012 =
x2 − x0
El polinomio es equivalente a:
Pn ( x) = f 0 + f 01 ( x − x0 ) + f 012 ( x − x0 )( x − x1 ) + ...
... + f 012 ... k ( x − x0 )( x − x1 )...( x − xk −1 )
b0 = f ( x0 )
b1 = f [ x0 , x1 ]
b2 = f [ x0 , x1 , x2 ]
...
bn = f [ x0 , x1 ,..., xn ]
62
f ( x j ) − f ( xi )
f [ xi , x j ] =
x j − xi
f [ x j , xk ] − f [ xi , x j ]
f [ xi , x j , xk ] =
xk − xi
Y en general:
f [ x1 , x2 ,..., xk ] − f [ x0 , x1 ,..., xk −1 ]
f [ x0 , x1 , x2 ,..., xk ] =
xk − x0
k =1 k =2 k
x0 f0 f 01 f 012 f 0123
x1 f1 f12 f123
x2 f2 f 23
... ...
xn fn
f ( x1 ) − f ( x0 ) 1.3862944
f [ x0 , x1 ] = = = 0.46209813
x1 − x0 4 −1
1.7917595 − 1.3862944
f [ x1 , x2 ] = = 0.20273255
6−4
1.6094379 − 1.7917595
f [ x2 , x3 ] = = 0.18232160
5−6
f [ x1 , x2 ] − f [ x0 , x1 ]
f [ x0 , x1 , x2 ] = = −0.05187312
6 −1
f [ x2 , x3 ] − f [ x1 , x2 ]
f [ x1 , x2 , x3 ] = = −0.02041095
5−4
f [ x1 , x2 , x3 ] − f [ x0 , x1 , x2 ]
f [ x0 , x1 , x2 , x3 ] = = 0.00786554 ,
5 −1
i xi f (xi) 1 2 3
i 0 1 2 3
xi 0 1 4 6
yi 1 −1 1 −1
i xi f (xi) 1 2 3
1 1 -1 0.66666667 -0.33333333
2 4 1 -1.0
3 6 -1
function p=InterpolNewton(x,y,x0)
% Interpolacion por Newton
% E. Raffo Lecca
%
n=length(x);
T=zeros(n,n);
for i=1:n
T(i,1)=y(i);
end
%T(:,1)=y';
for j=2:n
for i=1:n+1-j
T(i,j)=(T(i+1,j-1)-T(i,j-1))/(x(i+j-1)-x(i));
end
end
%Evaluar P(x)
suma=0;
producto=1;
for j=1:n
suma=suma+T(1,j)*producto;
producto=producto*(x0-x(j));
65
end
p=suma;
%impresion
fprintf(' X Difer. Divididas\n');
fprintf('==========');
for j=1:n
fprintf('==========');
end
fprintf('\n');
for i=1:n
fprintf('%10.3f',x(i));
for j=1:n+1-i
fprintf('%10.3f',T(i,j));
end
fprintf('\n');
end
cn = bn
ck = bk + ( x-x k )bk +1 , k = n −1, n − 2,..., 0
k xk bk ck
3 6 -0.16666667 -0.16666667
2 4 0.66666667 1
1 1 -2.0 -1
0 0 1 -1
Pi ( x ) = f i
( x − x i0 ) Pi1 ... ik ( x ) − ( x − x ik ) Pi0 i1 ... ik −1 ( x )
Pi0 i1 ... ik ( x ) =
xik − x i0
k =0 k =1 2 3
x0 f 0 = P0 ( x)
P01 ( x )
x1 f1 = P1 ( x) P012 ( x )
P12 ( x) P0123 ( x)
x2 f 2 = P2 ( x) P123 ( x)
P23 ( x )
x3 f 3 = P3 ( x)
( x − x0 ) P12 ( x) − ( x − x2 ) P01 ( x)
P012 ( x) =
x2 − x0
Determinar P0123(2):
67
k =0 1 2 3
0 1
−3
1 −1 1
1 1
2 1 1
1
3 −1
( 2 − 0)(−1) − (2 − 1)(1)
P01 (2) = = −3
1− 0
(2 − 1)(1) − (2 − 2)(−1)
P12 (2) = =1
2 −1
(2 − 2)(−1) − (2 − 3)(1)
P23 (2) = =1
3− 2
(2 − 0)(1) − ( 2 − 2)(−3)
P012 (2) = =1
2−0
(2 − 1)(1) − (2 − 3)(1)
P123 (2) = =1
3 −1
(2 − 0)(1) − (2 − 3)(1)
P0123 (2) = =1
3−0
Ti + k , k = Pi , i +1,..., i + k
68
x0 f 0 = T00
T11
x1 f1 = T10 T22
T21 T33
x2 f 2 = T20 T32
T31
x3 f 3 = T30
Donde:
Ti 0 = f i
( x − xi − k ) Ti , k −1 − ( x − xi ) Ti −1, k −1
Tik =
( xi − xi − k )
Ti , k −1 − Ti −1, k −1
= Ti , k −1 + , 1 ≤ k ≤ i , i = 0,1,..., n
x − xi −k
−1
x − xi
f i = f ( xi ) , i = 0, 1, ..., n
Los cuales son interpolados. Una interpolación polinomial P(x) =P0...n(x) con
P ( xi ) = f i , i = 0, 1, ..., n
Teorema:
Si una función f tiene (n+1) derivadas; entonces para cada argumento X ,
existe un número
ξ en el más pequeño intervalo I [xo, ...., xn, X ] que contiene a X y a todas
las abscisas xi, satisfaciendo:
π( x) f ( n +1)
(ξ )
f ( x ) − P01 ... n ( x ) =
( n +1)!
π( x ) = ( x − x0 )( x − x1 )....( x − xn )
Así:
f ( x) =cos x
iπ
xi = , i = 0,1,2,3,4,5 o n =5
10
cos ξ
f ( x) − P ( x ) = ( x − x0 )( x − x1 )...( x − x5 )
720
| π( x ) |
f ( x) −P ( x ) ≤ , donde ξ ∈I [ x0 ,...., xn , x)
720
f ( x ) = Pn ( x) + Rn ( x )
Pn ( x) = f [ x0 ] + ( x − x 0 ) f [ x 0 x1 ] + ( x − x 0 )( x − x1 ) f [ x0 x1 x 2 ] + ...
+ ( x − x 0 )( x − x1 )...( x − x n −1 ) f [ x 0 x1 x 2 ... x n ]
70
Y residuo
Rn ( x ) = ( x − x0 )( x − x1 )...( x − x n −1 )( x − xn ) f [ x0 x1 x2 ... xn x n +1 ] ;
para el caso de existir el punto ( x n +1 , f ( x n +1 )) .
n
Rn ( x) = ∏[( x − xi )] f [ x0 x1 x 2 ... x n x n +1 ]
i =0
Teorema de Rolle:
Luego:
f ( x ) = Pn ( x ) + Rn ( x ), y
Q (t ) = f (t ) − Pn (t ) − Rn ( x )
Rn ( x) = π( x ) K
Entonces:
n
Q (t ) = f (t ) − Pn (t ) − ∏ ( x − xi ) K
i =0
Q ( n+1) (t ) = f ( n +1)
(t ) − P ( n+1) (t ) −( n +1)! K
de donde:
71
( n +1)
f (t ) −( n +1)! K = 0
f ( n+1) (t )
K =
( n +1)!
Rn ( x ) = π ( x ) K
f ( n+1) (t )
= π ( x)
(n +1)!
Teorema:
Si xo,..., xn son (n+1) puntos distintos contenidos en el intervalo
[a,b] y f (ξ) es una función derivable n+1 veces en (a, b), entonces:
π( x ) f ( n +1)
(ξ )
f ( x) − P ( x) ∞ ≤
( n +1)!
Por ejemplo:
72
h2
π1 ( x) ∞[ 0,1] = h 2 t (t −1) ∞ =
[ 0 ,1 ] 4
2h 3
π2 ( x) ∞[ 0, 2 ] = h 3 t (t −1)( t − 2) ∞[ 0, 2 ] =
3 3
π3 ( x) ∞[ 0,3] = h t (t −1)( t − 2)( t − 3) ∞[ 0,3] = h 4
4
( n +1)
f
f ( x ) −P ( x ) ≤ ∞
h ( n+1)
∞
4( n +1)
1
Por ejemplo, para f ( x) = , aproximado a una cuadrática:
2 x
1
f ( x) = , con x ∈[θ ,1], y n = 2
2 x
15 1 −θ
f (ξ) = − ξ −7 / 2 , h =
( 3)
16 2
(1 − θ ) 3
15
f ( x) − P ( x ) ∞ ≤
2 .3 16 θ 7 / 2
5
∏ ={ pj n ( x ), n ≤ j }
b
( f , g ) = ∫ w( x ) f ( x ) g ( x ) dx
a
( pi , p j ) = 0, si i ≠ j
b
( p, q ) = ∫ w( x ) pn ( x) qn −1 ( x) dx = 0
a
min max f ( x ) − Pn ( x)
p a ≤x ≤b
π( x) f ( n +1)
(ξ )
Rn ( x ) =
( n +1)!
f ( x) = a0 + a1 x + a2 x 2 + ... + x n +1 ,
2.2.3 Polinomios Tn
En Chebyshev para el intervalo (-1,1), la función de peso es:
75
1
w( x) =
1 − x2
1
pn ( x ) qn −1 ( x )
∫
−1
1 − x2
dx = 0
∫p
0
n (cos θ ) qn −1 (cos θ ) dθ = 0
∫p
0
n (cos θ )(cos kθ ) dθ = 0, k = 0,1,..., n −1
pn ( x) = A cos nθ
pn ( x ) = cos( n cos −1 x) = Tn ( x )
1
Tr ( x )Tk ( x )
∫
−1 1 − x2
dx = 0, r ≠ k
76
Pn ( x) = a0 + a1 x + a2 x 2 + ... + an x n
b −a z −a
=
1 − (−1) x − ( −1)
2z −b − a
x= , z ∈[ a, b] ,
b −a
T0 =1
Tn ( x ) = cos( nθ ), n =1,2,...
cos θ = cos(arccos x) = x
T1 ( x) = x
cos 2θ = 2 cos 2 θ −1
T2 ( x) = 2( cos θ ) −1
2
= 2 x 2 −1
-1 1 x
Figura 2.9
T3 ( x ) = 2 xT 2 ( x ) −T1 ( x )
= 2 x ( 2 x 2 −1) − x
= 4 x 3 −3x
Tn Expresión
T0 1
T1 x
T2 2x2 - 1
T3 4x3 - 3x
T4 8x4 - 8x2 + 1
T5 16x5 - 20x3 +5x
79
Figura 2.10
Teorema:
max 2 −n Tn +1 ( x ) = 2 −n . max Pn +1 ( x )
−1≤x ≤1 −1≤x ≤1
2 −n Tn+1 ( x ) =2 −n Tn+1 ( x ) ∞
=2 −n
∞
Se cumple que:
max π( x) ≤ max Pn +1 ( x)
−1≤x ≤1 −1≤x ≤1
Figura 2.11
πn ( x) ∞[ −1,1] = 2 −n Tn+1 ( x) ∞ = 2 −n .
Figura 2.12
( 2k −1)π
xk = cos , k =1,..., n
2n
− 1
xk = [ (b − a) x k + a + b]
2
83
function p=InterpolChebyshev(f,n,a,b,x0)
% Interpolacion por Chebyshev
% E. Raffo Lecca
%
n=n+1;
x=zeros(n,1);
y=zeros(n,1);
for k=1:n
i=k-1;
x(k)=cos(0.5*(2*i+1)*pi/n);
end
x=0.5*((b-a)*x+a+b);
for k=1:n
y(k)=f(x(k));
end
InterpolNewton(x,y,x0);
2.2.4 Ortogonalidad
Dos miembros de una función, se dice que son ortogonales con respecto a una
función de peso w, si su producto interno es cero.
π 0 ( m ≠ n)
∫0 cos mθ cos nθ dθ = π / 2 (m = n ≠ 0)
π ( m = n = 0)
1 0 ( m ≠ n)
Tm ( x )Tn ( x)
∫ 1−x2
dx = π / 2 ( m = n ≠ 0)
−1 π ( m = n = 0)
1
w( x ) =
1− x2
La propiedad
L( xi ) = yi , i = 0,1,2,..., n
Indica que
x − xi +1 x − xi , xi ≤ x ≤ xi +1
L ( x ) = yi + yi +1
xi − xi +1 xi +1 − xi , i = 0,1,2,...., n − 1
function HermiteLinDemo()
% Demo de Interpolacion Hermite Lineal
% E. Raffo Lecca
% Datos
% x = es el vector x
% y = es el vector f(x)
% xinterpol = valor de x a interpolar
% Resultados
% yinterpol = valor interpolado
x=[0 1 4 6];y=[1 -1 1 -1];
xinterpol=0:0.1:6;
%invocacion a Hermite Lineal
for i=1:length(xinterpol)
yinterpol(i)=HermiteLin(x,y,xinterpol(i));
end
%grafica
plot(x,y,'o',xinterpol,yinterpol);
grid on;
xlabel('X');ylabel('Y');
title('comparacion con la interpol de Hermite Lineal');
legend('real','interpol Hermite Lineal',2);
pi ( x ) y x ∈[ xi , xi +1 ] ∀i = 0,1,..., n −1
Definida por:
89
C ( xi ) = f ( xi ), i = 0,1,2,..., n
De donde se deduce que:
pi ( xi ) = f ( xi ), i = 0,1, 2,..., n −1
pi ( xi + 1) = f ( xi + 1), i = 0,1,2,..., n −1
1 2 3
xi fi f[xi xi] f[xixi xi+1] f[xixixi+1xi+1]
xi fi f[xi xi+1] f[xixi+1 xi+1]
xi+1 fi+1 f[xi+1 xi+1]
xi+1 fi+1
Para evaluar f[xixi], se hace uso del concepto de la derivada como una
aproximación; así para ∆xi muy pequeño, se tiene:
f ( xi + ∆xi ) − f ( xi )
f [ xi x i] = lim Δx →0 = f ' ( xi )
∆xi
Luego
b 0 = f ( xi )
b1 = f ' ( xi )
f [ xixi + 1] − b1
b2 =
∆xi
f ' ( xi + 1) − 2 f [ xixi + 1] + b1
b3 =
∆x 2 i
90
function valor=Hermite(x,y,yderiv,x0)
% Interpolacion segementaria por Hermite
% E. Raffo Lecca
%
% Datos
% x =es el vector x
% y =es el vector f(x)
% yderiv=es el vector de derivadas
% x0 =valor de x a interpolar
% n =el numero de segmentos
% np =numero de polinomios cubicos p
% Resultados
% C =es el conjunto de todos los polinomios p
% valor =valor interpolado
np=length(x);
n=np-1;
for i=1:n
C(1,i)=y(i);
C(2,i)=yderiv(i);
dx=x(i+1)-x(i);
difer=(y(i+1)-y(i))/dx;
C(4,i)=(yderiv(i+1)-2*difer+C(2,i))/dx/dx;
C(3,i)=(difer-C(2,i))/dx-C(4,i)*dx;
end
Ok=0;
if x0<x(1)
Ok=1;
j=1;
else
for i=2:np
if x0<x(i)
Ok=1;
j=i-1;
break;
end
end
end
if Ok==0
j=n;
end
difer = x0-x(j);
91
valor = C(1,j)+C(2,j)*difer+C(3,j)*difer^2+C(4,j)*(x0-
x(j+1))*difer^2;
function HermiteDemo()
% Demo de Interpolacion segementaria por Hermite
% E. Raffo Lecca
% Datos
% x =es el vector x
% y =es el vector f(x)
% xderiv =es el vector de derivadas
% xinterpol =valor de x a interpolar
% Resultados
% yinterpol =valor interpolado
x=0:pi/8:pi/2;y=cos(x);
xderiv=-sin(x);
xinterpol=0:pi/50:pi/2;
%invocacion a Hermite
for i=1:length(xinterpol)
yinterpol(i)=Hermite(x,y,xderiv,xinterpol(i));
end
%grafica
plot(x,y,'o',xinterpol,yinterpol);
grid on;
xlabel('angulo rad');
ylabel('cos');
title('comparacion cos y la interpolacion de Hermite');
legend('cos','interpol Hermite')
Definiendo:
f ( xi +1 ) − f ( xi )
mi = ,y
xi +1 − xi
f ( x ) − f ( xi )
mi =
x − xi
Se tiene la forma
f ( x ) = f ( xi ) + mi ( x − xi )
f ( x ) = f ( x 0 ) + m0 ( x − x 0 ) x 0 ≤ x ≤ x1
f ( x ) = f ( x1 ) + m1 ( x − x1 ) x1 ≤ x ≤ x 2
f ( x ) = f ( x n −1 ) + mn −1 ( x − x n −1 ) x n −1 ≤ x ≤ x n
f ( x ) = f ( x2 ) + m2 ( x − x2 ) x2 ≤ x ≤ x3
f ( x3 ) − f ( x 2 ) 0.5 − 2.5
m2 = = = −1
x3 − x 2 9.0 − 7.0
y0 + m0 ( x − x0 ) x ∈[ x0 , x1 ]
y + m (x − x ) x ∈[ x1 , x2 ]
1 1 1
S ( x) =
... ...
yn −1 + mn −1 ( x − xn −1 ) x ∈[ xn −1 , xn ]
f(x)
(x3,y3)
(x1,y1)
(x0,y0) (x2,y2)
x0 x1 x2 x3 x
Figura 2.23
function p=InteSpl1(x,y,x0)
% Interpolacion segmentaria spline lineal
% E. Raffo Lecca
%
% Datos
% n = numero de puntos
% x = vector x
% y = vector y
% x0 = valor a interpolar
% Resultados
95
% p = valor interpolado
n = length(x);
if x0<x(1)
p=y(1);
else
if x0<=x(n)
i=1;
while x0>x(i)
i=i+1;
end
if x0==x(i)
p=y(i);
else
m=(y(i)-y(i-1))/(x(i)-x(i-1));
p=y(i-1)+m*(x0-x(i-1));
end
else
p=y(n);
end
end
S i ( x ) = αi + βi ( x − xi ) + γ i ( x − xi ) 2 + δi ( x − xi ) 3
1.
para : x ∈[ xi , xi +1 ] , i = 0, 1, 2,..., n
x − xi +1 x − xi
S i´´ ( x ) = S ´´ ( xi ) + S ´´ ( xi +1 )
xi − xi +1 xi +1 − xi
x − xi +1 x − xi
S i´´ ( x ) = M i + M i +1 para x ∈[ xi , xi +1 ]
xi − xi +1 xi +1 − xi
( xi +1 − x ) 2 ( x − xi ) 2
S i´ ( x ) = −M i + M i +1 + Ai
2 hi +1 2 hi +1
( x − xi +1 ) 3 ( x − xi ) 3
S i ( x) = M i + M i +1 + Ai ( x − xi ) + Bi
6 hi +1 6 hi +1
con i = 0, 1, 2, ..., n − 1
2
h
M i i +1 + Bi = yi
6
2
h
M i +1 i +1 + Ai hi +1 + Bi = yi +1
6
98
2
h y − y i hi +1
Bi = y i − M i i +1 , Ai = i +1 − ( M i +1 − M i )
6 hi +1 6
S i ( xi ) = y i = α i , α i = yi
Mi
S i´´ ( xi ) = 2γ i = M i , γi =
2
M i hi +1 y − y i 2 M i + M i +1
S i´ ( xi ) = − + Ai = β i , β i = i +1 − hi +1
2 hi +1 6
M i +1 − M i M i +1 − M i
S i´´ ( xi ) = 6 δ i = , δi =
hi +1 6hi +1
Por cada [xi, xi+1] existen cuatro incógnitas, luego el spline cúbico
tiene aparentemente 4n incógnitas. Se plantean las siguientes ecuaciones:
− M i ( xi +1 − x) 2 ( x − xi ) 2 y i +1 − y i
S i´ ( x) = + M i +1 + −
2 hi +1 2 hi +1 hi +1
hi +1
( M i +1 − M i )
6
M h y − y i M i hi +1
S i´ ( xi +1 ) = i +1 i +1 + i +1 +
3 hi +1 6
− M i +1 ( x − xi +2 ) 2 M i +2 ( x − xi +1 ) 2 y i +2 − y i +1
S i´+1 ( x) = + + −
2 hi +2 2 hi +2 hi +2
hi +2
( M i +2 − M i +1 )
6
M h y − y i +1 M i +2 hi +2
S i´+1 ( xi +1 ) = − i +1 i +2 + i +2 −
3 hi +2 6
hi M i −1 hi + hi +1 h y − y y − yi −1
+ M i + i +1 M i +1 = i +1 i − i
6 3 6 hi +1 hi
i = 1,2,..., n − 1
S0´´(a) = M0 = 0 = Mn = Sn-1´´(b)
h1 h y −y
M 0 + 1 M 1 = 1 0 − y0´
3 6 h1
hn h y − yn −1
M n −1 + n M n = y´n − n
6 3 hn
hi +1
λi = , μ i = 1 − λi
hi + hi +1
6 yi +1 − yi yi − yi −1
bi = − i = 1, 2,..., n − 1
hi + hi +1 hi +1 hi
con:
6 y1 − y 0
λ 0 = 1 , b0 = − y 0´
h1 h1
6 y − y n −1
μ n = 1 , bn = y ´n − n
hn hn
2M 0 + λ 0 M 1 = b0
μ1M 0 + 2 M 1 + λ1M 2 = b1
...
μ n −1M n −2 + 2 M n −1 + λ n −1M n = bn −1
μ n M n −1 + 2M n = bn
En notación matricial:
101
2 λ0 0 ... 0 0 0 M 0 b0
μ1 2 λ1 ... 0 0 0 M 1 b1
... ... ... ... ... ... ... ... = ...
0 0 0 ... μ n −1 2 λ n −1 M n −1 bn −1
0 0 0 ... 0 μn 2 M n bn
ai = μ i
di = 2
ci = λ i , i = 0,1,2,..., n
Con a0 = 0 y cn = 0.
Para n =3 y los puntos (x,y) : (0,0), (1, 0.5), (2,3), (3,2) ; con la
modalidad natural se tienen los resultados siguientes:
x α β Γ Δ
(x0, x1) 0 -0.267 0 0.767
(x1, x2) 0.5 2.033 2.300 -1.833
(x2, x3) 3.0 1.133 -3.200 1.067
function p=InteSpl3(X,Y)
% Interpolacion segmentaria (spline) lineal
% E. Raffo Lecca
%
% Datos
% n =numero de puntos
% X =vector x
% Y =vector y
% x0=valor a interpolar
% Resultados
% p =valor interpolado
n=length(X);
while 1
option=input('Ingres opci¢n(1=Nor,2=Empal) -> ');
if (option>=1 & option<=2)
102
break;
end
end
if option==2
Y0Deriv=input('ingrese YDeriv[0] -> ');
YNDeriv=input('ingrese YDeriv[N] -> ');
end
%SplineCubic
for i=1:n-1
H(i+1)=X(i+1)-X(i);
end
for i=2:n-1
C(i)=H(i+1)/(H(i)+H(i+1));
D(i)=2;
A(i)=1-C(i);
b(i)=6*((Y(i+1)-Y(i))/H(i+1)-(Y(i)-Y(i-1))/H(i))/
(H(i)+H(i+1));
end
A(1)=0;
D(1)=2;
C(n)=0;
D(n)=2;
if option==1
b(1)=0;
C(1)=0;
b(n)=0;
A(n)=0;
else
C(1)=1;
b(1)=6*((Y(2)-Y(1))/H(2)-Y0Deriv)/H(2);
A(n)=1;
b(n)=6*(YNDeriv-(Y(n)-Y(n-1))/H(n))/H(n);
end
%TriDiagonal
M=Tridiagonal(A,D,C,b);
for i=1:n-1
Alpha(i)=Y(i);
Betha(i)=(Y(i+1)-Y(i))/H(i+1)-(2*M(i)+M(i+1))
*H(i+1)/6;
Gamma(i)=M(i)/2;
Delta(i)=(M(i+1)-M(i))/(6*H(i+1));
end
103
% EscribirVector
fprintf('\n');
fprintf(' Alpha Betha Gamma
Delta\n');
for i=1:n-1
fprintf('%10.3f%10.3f%10.3f%10.3f\n',Alpha(i),Betha(i),...
Gamma(i),Delta(i));
end
end
PROBLEMAS
1. Asumir f continua en [a,b] y x0 es un valor en Î [a,b]
2. Definiendo
n
P ( x) = ∏ ( x − xi )
i =0
Probar que:
Lk ( x) = ∏ ( x)
( x − x )∏ ´(x )
k k
i xi yi
0 6 104
1 10 160
2 20 370
Tm ( x ) =
a0 m
[
+ ∑ a j cos( jx ) + b j sen ( jx ) ,
2 j =1
]
2 n
aj = ∑ f ( xi ) cos( jxi )
n i =1
, j = 0,1,..., m
2 n
bj = ∑ f ( xi ) sen( jxi )
n i =1
, j = 1,2,..., m
yi = f ( xi )
xi = − π + 2 iπ / n i = 1,2,..., n
7.
Utilizar la función Lagrange.m para ilustrar el uso de este método.
y = a0 + a1 x + a2 x 2 + ... + an x n
Donde existen (n+1) pares de puntos (x,y); y se desea conocer los (n+1)
coeficientes de a. La solución al sistema de ecuaciones siguientes:
1 x0 x0 ... x0 a0 y0
2 n
1 x1 x1 ... x1 a1 y1
2 n
=
... ... ... ... ... ...
1 xn an yn
2 n
xn xn
Sugerencia:
12. En una interpolación con los pares (x, y), se quiere encontrar y(x) = c. El
caso de calcular x(y), y = c, éste es conocido como interpolación inversa.
107
xk yk
1 -4
2 -2
4 2
6 1
RESPUESTAS
1. P5(x) = x – x3 / 3! + x5 / 5! = 0.71739733
P5(x) = – x7 / 7! = - 0.00004161
P7(x) = x – x3 / 3! + x5 / 5! – x7 / 7! = 0.71735572
P7(x) = x9 / 9! = 0.0000003
n
2. De P ( x) = ∏ ( x − xi )
i =0
n
Luego P ´(x) = ∏ ( x − xi ) , y
i≠k
n
P ´(xk ) = ∏ ( xk − xi )
i≠k
De la definición del polinomio:
∏ (x − x ) i
L ( x) = i≠k
∏ (x − x )
k
k i
i≠k
Reemplazando:
108
∏ ( x − x )( x − x )
i k
∏ ( x)
L ( x) = i≠k
=
∏ ( x − x )( x − x ) ( x − x )∏´(x )
k
k i k k k
i≠k
i f (xi) Li(x)
0 1.024 0.01075
1 1.049 -0.08736
2 1.073 0.46592
3 1.095 0.69888
4 1.118 -0.09984
5 1.140 0.01165
i xi F(xi ) 1 2
0 6 104 14 0.50
1 10 160 21
2 20 370
5. Sea f(x) = x/2 sobre [-pi, pi], con n = 12 y m = 15. Los vectores resultantes:
6.
function griddataDemo
% Demo para gridData
% E. Raffo Lecca
x =rand(100,1)*4-2;
y =rand(100,1)*4-2;
z =x.*exp(-x.^2-y.^2);
t =-2:0.20:2;
[XI,YI] =meshgrid(t,t);
ZI =griddata(x,y,z,XI,YI);
109
mesh(XI,YI,ZI), hold
plot3(x,y,z,'o'), hold off
7.
function LagranDemo()
% Demo Interpolacion de Lagrange
% E. Raffo Lecca
x=[0 1 4 6]; y=[1 -1 1 -1];
x0=0:0.10:6;
for i=1:length(x0)
y0(i)=lagrange(x,y,x0(i));
end
plot(x,y,'o');
hold on;%para colocar varios plot;por default es off
110
plot(x0,y0);
legend('dato','Lagrange');
title('Interpolación de Lagrange');
9.
function x=rootCheby1(n)
111
Comentario:
10.
function x=rootCheby2(n)
% Raices de polinomio de Chebyshev
% E. Raffo Lecca
%
n=n+1;
if n==1
p=[1]; % p=1
else if n==2
p=[1,0];% p= 0+x
else
p2=[1];
p1=[1,0];
for k=3:n
p=2*[p1,0]-[0,0,p2];
p2=p1;
p1=p;
end
end
end
x=sort(roots(p));
12.
P (0) = 5.67
k xk bk ck
3 2 -0.167 -0.167
2 1 0.167 0.344
112
1 -2 0.5 1.168
0 -4 1 5.67
13.
function polyCheby()
% E. Raffo Lecca
% Compara función soporte
% entre puntos equidistantes y chebyshev
n=10;
a=-1;b=1;
x=linspace(a,b,n+1);
xCheby=sort(cos(0.5*(2*(1:n)-1)*pi/n));
x0=a:0.01:b;
for k=1:length(x0)
w(k)=prod(x0(k)-x);
wCheby(k)=prod(x0(k)-xCheby);
end
plot(x0,w,x0,wCheby)
legend('equidistante','Chebyshev')
REFERENCIAS
1. F.B. Hildebrandt, Introduction to Numerical Analysis, McGraw-Hill Book
Company, Inc, 1956.
113