Professional Documents
Culture Documents
1. Objetivos:
Implementar diversas fórmulas que permiten calcular polinomios de
interpolación, según las diversas metodologías estudiadas:
interpolación lineal, interpolación cuadrática, interpolación por el
polinomio de Newton e interpolación por el polinomio de Lagrange.
Graficar polinomios de interpolación de diversas funciones y con
diferentes mallas de modo de poder apreciar en el computador los
errores de interpolación.
2. Guide:
3. Codificación:
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
global radioboton;
if radioboton == 0
%lineal
dato = 0;
f = get(handles.funcion,'string');
vect = str2num(get(handles.datos,'string'));
x0 = vect(1);
x1 = vect(2);
xI = str2double(get(handles.datointerpolar,'string'));
x =x0; fx0 = eval(f);
x =x1; fx1 = eval(f);
x =xI; fxI = eval(f);
por = '%';
aprox = fx0 + ((fx1-fx0)/(x1-x0))*(xI-x0);
set(handles.aprox,'string',aprox);
%fprintf('Aproximacion = %.6f\n',aprox);
set(handles.valorreal,'string',fxI)
error = 100- (aprox*100)/fxI;
set(handles.error,'string',error)
%fprintf('Error = %.2f%c\n',error,por);
v(1,1)=fx0;
v(2,1)=fx1;
v(2,2)=(fx1-fx0)/(x1-x0);
set(handles.uitable1,'Data',v);
axes(handles.axes1)
fplot(f,[x0, x1],'r');
grid on
hold on
plot(xI,aprox,'*b')
elseif radioboton == 1
%cuadratica
dato = 1;
f = get(handles.funcion,'string');
vect = str2num(get(handles.datos,'string'));
a = vect(1);
b = vect(2);
c = vect(3);
d = str2double(get(handles.datointerpolar,'string'));
x=a; fa=eval(f);
x=b; fb=eval(f);
x=c; fc=eval(f);
x=d; vreal = eval(f);
b3 = (fc-fb)/(c-b);
b0=fa;
b1=(fb-fa)/(b-a);
b2=(((fc-fb)/(c-b))-((fb-fa)/(b-a)))/(c-a);
a0=b0-(b1*a)+(b2*a*b);
a1=b1-(b2*a)-(b2*b);
a2=b2;
f2=[num2str(a0),'+',num2str(a1),'*x+',num2str(a2),'*x.^2'];
x=d;
fd=eval(f2);
error = 100 - ((fd*100)/vreal);
set(handles.aprox,'string',fd);
set(handles.valorreal,'string',vreal);
set(handles.error,'string',error);
%fprintf('La aproximacion es %-3.6f\n',fd)
axes(handles.axes1);
x=linspace(a,c,1000);
y=eval(f);
elseif radioboton == 2
%cubica
dato = 2;
f = get(handles.funcion,'string');
fil = length(str2num(get(handles.datos,'string')));
%fil = input('ingrese cantiad de elementos: ');
vector = str2num(get(handles.datos,'string'));
%inicializando matriz
col = fil;
for i=1:fil
for j=1:col
mat(i,j)=-100;
end
end
%generando vector de entrada y matriz de datos iniciales
for i=1:fil
%fprintf('- ingrese elemento %d : ' ,i-1);
vect(i) = vector(i);
x = vect(i); fx = eval(f);
mat(i,1) = fx;
end
%disp(vect);
%disp(mat);
xI = str2double(get(handles.datointerpolar,'string'));
%xI = input('ingrese el valor a intepolar: ');
%calculando b0,b1,b2....b(n-1)
a = 1;
for j=2:col
a=a+1;
for i=a:fil
mat(i,j)= ((mat(i,j-1))-(mat(i-1,j-1)))/(vect(i)-
vect(i-j+1));
end
end
for i=1:fil
for j=1:col
if (mat(i,j)==-100)
mat(i,j)= 0;
end
end
end
set(handles.uitable1,'Data',mat);
%visualizando matriz b
%apro= b0+b1(xI-x0)+
apro=0;
val=0;
cic=0;
for i=1:fil
b= mat(i,i);
acu=1;
if val~=0
cic=cic+1;
for j=1:cic
acu = acu * (xI-vect(j));
end
end
val=1
apro =apro+(b*acu)
end
%por='%';
set(handles.aprox,'string',apro);
%fprintf('Aproximacion = %.6f\n',apro);
x=xI; vreal = eval(f);
error = 100 - (apro*100)/vreal;
set(handles.error,'string',error);
set(handles.valorreal,'string',vreal)
%fprintf('Error = %.2f%c\n',error,por);
x=vector(1); fx0=eval(f);
x=vector(2); fx1=eval(f);
x=vector(3); fx2=eval(f);
x=vector(4); fx3=eval(f);
x=xI; vreal = eval(f);
b0 = fx0;
b1 = (fx1-fx0)/(vector(2)-vector(1));
z = (fx2-fx1)/(vector(3)-vector(2));
b2 = (z-b1)/(vector(3)-vector(1));
c = (fx3-fx2)/(vector(4)-vector(3));
x = (c-z)/(vector(4)-vector(2));
b3 = (xI-b2)/(vector(4)-vector(1));
t1 = (b1*(xI-vector(1)));
t2 = (b2*(xI-vector(1))*(xI-vector(2)));
t3 = (b3*(xI-vector(1))*(xI-vector(2))*(xI-vector(3)));
%fi = (b0) + +(b2*(xI-vector(1))*(xI-vector(2)))+(b3*(xI-
vector(1))*(xI-vector(2))*(xI-vector(3)));
%fi = [num2str(b0),'-',num2str(t1),'-
',num2str(t2),'*x+',num2str(t3),'*x.^3'];
%f2=[num2str(a0),'+',num2str(a1),'*x+',num2str(a2),'*x.^2'];
x= xI;
%fd = eval(fi)
axes(handles.axes1);
x=linspace(vect(1),vect(4)+4,1000);
y=eval(f);
% func = a + b*x+c*x^2+d*x^3
fun1 = mat(1,1);
fun2 = mat(2,2);
fun3 = mat(3,3);
fun4 = mat(4,4);
func= [num2str(fun1),'+',num2str(fun2),'*(x-
1)',num2str(fun3),'*(x.^2-5*x+4)+',num2str(fun4),'*(x.^3-
11*x.^2+34*x-24)'];
plot(xI,fd,'sk','MarkerFaceColor','k')
%plot(xq1,s,'-.')
hold on
plot(vector(1),fx0,'og',vector(2),fx1,'og',vector(3),fx2,'og',vect
or(4),fx3,'og')
else
dato = 3;
f = get(handles.funcion,'string');
vect = get(handles.datos,'string');
v = str2num(vect);
xI = str2double(get(handles.datointerpolar,'string'));
n = length(v);
x=xI; vreal = eval(f);
for i=1:n
x = v(i);
mat(i,1) = eval(f);
end
j=2;
acu=2;
i=2;
while j<=n
while i<=n
mat(i,j)= ((mat(i,j-1))-(mat(i-1,j-1)))/(v(i)-v(i-
j+1));
i= i+1;
end
j=j+1;
acu = acu+1;
i = acu;
end
set(handles.valorreal,'string',vreal);
set(handles.uitable1,'Data',mat);
acu=1;
fi = 0;
for i=1:n-1
acu = (xI-v(i))*acu;
fi =(mat(i+1,i+1)*acu) + fi;
end
aprox = mat(1,1) + fi;
set(handles.aprox,'string',aprox);
error = abs(100 - ((aprox*100)/vreal));
set(handles.error,'string',error);
end
radioboton = dato;
% --- Executes on button press in limpiar.
function limpiar_Callback(hObject, eventdata, handles)
cla(handles.axes1,'reset')