Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
n=9;
syms x;
xn=[-5 -3 -2 -1 0 1 2 3 4 5];
yn=1./(1.+xn.^2);
plot(xn,yn,'*r')
hold on
p=0;
for i=1:n
L=1;
for j=1:n
if j~=i
L=L*(x-xn(j))/(xn(i)-xn(j));
end
end
p=p+L*yn(i);
end
p=simplify(p)
pretty(p)
x=-5:0.01:5;
f=1./(1+x.^2);
plot(x,f);
>> poli_interp
p=
8 7 6 5 4 3 2
21 x x 92 x 7 x 7659 x 49 x 7261 x
18 x
---- + 1
1105
%Programa que calcula el polinomio interpolador con comentarios
>> poli_inter_nom
p=
21 x x 92 x 7 x 7659 x 49 x 7261 x
18 x
---- + 1
1105
%Programa que calcula varios polinomios interpoladores de n+1 puntos
a=-5; b=5;
syms x;
t=-5:0.01:5;
f=1./(1+t.^2);
plot(t,f,'g.'); hold on;
for n=2:4
h=(b-a)/n;
for k=1:n+1
xn(k)=a+(k-1)*h; yn(k)=1/(1+xn(k)^2);
plot(xn,yn,'*r')
end
hold on
p=0;
for i=1:n+1
L=1;
for j=1:n+1
if j~=i
L=L.*(x-xn(j))./(xn(i)-xn(j));
end
end
p=p+L*yn(i);
end
ezplot(p);
hold on
end
Funcion general de Lagrange
function [C, L] = LAGRANGE (X,Y)
% X ES UN VECTOR QUE CONTIENE LAS LISTAS DE ABSISAS
% Y ES UN VECTOR QUE CONTIENE LAS LISTAS DE ORDENADAS
% C ES LA MATRIZ QUE CONTIENE LOS COEFICIENTES DEL POLINOMIO
% Li ES LA MATRIZ QUE CONTIENE LOS COEFICIENTES DDEL POLINOMIO DE
LAGRANGE
w=length(X);
n=w-1;
L=zeros(w,w);
%FORMACION DE LOS POLINOMIO Li LAGRANGE
for k=1:n+1
V=1;
for r=1:n+1;
if k~=r
V=conv(V,poly(X(r)))/(X(k)-X(r));
end
end
L(k,:)=V;
end
% CALCULO DE LOS POLINOMIOS INTERPOLADOR
C=Y*L;
Para calculos
>> X=-3:2:3;
>> Y=4./(1+X.^2);
C=
0 -0.2000 0 2.2000
L=
DIFERENCIAS DIVIDIDAS
PUNTOS 0 1 2 3 4 5
x -2 -1 0 2 3 6
f(x) -18 -5 -2 -2 7 142
x0 f[x0]
5(18)
f[x0,x1] = =13
1(2)
313
x0 f[x0] f[x0,x1,x2] = =-5
0(2)
2(5) 1(5)
f[x1,x2] = =3 f[x0,x1,x1,x3] = =1
0(1) 2(2)
03
x0 f[x0] f[x2,x2,x3] = =-1
2(1)
2(2) 3(1)
f[x2,x3] = =0 f[x0,x1,x1,x3] = =1
02 3(1)
90
x0 f[x0] f[x2,x3,x4] = = 3
30
7(2) 93
f[x3,x4] = =9 f[x0,x1,x1,x3] = =1
32 60
459
x0 f[x0] f[x3,x4,x5] = = 9
62
1427
f[x4,x5] = =45
63
x0 f[x0]
11
f[x0,x1,x1,x3,x4] = =0
3(2)
11
f[x1,x2,x3,x4,x5] = =0
6(1)
En Matlab:
clear
x= [5 20 40];
fx=[113 181 214.5];
M=3; N=M-1;
for i=1 : N
T(i,1) = (fx(i+1)-fx(i))/(x(i+1)-x(i))
end
for j=2:N
for i=j:N
T(i,j)=(T(i,j-1)-T(i-1,j-1))/(x(i+1)-x(i-j+1));
end
end
T
Xint=30;
px2=fx(1)+T(1,1)*(Xint-x(1))+T(2,2)*(Xint-x(1))*(Xint-x(2));
fprintf('T(%2d)=%6.1f\n',Xint, px2)
.
clear
x=[1 5 20 40];
fx=[56.5 113 181 214.5];
M=4;N=M-1;
for i=1:N
T(i,1)=(fx(i+1)-fx(i))/(x(i+1)-x(i));
end
for j=2:N
for i=j:N
T(i,j)=(T(i,j-1)-T(i-1,j-1))/(x(i+1)-x(i-j+1));
end
end
T
Xint=2;
fprintf('N Fxint \n')
px1=fx(1)+T(1,1)*(Xint-x(1));
fprintf('%d %6.1f \n',1,px1)
px2=fx(1)+T(1,1)*(Xint-x(1))+T(2,2)*(Xint-x(1))*(Xint-x(2));
fprintf('%d %6.1f \n',2,px2)
px3=fx(1)+T(1,1)*(Xint-x(1))+T(2,2)*(Xint-x(1))*(Xint-
x(2))+T(3,3)*(Xint-x(1))*(Xint-x(2))*(Xint-x(3));
fprintf('%d %6.1f \n',3,px3)
clear;clc;
disp('metodos numericos');
disp('interpolacion');
disp('interpolacion');
n=input('ingrese el grado del polinomio, n=');
fprintf('Se necesitan %.0f puntos\n',n+1);
disp('ingrese los puntos');
for i=1:n+1
fprintf('x%.0f=',i-1);
X(i)=input(' ');
fprintf('y%.0f=',i-1);
Y(i)=input(' ');
end
DD=zeros(n+1);
DD(:,1)=Y;
for k=2:n+1
for J=k:n+1
DD(J,k)=[DD(J,k-1)-DD(J-1,k-1)]/[X(J)-X(J-k+1)];
end
end
disp('La matriz de diferencias divididas es:');
disp(DD);
disp('El polinomio de newton es');
syms x;
polnew=DD(1,1);
P=1;
for i=1:n
P=P*(x-X(i));
polnew=polnew+P*DD(i+1,i+1);
end
polnew=expand(polnew);
pretty(polnew);
x=input('ingrese el valor de x a interpolar,x=');
vi=eval(polnew);
fprintf('el valor interpolado es %.2f\n',vi);
hold on;
ezplot(polnew,[X(1) X(n+1)]);
plot(x,vi,'r+');
..
n=9; % nmero de puntos
syms x; % define la variable simblica para crear el polinomio
xn=[-5 -3 -2 -1 0 1 2 3 4 5]; % abscisas de los puntos a interpolar
yn=1./(1.+xn.^2); % ordenadas de estas abscisas
plot(xn,yn,'*r') % dibuja los puntos a interpolar
hold on
p=0; % inicializa el polinomio de interpolacin que empezar a
calcular
for i=1:n
L=1;
for j=1:n
if j~=i
L=L*(x-xn(j))/(xn(i)-xn(j));
end
end
p=p+L*yn(i); % forma de Lagrange
end
p=simplify(p)
pretty(p) % muestra el polinomio en pantalla
x=-5:0.01:5;
f=1./(1+x.^2);
plot(x,f); % dibuja la funcin a interpolar