Está en la página 1de 4

PROGRAMAS FUENTE

1. POLINOMIO DE TAYLOR
% POLINOMIO DE TAYLOR
% Halle el polinomio de Taylor de grado N=4 de la funcion dada por
% F(X)=X^(1/2) alrededor de Xo=0
clc
clear
disp(' Universidad Central del Ecuador');
disp(' Facultad de Ingeniería Química');
disp(' Análisis Numérico');
f=inline(input('f(x):','s'));
x1=input('X(i):');
x2=input('X(i+1):');
n=input('Numero de orden: ');
h=x2 - x1;

v1=f(x1);
v2=f(x2);
et=abs((v2-v1)/v2*100);

X=[0 v1 0 et];

for i=1:n
d=diff(sym(f(x1),i));
v1=v1+(d*((h^i)/factorial(i)));
et=abs(((v2-v1)/v2)*100);
X(i+1,1)=i;
X(i+1,2)=v1;
ea=abs((X(i+1,2)-X(i,2))/X(i,2)*100);
X(i+1,3)=ea;
X(i+1,4)=et;
if ea<=0
break
end
end

2. POLINOMIO INTERPOLADOR DE NEWTON

% POLINOMIO INTERPOLADOR DE NEWTON


% Halle el polinomio interpolador de Newton.
clear;clc
x=[0 1 2 3];y=[1 2 2.5 4]; % entrada de datos.

%Cuerpo del programa


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 interpolacion Newton : %s n\n',acum);
x=input(' X interp = ');
if x>max(xa)|x<min(xa)
fprintf('t Punto fuera de rango. El resultado puede ser equivocado
n:\n');
end
xinterp=x;
yinterp=eval(acum);
fprintf(' Y(%g) = %g n\n',x,yinterp);
% Grafica de los puntos
fprintf(' Pulse cualquier tecla para ver la grafica de los puntos n:');
pause
xg=linspace(min(xa),max(xa));
x=xg;yg=eval(acum);
plot(xg,yg,xa,ya,'.r',xinterp,yinterp,'or');
grid

3. POLINOMIO INTERPOLADOR DE LAGRANGE


% POLINOMIO INTERPOLADOR DE LAGRANGE
% Halle el polinomio interpolador de Lagrange.

fprintf('Interpolación de Lagrange ')


n=input('Ingrese el número de puntos : ');
for i = 1:n
fprintf('x%d = ',i)
x(i)= input(' ');
fprintf('f(x(%d)) = ',i)
f(i)= input(' ');
end
a=input('Ingrese el valor de x donde se quiere evaluar el polinomio
interpolante : ');
for k=1:n
for i=1:n
if k==i
I(i)=1;
else
I(i)=(a-x(i))/(x(k)-x(i));
end
end
fI(k)=f(k)*prod(I);
end
fa=sum(fI);
fprintf('f(%1.2f) = %3.6f',a,fa)
for i=1:n
l0=((x(i)-2)*(x(i)-3)*(x(i)-4)*(x(i)-5))/((1-2)*(1-3)*(1-4)*(1-5))
l1=((x(i)-1)*(x(i)-3)*(x(i)-4)*(x(i)-5))/((2-1)*(2-3)*(2-4)*(2-5))
l2=((x(i)-1)*(x(i)-2)*(x(i)-4)*(x(i)-5))/((3-1)*(3-2)*(3-4)*(3-5))
l3=((x(i)-1)*(x(i)-2)*(x(i)-3)*(x(i)-5))/((4-1)*(4-2)*(4-3)*(4-5))
l4=((x(i)-1)*(x(i)-2)*(x(i)-3)*(x(i)-4))/((5-1)*(5-2)*(5-3)*(5-4))
p(i)=(f(1))*l0+(f(2))*l1+(f(3))*l2+(f(4))*l3+(f(5))*l4
end
r=[x(1) x(2) x(3) x(4) x(5)];
t=[f(1) f(2) f(3) f(4) f(5)];
y=[p(1) p(2) p(3) p(4) p(5)];
a1=a+0.1;
fa1=exp(a1);
fa0=((fa1-fa)/(a1-a))+(((fa1-fa)/(a1-a))*-a)+fa;
fa2=(((fa1-fa)/(a1-a))*x(5))-(((fa1-fa)/(a1-a))*a)+fa;
z=[1 a a1 x(5)];
q=[fa0 fa fa1 fa2];
hold on
plot(r,t,'r')
plot(r,y,'b')
plot(z,q,'y')
hold off

4. POLINOMIO DE CHEBYSHEV

% POLINOMIO DE CHEBYSHEV
% Halle el polinomio de chebyshev.
xxx=0;
k2=[1:n];
for kk8=1:length(k2)
xxx(kk8)=cos((kk8-1/2)*pi/n);
end
ftx=zeros(n,n+1);
for kk9=1:n
ftx1=ones(1,n+1);
ftx2=[n:-1:0];
ftx3=ftx1*xxx(kk9);
ftx4=ftx3.^ftx2;
ftx(kk9,:)=[ftx4];
end
ifwxt=0;
for kk10=1:n
ifa1=tnx;
ifa2=ftx(kk10,:);
ifa3=ifa1.*ifa2;
ifa4=sum(ifa3);
ifa5=pi/n*ifa4;
ifwxt=ifwxt+ifa5;
end
ifwxt

También podría gustarte