Está en la página 1de 7

Diferencias divididas de Newton

x=[1.9 3.1 4.2 5.1 5.8 6.9 8.1 9.3 10.0];y=[0.3 0.6 0.4 0.9 0.7 1.1
1.5 1.3 1.6];
xa=x;ya=y;
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
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
fprintf('\n Valores de X y Y \n ');
disp(xa);
disp(ya);
fprintf('\n Polinomio interpolacin Newton : %s \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');
end
xinterp=x;
yinterp=eval(acum);
fprintf(' Y(%g) = %g \n',x,yinterp);
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

Valores de X y Y

1.9000 3.1000 4.2000 5.1000 5.8000 6.9000 8.1000 9.3000 10.0000

0.3000 0.6000 0.4000 0.9000 0.7000 1.1000 1.5000 1.3000 1.6000

Polinomio interpolacin Newton : 0.3+0.25*(x-1.9)-0.18775*(x-1.9).*(x-3.1)+0.17389*(x1.9).*(x-3.1).*(x-4.2)-0.12953*(x-1.9).*(x-3.1).*(x-4.2).*(x-5.1)+0.060624*(x-1.9).*(x-3.1).*(x4.2).*(x-5.1).*(x-5.8)-0.019125*(x-1.9).*(x-3.1).*(x-4.2).*(x-5.1).*(x-5.8).*(x6.9)+0.0044139*(x-1.9).*(x-3.1).*(x-4.2).*(x-5.1).*(x-5.8).*(x-6.9).*(x-8.1)-0.00084409*(x1.9).*(x-3.1).*(x-4.2).*(x-5.1).*(x-5.8).*(x-6.9).*(x-8.1).*(x-9.3)


X interp = 2.14
Y(2.14) = 2.97306
X interp = 2.38
Y(2.38) = 3.22774
X interp = 2.62
Y(2.62) = 2.44414
X interp = 2.86
Y(2.86) = 1.43084
X interp = 3.32
Y(3.32) = 0.132007
X interp = 3.54
Y(3.54) = -0.052857
X interp = 3.76
Y(3.76) = -0.014253
X interp = 3.98
Y(3.98) = 0.164366
X interp = 4.38
Y(4.38) = 0.587141
X interp = 4.56

Y(4.56) = 0.740666
X interp = 4.74
Y(4.74) = 0.845557
X interp = 4.92
Y(4.92) = 0.897226
X interp = 5.24
Y(5.24) = 0.875322
X interp = 5.38
Y(5.38) = 0.8349
X interp = 5.52
Y(5.52) = 0.786802
X interp = 5.66
Y(5.66) = 0.739268
X interp = 6.02
Y(6.02) = 0.670965
X interp = 6.24
Y(6.24) = 0.696673
X interp = 6.46
Y(6.46) = 0.783302
X interp = 6.68
Y(6.68) = 0.92405
X interp = 7.14
Y(7.14) = 1.30096
X interp = 7.38
Y(7.38) = 1.47083
X interp = 7.62
Y(7.62) = 1.57295

X interp = 7.86
Y(7.86) = 1.58351
X interp = 8.34
Y(8.34) = 1.34965
X interp = 8.58
Y(8.58) = 1.18414
X interp = 8.82
Y(8.82) = 1.07708
X interp = 9.06
Y(9.06) = 1.10192
X interp = 9.44
Y(9.44) = 1.47891
X interp = 9.58
Y(9.58) = 1.67293
X interp = 9.72
Y(9.72) = 1.82341
X interp = 9.86
Y(9.86) = 1.8406
SPLINES
function[tx,ts]=spline1(x,y,npti)
n1=length(x);
n=n1-1;
for i=1:n
h(i)=x(i+1)-x(i);
end
[D2]=thomas(h,y);
M(1)=0;
M(n1)=0;
M(2:n)=D2;
nc=0;
for i=1:n
paso=h(i)/(npti+1);
for j=1:npti+1
nc=nc+1;
tx(nc)=x(i)+paso*(j-1);
xp=tx(nc);
t1=(1/(6*h(i)))*(((xp-x(i))^3)*M(i+1)+((x(i+1)-xp)^3)*M(i));
t2=(1/h(i))*((xp-x(i))*y(i+1)+(x(i+1)-xp)*y(i));

t3=(h(i)/6)*((xp-x(i))*M(i+1)+(x(i+1)-xp)*M(i));
ts(nc)=t1+t2-t3;
end
end
tx(nc+1)=x(n1);
ts(nc+1)=y(n1);

function[D2]=thomas(h,y)
n=length(h);
m1=n-1;
for i=1:m1
b(i)=(h(i)+h(i+1))/3;
end
for i=1:m1-1
c(i)=h(i+1)/6;
end
c(m1)=0;
a(1)=0;
for i=2:m1
a(i)=h(i)/6;
end
for i=1:m1
d(i)=(y(i+2)-y(i+1))/h(i+1)-(y(i+1)-y(i))/h(i);
end
for i=1:m1-1
factor=a(i+1)/b(i);
b(i+1)=b(i+1)-factor*c(i);
d(i+1)=d(i+1)-factor*d(i);
end
D2(m1)=d(m1)/b(m1);
for i=m1-1:-1:1
D2(i)=(d(i)-c(i)*D2(i+1))/b(i);
end

tx =

Columns 1 through 10

1.9000 2.1400 2.3800 2.6200 2.8600 3.1000 3.3200 3.5400 3.7600 3.9800

Columns 11 through 20

4.2000 4.3800 4.5600 4.7400 4.9200 5.1000 5.2400 5.3800 5.5200 5.6600

Columns 21 through 30

5.8000 6.0200 6.2400 6.4600 6.6800 6.9000 7.1400 7.3800 7.6200 7.8600

Columns 31 through 40

8.1000 8.3400 8.5800 8.8200 9.0600 9.3000 9.4400 9.5800 9.7200 9.8600

Column 41

10.0000

ts =

Columns 1 through 10

0.3000 0.4074 0.5030 0.5748 0.6111 0.6000 0.5444 0.4677 0.3989 0.3667

Columns 11 through 20

0.4000 0.4886 0.6116 0.7403 0.8460 0.9000 0.8929 0.8532 0.7971 0.7407

Columns 21 through 30

0.7000 0.6933 0.7476 0.8453 0.9686 1.1000 1.2343 1.3493 1.4371 1.4900

Columns 31 through 40

1.5000 1.4647 1.4029 1.3387 1.2964 1.3000 1.3314 1.3818 1.4466 1.5209

Column 41

1.6000

También podría gustarte