Вы находитесь на странице: 1из 9

Mtodos Numricos

UNIVERSIDAD DE LAS FUERZAS ARMADAS


ESPE

TAREA EXTRA

CARLOS JARAMILLO

DEPARTAMENTO CIENCIAS EXACTAS

DOCENTE Mgs. Fabin Ordoez

13 de Noviembre de 2013

1
Carlos Jaramillo

Mtodos Numricos

DESARROLLE EL CODIGO PARA LA INTERPOLACION POR NEWTON


EN MATLAB

CDIGO EN MATLAB
Interpolacion de newton
clear;clc;
disp('Metodos Numericos');
disp('interpolacion de newton');
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 tabulada es:');
disp(DD);
disp('El polinomio interpolador 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+');

2
Carlos Jaramillo

Mtodos Numricos

RESULTADO

Metodos Numericos
interpolacion de newton
ingrese el grado del polinomio, n=5
Entonces se necesitan 6 puntos
ingrese los puntos
x0= 1
y0= -3
x1= 2
y1= 0
x2= 3
y2= 15
x3= 4
y3= 48
x4= 5
y4= 105
x5= 6
y5= 192

3
Carlos Jaramillo

Mtodos Numricos

La matriz tabulada es:


-3

15

15

48

33

105

57

12

192

87

15

El polinomio interpolador de newton es

-4x

GRAFICA

4
Carlos Jaramillo

Mtodos Numricos

DESARROLLO DEL CODIGO DEL SPLIN CUBICO EN MATLAB


function [a,b,c,d]=spline3(X)
X=[1 2 3 4 5 6 7 8 9; 0 3 4 -6 2 4 0 4 3];
n=length(X(1,:));
for i=1:n;
a(i)=X(2,i);
end
for i=1:n-1;
h(i)=X(1,i+1)-X(1,i);
end
for i=2:n-1;
alfa(i)=3/h(i)*(a(i+1)-a(i))-3/h(i-1)*(a(i)-a(i-1));
end
l(1)=1;
mu(1)=0;
z(1)=0;
for i=2:n-1;
l(i)=2*(X(1,i+1)-X(1,i-1))-h(i-1)*mu(i-1);
mu(i)=h(i)/l(i);
z(i)=(alfa(i)-h(i-1)*z(i-1))/l(i);
end
l(n)=1;
z(n)=0;
c(n)=0;
for i=n-1:-1:1;
c(i)=z(i)-mu(i)*c(i+1);
b(i)=(a(i+1)-a(i))/h(i)-h(i)*(c(i+1)+2*c(i))/3;
d(i)=(c(i+1)-c(i))/(3*h(i));
end
for i=1:n-1;
x=X(1,i):0.1:X(1,i+1);
y=a(i)+b(i)*(x-X(1,i))+c(i)*(x-X(1,i)).^2+d(i)*(x-X(1,i)).^3;
hold on;
plot(x,y,'b');
end
5
Carlos Jaramillo

Mtodos Numricos

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 tabulada es:');
disp(DD);
disp('Los polinomios interpoladores por splin son);
syms x;
polnew=DD(1,1);
P=1;

for i=1:n;
hold on;
plot (X(1,i),X(2,i),'*','MarkerEdgeColor','r','LineWidth',1);
title('Interpolacin por "splines" de orden 3.');
end
EJEMPLO

Por ejemplo, para los datos {(1,0),(2,3),(3,4),(4,-6),(5,2),(6,4),(7,0),(8,4),(9,3


>>X=[1 2 3 4 5 6 7 8 9; 0 3 4 -6 2 4 0 4 3];
>>[m,b]=spline3(X) ;
a = 0 3 4 -6 2 4 0 4 3
b = 2.3806 4.2388 -7.3357 -1.8960 8.9196 -3.7826 0.2107 2.9398
c = 0 1.8582 -13.4326 18.8723 -8.0567 -4.6455 8.6388 -5.9097 0
d = 0.6194 -5.0969 10.7683 -8.9763 1.1371 4.4281 -4.8495 1.9699
ans:
0+2.3806x + 0x2 + 0.6194x3
3 + 4.2388x+1.8582X2 -5.0969x3
4-7.3357x-13.4326x2 -10.7683x3
6
Carlos Jaramillo

Mtodos Numricos

-6-1.8960x+18.8723x2 -8.9763x3
2+8.9196x- 8.0567x2 +1.1371x3
4-3.7826x-4.6455x2 +4.4281x3
0+0.2107x+8.6388x2 -4.8495x3
4+2.9398x-5.9097x2 +0x3

GRAFICA RESULTANTE

7
Carlos Jaramillo

Mtodos Numricos

DESARROLLO DEL CODIGO DEL SPLIN LINEAL EN MATLAB

CODIGO MATLAB
% SPLINE LINEAL
function [m,b]=spline(X)
X= [1 2 3 4 5 6 7 8 9; 0 3 4 -6 2 4 0 4 3]
n=length(X(1,:));

for i=1:n-1;
m(i)=(X(2,i+1)-X(2,i))/(X(1,i+1)-X(1,i))
b(i)=X(2,i);
x=X(1,i):X(1,i+1);
y=m(i)*(x-X(1,i))+b(i)
hold on;
plot(x,y,'b');
end

for i=1:n;
hold on;
plot (X(1,i),X(2,i),'*','MarkerEdgeColor','r','LineWidth',1);
title('Interpolacin por "splines" de orden 1.');
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 tabulada es:');
disp(DD);
disp('Los polinomios interpoladores por splin lineal son);
syms x;
polnew=DD(1,1);

8
Carlos Jaramillo

Mtodos Numricos

Por ejemplo, para los datos {(1,0),(2,3),(3,4),(4,-6),(5,2),(6,4),(7,0),(8,4),(9,3)}


m = 3 1 -10 8 2 -4 4 -1
b = 0 3 4 -6 2 4 0 4
ans:
3x+0
X+3
-10x+4
8x+6
2x+2
-4x+4
4x+0
-x+4
GRAFICA RESULTANTE

9
Carlos Jaramillo

Вам также может понравиться