viernes, 6 de mayo de 2011

METODO DE LAGRANGE EN MATLAB

clear;clc
x=[1994 1996 1998 2000];y=[108.6 94.4 51 43.4]; % entrada de datos

xa=x;ya=y;
% Formacion de las diferencias divididas
d=zeros(length(y));
d(:,1)=y';
for k=2:length(x)
for j=1:length(x)+1-k
d(j,k)=(d(j+1,k-1)-d(j,k-1))/(x(j+k-1)-x(j));
end
end
% Formacion del polinomio
for w=1:length(x)
ds=num2str(abs(d(1,w)));
if w>1
if x(w-1)<0
sg1='+';
else
sg1='-';
end
end
if d(1,w)<0
sg2='-';
else
sg2='+';
end
if w==1
acum=num2str(d(1,1));
elseif w==2
polact=['(x' sg1 num2str(abs(x(w-1))) ')' ];
actual=[ds '*' polact];
acum=[acum sg2 actual];
else
polact=[polact '.*' '(x' sg1 num2str(abs(x(w-1))) ')' ];
actual=[ds '*' polact];
acum=[acum sg2 actual];
end
end

% Presentacion de resultados
fprintf(' n Valores de X y Y n ');
disp(xa);
disp(ya);
fprintf('n Polinomio interpolación Newton : %s n',acum);
x=input(' X interp = ');
if x>max(xa)|x

GUARDAR EN MATLAB EN UN ARCHIVO .m Y LUEGO EJECUTAR

INTERPOLACION

Een este link podran ver varios ejemplos resueltos de interpolacion mediante Lagrange vistos en clase y algunos ejercicios propuestos. Veanlo