Está en la página 1de 21

LAGRANGE

INTERPOLACIÓN NEWTON

DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN


121
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
MÉTODO DIRECTO ∅ .𝑎 = 𝑦

DADOS n+1 datos:

𝐵𝐴𝑆𝐸 = ∅0 𝑥 , ∅1 𝑥 , ∅2 𝑥 , … , ∅𝑛 𝑥 = 1, 𝑥, 𝑥 2 , 𝑥 3 , … , 𝑥 𝑛

1 𝑥0 𝑥02 … 𝑥0𝑛 𝑎0 𝑦0
1 𝑥1 𝑥12 … 𝑥1𝑛 𝑎1 𝑦1
1 𝑥2 𝑥22 … 𝑥2𝑛 𝑎2 = 𝑦2
… … … … … … …
1 𝑥𝑛 𝑥𝑛2 … 𝑥𝑛𝑛 𝑎𝑛 𝑦𝑛
122
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
x 3 7 9 1 3 9 𝑎0 5
y 5 -1 2
1 7 49 𝑎1 = −1
1 9 81 𝑎2 2
𝑎0 20
𝑎1 = −6,5
𝑎2 0,5

𝑃 𝑥 = 20 − 6,5 𝑥 + 0,5 𝑥 2
123
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
function Interpolacion_MD
%% Interpolacion Método Directo iter=0;
clc; clear all; for i=3:0.5:9
datosx=[3;7;9]; iter=iter+1;
x(iter)=i
datosy=[5;-1;2];
v(iter)= f(a(1),a(2),a(3),i)
%dimensión de los datos end
N=size(datos); N=N(1); hold on
V=[ones(N,1) datos datos.^2] plot(x,v,'--r')
a=V\datosy xlabel('x');
plot(datosx,datosy,'rs',… ylabel('y');
'LineWidth',2,... title('Método Directo')
end
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',... function y=f(a1,a2,a3,x)
'MarkerSize',10) y=a1+a2*x+a3*(x^2);
end
124
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
𝑛 (𝑥−𝑥𝑘 )
𝑙𝑖 𝑥 = 𝑘=0 (𝑥 −𝑥 ) 𝐵𝑎𝑠𝑒𝑠
𝑘≠𝑖 𝑖 𝑘

𝑛
𝑃𝑛 𝑥 = 𝑘=0 𝑦𝑘 𝑙𝑘 (𝑥)
125
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
POLINOMIOS DE LAGRANGE
𝑛 (𝑥−𝑥𝑘 ) i 0 1 2
𝑙𝑖 𝑥 = 𝑘=0
(𝑥 −𝑥 ) 𝑖 𝑘 xi 3 7 9
𝑘≠𝑖
yi 5 -1 2

𝑥−𝑥1 (𝑥−𝑥2 ) 𝑥−7 (𝑥−9) 𝑥−7 𝑥−9


𝑙0 𝑥 = 𝑥0 −𝑥1 (𝑥0 −𝑥2 )
= 3−7 (3−9)
= 24

𝑥−𝑥0 (𝑥−𝑥2 ) 𝑥−3 (𝑥−9) 𝑥−3 𝑥−9


𝑙1 𝑥 = = =
𝑥1 −𝑥0 (𝑥1 −𝑥2 ) 7−3 (7−9) −8

𝑥−𝑥0 (𝑥−𝑥1 ) 𝑥−3 (𝑥−7) 𝑥−3 𝑥−7


𝑙2 𝑥 = = =
𝑥2 −𝑥0 (𝑥2 −𝑥1 ) 9−3 (9−7) 12
126
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
𝑛
i 0 1 2

xi 3 7 9
𝑃𝑛 𝑥 = 𝑦𝑘 𝑙𝑘 (𝑥)
yi 5 -1 2
𝑘=0

𝒙−𝟕 𝒙−𝟗 𝒙−𝟑 𝒙−𝟗 𝒙−𝟑 𝒙−𝟕


𝒍𝟎 𝒙 = 𝒍𝟏 𝒙 = 𝒍𝟐 𝒙 =
𝟐𝟒 −𝟖 𝟏𝟐

5 1 2
𝑃𝑛 𝑥 = 𝑥−7 𝑥−9 + 𝑥−3 𝑥−9 + 12 𝑥−3 𝑥−7
24 8

127
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
5 1 2
𝑃𝑛 𝑥 = 𝑥−7 𝑥−9 + 𝑥−3 𝑥−9 + 𝑥−3 𝑥−7
24 8 12

128
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
function Interpolacion_Mlag % Interpolación Polinomios de LAGRANGE
datosx=[3;7;9];
datosy=[5;-1;2];
N=size(datosx); N=N(1);%dimensión del vector de datos
Deltax=0.1; %incremento en x para graficar
%% sumatoria
% cálculo del polinomio resultante
ind=0; % inicializa subíndice del vector polinomio
for k=datosx(1):Deltax:datosx(N)
ind=ind+1;
polix(ind)=k;
suma=0; %inicializa la sumatoria
for i=1:N
suma=suma+(datosy(i)*l(i,N,k,datosx));
end 𝑛
poliy(ind)=suma;
end 𝑃𝑛 𝑥 = 𝑦𝑘 𝑙 𝑘 (𝑥)
𝑘=0

129
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
%% Gréafico del polinomio y puntos dato
plot(polix,poliy,'r',datosx,datosy,'rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10)
title('Interpolación - Polinomios de LAGRANGE')
xlabel('x'); ylabel('y');grid('on');
end

130
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
%% función que calcula los polinomios de Lagrange en x; ln(x)

function f=l(n,N,x,datosx) 𝑛 (𝑥 − 𝑥𝑘 )
% n subíndice de los PL 𝑙𝑖 𝑥 = 𝑘=0 (𝑥 − 𝑥 )
𝑖 𝑘
% N cantidad de datos 𝑘≠𝑖

% x variable
% datosx vector datos con las abscisas

multi=1; %inicializa la productoria


for k=1:N
if k~=n
multi=multi*((x-datosx(k))/(datosx(n)-datosx(k)));
end
end
f=multi;
end
131
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
POLINOMIOS DE NEWTON ∅ . 𝑎 = 𝑦

∅0 𝑥 = 𝑛0 𝑥 = 1
∅1 𝑥 = 𝑛1 𝑥 = 1 (𝑥 − 𝑥0 )
∅2 𝑥 = 𝑛2 𝑥 = 1 𝑥 − 𝑥0 𝑥 − 𝑥1
En general ∅𝒏 𝒙 = 𝒏𝒏 𝒙 = 𝟏 𝒙 − 𝒙𝟎 𝒙 − 𝒙𝟏 𝒙 − 𝒙𝟐 … 𝒙 − 𝒙𝒏−𝟏

1 0 0 … 0
1 (𝑥1 − 𝑥0 ) 0 … 0
∅= 1 (𝑥2 − 𝑥0 ) 𝑥2 − 𝑥0 (𝑥2 − 𝑥1 ) … 0
….. …… ….. ….. …..
1 (𝑥𝑛 − 𝑥0 ) 𝑥𝑛 − 𝑥0 (𝑥𝑛 − 𝑥1 ) … 𝑥𝑛 − 𝑥0 𝑥𝑛 − 𝑥1 … (𝑥𝑛 − 𝑥𝑛−1
132
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
POLINOMIOS DE NEWTON ∅ . 𝑎 = 𝑦
i 0 1 2
1 𝑥0 − 𝑥0 (𝑥0 −𝑥0 ) ∗ (𝑥0 − 𝑥1 )
xi 3 7 9
∅=1 𝑥1 − 𝑥0 (𝑥1 −𝑥0 ) ∗ (𝑥1 − 𝑥1 )
yi 5 -1 2 1 𝑥2 − 𝑥0 (𝑥2 −𝑥0 ) ∗ (𝑥2 − 𝑥1 )
5
𝑎 = −1,5
1 0 0 1 0 0 0,5
∅=1 7−3 0 = 1 4 0
1 9 − 3 (9 − 3) ∗ (9 − 7) 1 6 12

P(x) =a0 . 1 + a1 . (𝑥 − 𝑥0 ) + a2 . (𝑥 − 𝑥0 ) . (𝑥 − 𝑥1 )

P(x) = 5 − 1,5(𝑥 − 3) + 0,5 (𝑥 − 3)(𝑥 − 7)

133
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
clear all;clc;
POLINOMIOS DE NEWTON
datosx=[3;7;9]; datosy=[5;-1;2]; %-------------------DATOS---------------
d=size(datosx);N=d(1) % dimensión de los datos
%-------------- matriz Fi------
c1=datosx-datosx(1); c2= c1.*(datosx-datosx(2));
Fi=[ones(N,1) c1 c2]
%----------------------------
a=Fi\datosy % vector de coeficientes para combinación lineal
%---------gráfico del polinomio--------------
x=datosx(1); incx=0.1;
for i=1:(datosx(N)-datosx(1))/incx
polix(i)=x;
poliy(i)=a(1)+ a(2)*(polix(i)-datosx(1))+ a(3)*(polix(i)-datosx(1))*(polix(i)-datosx(2));
x=x+incx;
end

plot(datosx,datosy,'db',polix,poliy,'g')
title('Datos e interpolación con polinomios de Newton')
xlabel('x'); ylabel('y')
legend('datos','polinomio'); 134
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
Programa genérico

%% POLINOMIOS DE NEWTON

function Interpolacion_PN
clear;clc;
%-------------------DATOS---------------
datosx=[0;pi/2; pi/4; 3*pi/4];
datosy=sin(datosx);
%---------------------------------------
fc=size(datosx); N=fc(1); %calcula cantidad de datos
incx=0.01; %incremento en x para graficar
x=datosx(1); %inicializo x para graficar
Fi=n(N,datosx) % matriz Fi
a=Fi\datosy % vector de coeficientes para combinación lineal

135
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
%---------gráfico del polinomio--------------
for i=1:(datosx(N)-datosx(1))/incx
polix(i)=x;
poliy(i)=a(1)+...
a(2)*(polix(i)-datosx(1))+...
a(3)*(polix(i)-datosx(1))*(polix(i)-datosx(2))+...
a(4)*(polix(i)-datosx(1))*(polix(i)-datosx(2))*(polix(i)-datosx(3));
x=x+incx;
end
figure(1)
plot(datosx,datosy,'db',polix,poliy,'g',polix,sin(polix),'b')
title('Datos e interpolación con polinomios de Newton')
xlabel('x'); ylabel('y')
legend('datos','polinomio','seno(x)');

%% entrega la matriz Fi
function f=n(N,datosx)
f=zeros(N,1);
f(:,1)=1;
for k=2:N
f(:,k)=f(:,k-1).* (datosx(:)-datosx(k-1));%polinomios de Newton
end
end
136
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
figure(2)
x=datosx(1);
for i=1:(datosx(N)-datosx(1))/incx
nx(i)=x;
n1(i)=1;
n2(i)=nx(i)-datosx(1);
n3(i)=(nx(i)-datosx(1))*(nx(i)-datosx(2));
n4(i)=(nx(i)-datosx(1))*(nx(i)-
datosx(2))*(nx(i)-datosx(3));
x=x+incx;
end
plot(nx,n1,'r',nx,n2,'b',nx,n3,'g',nx,n4,'k')
title('Polinomios de Newton')
xlabel('x'); ylabel('y');
legend('n0','n1','n2','n3')
end

137
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
x 0 𝝅 𝝅 𝟑𝝅
𝟒 𝟐 𝟒
y Seno(0) 𝝅
Seno(𝟒 )
𝝅
Seno(𝟐 ) 𝟑𝝅
Seno( 𝟒 )

𝑎0 = 𝑦0 𝑦1 − 𝑦0
x y 𝑎1 =
𝑥1 − 𝑥0 𝑦2 − 𝑦1
− 𝑎1
0 0 𝑥2 − 𝑥1
𝑎2 =
0,90031632 𝑥2 − 𝑥1
0,78539816 0,707106781 -0,33574887
0,37292323 -0,05902388
1,57079633 1 -0,4748206
𝑎3
-0,37292323
2,35619449 0,707106781
138
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
DIFERENCIAS DIVIDIDAS DE NEWTON
𝑎0 𝑎1 𝑎2 𝑎3
x y

0 0
0,707−0
=0,900
0,785−0

0,373−0,9
0,78539816 0,707106781 =-0,336
1,571−0
1−0,707 −0,475+0,336
=0,373
1,571−0,785 2,356−0 =0,059

−0,373−0,373
1,57079633 1 2,356−0,785 =-0,475
0,707−1
=-0,373
2,356−1,571

2,35619449 0,707106781

𝑷 𝒙 = 𝟎 . 𝟏 + 𝟎, 𝟗 𝒙 − 𝟎 + 𝟎, 𝟑𝟑𝟔 𝒙 − 𝟎 𝒙 − 𝟎, 𝟕𝟖𝟓 + 𝟎, 𝟎𝟓𝟗 (𝒙 − 𝟎) 𝒙 − 𝟎, 𝟕𝟖𝟓 𝒙 − 𝟏, 𝟓𝟕𝟏


DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN
𝑷 𝒙 = 𝟎 . 𝟏 + 𝟎, 𝟗 𝒙 − 𝟎 + 𝟎, 𝟑𝟑𝟔 𝒙 − 𝟎 𝒙 − 𝟎, 𝟕𝟖𝟓 + 𝟎, 𝟎𝟓𝟗 (𝒙 − 𝟎) 𝒙 − 𝟎, 𝟕𝟖𝟓 𝒙 − 𝟏, 𝟓𝟕𝟏

140
DRA. ING. SELVA S. RIVERA - PROF. ADJUNTA - CÁLCULO NUMÉRICO Y COMPUTACIÓN

También podría gustarte