You are on page 1of 1

function cubica

X=input('ingrese el vector x: ');

Y=input('ingrese el vector y: ');

x=input('ingrese el dato a interpolar: ');

n=length(X);

for i=1:(n-2)

A(i,i)=X(i+1)-X(i);

A(i,i+1)=2*(X(i+2)-X(i));

A(i,i+2)=X(i+2)-X(i+1);

B(i,1)=(6*(Y(i+2)-Y(i+1))/(X(i+2)-X(i+1)))+(6*(Y(i)-Y(i+1))/(X(i+1)-X(i)));

end

A=A(:,2:(n-1));

C=inv(A)*B;

f(1)=0;

f(n)=0;

for k=2:n-1

f(k)=C(k-1);

end

for j=1:n

if X(j)<x & x<X(j+1)

deltax=X(j+1)-X(j);

valy=(f(j)*((X(j+1)-x)^3)/(6*deltax))+(f(j+1)*((x-X(j))^3)/(6*deltax))+((Y(j)/deltax-
f(j)*deltax/6)*(X(j+1)-x))+((Y(j+1)/deltax-f(j+1)*deltax/6)*(x-X(j)));

end

end

printf('el valor aproximado es: %2.2f',valy);

endfunction