You are on page 1of 7

INTERPOLACION

clear
clear all
n=('Ingrese el numero del valor de la funcion n=');
disp('Ingreso de datos...');
fx=input('Ingrese la funcion fx=');
x0=input('Ingrese los valores de x0=');
x1=input('Ingrese el valor de x1=');
x=x0;
fx0=eval(fx);
x=x1;
fx1=eval(fx);
x=input('ingrese el valor a calclar');
fx=fx0+((fx1-fx0)/(x1-x0))*(x-x0);
fprintf('El valor de x es %2.6f',fx);

comando for
clear;
clear all;
disp('Programa de Interpolacion Polinomial');
n=input('Ingrese el orden del polinomio, n=');
disp('Ingreso de datos...');
for i=1:n+1
x(i)=input('Ingrese abcisa...');
fx(i)=input('Ingrese ordenada...');
end
syms X;
FX1=fx(1)+((fx(2)-fx(1))/(x(2)-x(1)))*(X-x(1));
X=input('Ingrese el valor a interpolar...x=');
VI=eval(FX1);
fprintf('El valor de la interpolacion es: %0.6f\n',VI);

clear;
clear all;
disp('Programa de Interpolacion Polinomial');
n=input('Ingrese el orden del polinomio, n=');
disp('Ingreso de datos...');
for i=1:n+1
fprintf('Ingrese abcisa x(%0,0f)=',i-1);
x(i)=input('');
fprintf('Ingrese ordenada f(%0.0f)=',i-1);
fx(i)=input('');
end
switch n
case 1
syms X;
FX1=fx(1)+((fx(2)-fx(1))/(x(2)-x(1)))*(X-x(1));
X=input('Ingrese el valor a interpolar...x=');
VI=eval(FX1);
fprintf('El valor de la interpolacion es: %0.6f\n',VI);
case 2
syms X
B0=fx(1);
B1=(fx(2)-fx(1))/(x(2)-x(1));
B2=((fx(3)-fx(2))/(x(3)-x(2))-(fx(2)-fx(1))/(x(2)-x(1)))/x(3)-
x(1);
FX2=B0+B1*(X-x(1))+B2*(X-x(1))*(X-x(2));
X=input('Ingrese el valor a interpolar...x=');
VI=eval(FX2);
fprintf('El valor de la interpolacion es: %0.6f\n',VI);
end

clear;
clear all;
disp('Programa de Interpolacion Polinomial Metodo de Newton');
n=input('Ingrese el valor de n=');
for i=1:n+1
x(i)=input('Ingrese abcisa...');
fx(i)=input('Ingrese ordenada...');
end
n=length(x);%Numero de datos
dd=zeros(n);%Creacion de la matriz de diferencias divididas
dd(:,1)=fx
%Parte 1--> Calculo de las diferencias Divididas Finitas
for k=2:n
for j=k:n
dd(j,k)=(dd(j,k-1)-dd(j-1,k-1))/(x(j)-x(j-k+1))
end
end
pause
syms a
term1=1
term2=0
for i=1:n-1
term1=term1*(a-x(i))
term2=term2+dd(i+1,i+1)*term1
end
pn=dd(1,1)+term2
a=input('el valor a interpolar,');
ipn=eval(pn)
x=x(1):0.1:x(n)
q=length(x)
for i=1:q
a=x(i)
epn(i)=eval(pn)
end
plot(x,epn,x,fx);grid

nction varargout = interpolar(varargin)


% INTERPOLAR MATLAB code for interpolar.fig
% INTERPOLAR, by itself, creates a new INTERPOLAR or raises the
existing
% singleton*.
%
% H = INTERPOLAR returns the handle to a new INTERPOLAR or the
handle to
% the existing singleton*.
%
% INTERPOLAR('CALLBACK',hObject,eventData,handles,...) calls the
local
% function named CALLBACK in INTERPOLAR.M with the given input
arguments.
%
% INTERPOLAR('Property','Value',...) creates a new INTERPOLAR or
raises the
% existing singleton*. Starting from the left, property value pairs
are
% applied to the GUI before interpolar_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property
application
% stop. All inputs are passed to interpolar_OpeningFcn via
varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only
one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help interpolar

% Last Modified by GUIDE v2.5 12-Oct-2015 11:40:16

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @interpolar_OpeningFcn, ...
'gui_OutputFcn', @interpolar_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before interpolar is made visible.


function interpolar_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to interpolar (see VARARGIN)

% Choose default command line output for interpolar


handles.output = hObject;
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes interpolar wait for user response (see UIRESUME)


% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = interpolar_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure


varargout{1} = handles.output;

function datos_Callback(hObject, eventdata, handles)


% hObject handle to datos (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of datos as text


% str2double(get(hObject,'String')) returns contents of datos as a
double

% --- Executes during object creation, after setting all properties.


function datos_CreateFcn(hObject, eventdata, handles)
% hObject handle to datos (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns
called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in lineal.


function lineal_Callback(hObject, eventdata, handles)
% hObject handle to lineal (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
X=eval(get(handles.x,'String'));
if get(handles.lineal,'Enable','on')
set(handles.cuadratica,'Enable','off');
set(handles.enesima,'Enable','off');
end
for i=1:n+1
set(handles.datos,'String',i-1);
x(i)=get(handles.datos,'String');
set(handles.datos,'String',i-1);
fx(i)=get(handles.datos,'String');
end
syms X;
FX1=fx(1)+((fx(2)-fx(1))/(x(2)-x(1)))*(X-x(1));
VI=eval(FX1);
end

% Hint: get(hObject,'Value') returns toggle state of lineal

% --- Executes on button press in cuadratica.


function cuadratica_Callback(hObject, eventdata, handles)
% hObject handle to cuadratica (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA
Y=eval(get(handles.x,'String'));
if get(handles.cuadratica,'Enable','on')
set(handles.lineal,'Enable','off');
set(handles.enesima,'Enable','off');
end
for i=1:n+1
set(handles.datos,'String',i-1);
x(i)=get(handles.datos,'String');
set(handles.datos,'String',i-1);
fx(i)=get(handles.datos,'String');
end
syms Y;
b=fx(1);
b1=(fx(2)-fx(1))/(x(2)-x(1));
b2=(((fx(3)-fx(2))/(x(3)-x(2)))-b1)/(x(3)-x(1));
FX2=b+b1*(Y-x(1))+b2*(Y-x(1))*(Y-x(2));
VI=eval(FX2);
end
% Hint: get(hObject,'Value') returns toggle state of cuadratica

% --- Executes on button press in enesima.


function enesima_Callback(hObject, eventdata, handles)
% hObject handle to enesima (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=eval(get(handles.x,'String'));
if get(handles.enesima,'Enable','on')
set(handles.lineal,'Enable','off');
set(handles.cuadratica,'Enable','off');
end
for i=1:n+1
set(handles.datos,'String',i-1);
x(i)=get(handles.datos,'String');
set(handles.datos,'String',i-1);
fx(i)=get(handles.datos,'String');
end
n=length(x);
dd=zeros(n);
dd(:,1)=fx;

for k=2:n
for j=k:n
dd(j,k)=(dd(j,k-1)-dd(j-1,k-1))/(x(j)-x(j-k+1));
end
end
pause
syms a
term1=1;
term2=0;
for i=1:n-1
term1=term1*(a-x(i));
term2=term2+dd(i+1,i+1)*term1;
end
pn=dd(1,1)+term2;
ipn=eval(pn);
x=x(1):0.1:x(n);
q=length(x);
for i=1:q
a=x(i);
epn(i)=eval(pn);
end
end
% Hint: get(hObject,'Value') returns toggle state of enesima

function x_Callback(hObject, eventdata, handles)


% hObject handle to x (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of x as text


% str2double(get(hObject,'String')) returns contents of x as a
double

% --- Executes during object creation, after setting all properties.


function x_CreateFcn(hObject, eventdata, handles)
% hObject handle to x (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns
called

% Hint: edit controls usually have a white background on Windows.


% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in interpolar.
function interpolar_Callback(hObject, eventdata, handles)
% hObject handle to interpolar (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
while get(handles.lineal,'Enable','on')
set(handles.pantalla,'String',VI);
while get(handles.cuadratica,'Enable','on')
set(handles.pantalla,'String',VI);
while get(handles.enesima,'Enable','on')
set(handles.pantalla,'String',z);

You might also like