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

APENDICE

ALGORITMO DE INTERPOLACION EN GUI ..................................................................... 2 FUNCTION VARARGOUT = INTERPOLACION(VARARGIN) .................................. 2 ALGORITMO DE INTEPOLACION EN SCRIP ................................................................ 17 LAGRANGE............................................................................................................................... 17 MINIMO CUADRADO............................................................................................................ 18 NEWTON .................................................................................................................................... 19 SPLINE CUBICO .................................................................................................................... 20 TCHEBYSHEV ......................................................................................................................... 21 VANDERMONDE .................................................................................................................... 22 HERMIT ...................................................................................................................................... 23 DIFERENCIAS DIVIDIDAS (NEWTON) .......................................................................... 23 DIFERENCIAS DIVIDIDAS ................................................................................................. 24 DIFERENCIAS FINITAS....................................................................................................... 25

ALGORITMO DE INTERPOLACION EN GUI


FUNCTION VARARGOUT = INTERPOLACION(VARARGIN)
% INTERPOLACION M-file for interpolacion.fig % INTERPOLACION, by itself, creates a new INTERPOLACION or raises the existing % singleton*. % % H = INTERPOLACION returns the handle to a new INTERPOLACION or the handle to % the existing singleton*. % % INTERPOLACION('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in INTERPOLACION.M with the given input arguments. % % INTERPOLACION('Property','Value',...) creates a new INTERPOLACION or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before interpolacion_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to interpolacion_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 interpolacion % Last Modified by GUIDE v2.5 24-Dec-2013 12:04:02 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @interpolacion_OpeningFcn, ... 'gui_OutputFcn', @interpolacion_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 interpolacion is made visible. function interpolacion_OpeningFcn(hObject, eventdata, handles, varargin) n1=str2double(get(handles.edit1,'string')) matriz=cell(n1,2) %matriz(:,:)={''} set(handles.uitable1,'Data',matriz); set(handles.uitable1,'ColumnEditable',true(1,n1)); A=get(handles.uitable1,'Data'); if isnumeric(A) A=A; else A=str2double(A) ; end % % % % % 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 interpolacion (see VARARGIN)

% Choose default command line output for interpolacion handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes interpolacion wait for user response (see UIRESUME) % uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line. function varargout = interpolacion_OutputFcn(hObject, eventdata, handles) set(handles.uitable1,'visible','off'); set(handles.uitable5,'visible','on'); set(handles.text9,'visible','off'); % 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;

% --- Executes on selection change in popupmenu1. function popupmenu1_Callback(hObject, eventdata, handles) u=get(handles.popupmenu1,'value'); if u==1 set(handles.uitable1,'visible','off');

set(handles.uitable5,'visible','on'); set(handles.text9,'visible','off'); elseif u==2 set(handles.uitable1,'visible','off'); set(handles.uitable5,'visible','on'); set(handles.text9,'visible','off'); elseif u==3 set(handles.uitable1,'visible','off'); set(handles.uitable5,'visible','on'); set(handles.text9,'visible','off'); elseif u==4 set(handles.uitable1,'visible','off'); set(handles.uitable5,'visible','on'); set(handles.text9,'visible','off'); elseif u==5 set(handles.uitable1,'visible','on'); set(handles.uitable5,'visible','off'); set(handles.text9,'visible','on'); elseif u==6 set(handles.uitable1,'visible','off'); set(handles.uitable5,'visible','on'); set(handles.text9,'visible','off'); end

% hObject % eventdata % handles

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

% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array % contents{get(hObject,'Value')} returns selected item from popupmenu1

% --- Executes during object creation, after setting all properties. function popupmenu1_CreateFcn(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: popupmenu 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 pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) valor=get(handles.popupmenu1,'value')

switch valor case 1 msgbox('elija una opcion','ERR0R') case 2 A=get(handles.uitable5,'Data'); A=get(handles.uitable5,'Data'); if isnumeric(A) A=A; else A=str2double(A); end x=A(:,1)' y=A(:,2)' symx = sym('x') u=symx %y=[-5 -6 -1 16] %x=9[1 4.2 5 5.6] f=x n = length(x); v = zeros(size(u)); for k = 1:n w = ones(size(u)); for j = [1:k-1 k+1:n] w = (u-x(j))./(x(k)-x(j)).*w; end v = v + w*y(k); end P=v P = simplify(P) r=sort(f) a=r(1) b=r(length(f)) x=a:.01:ceil(b); u=x; v=eval(P); plot(f,y,'o') hold on plot(u,v,'m') %plot(x,y,'o',u,v,'-') xlabel('X') ylabel('Y') title(['Grafica f(x)=',4]); x=str2double(get(handles.edit2,'String')); g=eval(P) set(handles.edit3,'String',g); case 3 A=get(handles.uitable5,'Data');

A=get(handles.uitable5,'Data'); if isnumeric(A) A=A; else A=str2double(A); end x=A(:,1)' y=A(:,2)' r=str2double(get(handles.edit2,'String')); for i=1:length(x)-1 if x(i+1)>r & r>x(i) m=(y(i+1)-y(i))/(x(i+1)-x(i)) u=m*(r-x(i))+y(i) set(handles.edit3,'String',u); else end

end plot(x,y,'o',x,y,'-') case 4 A=get(handles.uitable5,'Data'); A=get(handles.uitable5,'Data'); if isnumeric(A) A=A; else A=str2double(A); end x=A(:,1)' y=A(:,2)'

%x=[0 1 2 3];y=[1 2 2.5 4]; % entrada de datos. % No tienes que digitar modificar ms nada. %Cuerpo del programa xa=x;ya=y; % Formacion de las diferencias divididas 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 % Formacion del polinomio

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]; acum=simplify( acum)

end end % Presentacion de resultados 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);

xg=linspace(min(xa),max(xa)); x=xg;yg=eval(acum); plot(xg,yg,xa,ya,'o'); grid on x=str2double(get(handles.edit2,'String')); g=eval(acum) set(handles.edit3,'String',g); case 5

A=get(handles.uitable1,'Data'); A=get(handles.uitable1,'Data'); if isnumeric(A) A=A; else A=str2double(A); end symx = sym('x'); u=symx; x=A(:,1)' y=A(:,2)' deriv=A(:,3)'

f=x; n = length(x); v = zeros(size(u)); for k = 1:n w = ones(size(u)); for j = [1:k-1 k+1:n]; w = (u-x(j))./(x(k)-x(j)).*w; end L(1,k)=w; end symx = sym('x'); u=symx; D=diff(L); n = length(L); for k = 1:n v=D(1,k) x=val(1,k) v=eval(v) H(1,k)=(1-2*(u-x)*v)*(L(1,k)^2) HP(1,k)=(u-x)*(L(1,k)^2) end P=simplify(sum(H.*y+HP.*deriv)) r=sort(f) a=r(1) b=r(length(f)) x=a:.01:ceil(b); u=x; v=eval(P); plot(f,y,'o')

hold on plot(u,v,'r') x=str2double(get(handles.edit2,'String')); g=eval(P) set(handles.edit3,'String',g);

case 6

A=get(handles.uitable5,'Data'); A=get(handles.uitable5,'Data'); if isnumeric(A) A=A; else A=str2double(A); end x=A(:,1)' f=A(:,2)'

s=x plot(x,f,'o') n=length(x) a=f for i=2:n h(1,i-1)=x(i)-x(i-1) end for i=3:n alfa(1,i-2)=(3/h(i-1))*(a(i)-a(i-1))-(3/h(i-2))*(a(i-1)-a(i-2)) end A=eye(n)

for i=1:n-2 A(i+1,i)=h(i) A(i+1,i+1)=2*(h(i)+h(i+1))

A(i+1,i+2)=h(i+1) end alfa2(1,1)=0 alfa2(1,2:length(alfa)+1)=alfa alfa2(1,length(alfa)+2)=0 alfa2=alfa2' c=inv(A)*alfa2 c=c' n=length(c) for j= 1:n-1 b(1,j)= (f(1,j+1)-f(1,j))/(x(1,j+1)-x(1,j))-(x(1,j+1)x(1,j))*((c(1,j+1)+2*c(1,j)))/3 d(1,j)= ((c(1,j+1)-c(1,j)))/(3*(x(1,j+1)-x(1,j))) end x=x(1,1:n-1)' a=a(1,1:n-1)' b=b' c=c(1,1:n-1)' d=d' mtx=[ x a b c d] for j= 1:n-1 syms x pol=[1 (x-mtx(j,1)) (x-mtx(j,1))^2 (x-mtx(j,1))^3] eq=pol.*mtx(j,2:5) eq=(sum(eq)) ecuacion=eq x=s(1,j):0.01:s(1,j+1) eq=eval(eq) hold on plot(x,eq,'k') r=str2double(get(handles.edit2,'String'));

if s(1,j+1)>r & r>s(1,j)

x=r tt=eval(ecuacion) set(handles.edit3,'String',tt); else end

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

% --- Executes on button press in radiobutton1. function radiobutton1_Callback(hObject, eventdata, handles) n1=str2double(get(handles.edit1,'string')) matriz=cell(n1,2) %matriz(:,:)={''} set(handles.uitable1,'Data',matriz); set(handles.uitable1,'ColumnEditable',true(1,n1)); A=get(handles.uitable1,'Data'); if isnumeric(A) A=A; else A=str2double(A) ; end % hObject % eventdata % handles handle to radiobutton1 (see GCBO) reserved - to be defined in a future version of MATLAB structure with handles and user data (see GUIDATA)

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

function edit1_Callback(hObject, eventdata, handles) u=get(handles.popupmenu1,'value');

if u==1 n1=str2double(get(handles.edit1,'string')); matriz=cell(n1,2); matriz(:,:)={''}; set(handles.uitable5,'Data',matriz); set(handles.uitable5,'ColumnEditable',true(1,n1)); A=get(handles.uitable5,'Data'); if isnumeric(A) A=A; else A=str2double(A) ; end elseif u==2 n1=str2double(get(handles.edit1,'string')); matriz=cell(n1,2); matriz(:,:)={''};

set(handles.uitable5,'Data',matriz); set(handles.uitable5,'ColumnEditable',true(1,n1)); A=get(handles.uitable5,'Data'); if isnumeric(A) A=A; else A=str2double(A) ; end elseif u==3 n1=str2double(get(handles.edit1,'string')); matriz=cell(n1,2); matriz(:,:)={''}; set(handles.uitable5,'Data',matriz); set(handles.uitable5,'ColumnEditable',true(1,n1)); A=get(handles.uitable5,'Data'); if isnumeric(A) A=A; else A=str2double(A) ; end elseif u==4 n1=str2double(get(handles.edit1,'string')); matriz=cell(n1,2); matriz(:,:)={''}; set(handles.uitable5,'Data',matriz); set(handles.uitable5,'ColumnEditable',true(1,n1)); A=get(handles.uitable5,'Data'); if isnumeric(A) A=A; else A=str2double(A) ; end elseif u==5 n1=str2double(get(handles.edit1,'string')); matriz=cell(n1,3); matriz(:,:)={''}; set(handles.uitable1,'Data',matriz); set(handles.uitable1,'ColumnEditable',true(1,n1)); A=get(handles.uitable1,'Data'); if isnumeric(A) A=A; else A=str2double(A) ; end elseif u==6 n1=str2double(get(handles.edit1,'string')); matriz=cell(n1,2); matriz(:,:)={''}; set(handles.uitable5,'Data',matriz); set(handles.uitable5,'ColumnEditable',true(1,n1)); A=get(handles.uitable5,'Data'); if isnumeric(A) A=A;

else A=str2double(A) ; end end

% hObject % eventdata % handles

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

% Hints: get(hObject,'String') returns contents of edit1 as text % str2double(get(hObject,'String')) returns contents of edit1 as a double

% --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles) % hObject handle to edit1 (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 pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) % hObject % eventdata % handles handle to pushbutton2 (see GCBO) reserved - to be defined in a future version of MATLAB structure with handles and user data (see GUIDATA)

% --- Executes when entered data in editable cell(s) in uitable1. function uitable1_CellEditCallback(hObject, eventdata, handles) % hObject handle to uitable1 (see GCBO) % eventdata structure with the following fields (see UITABLE) % Indices: row and column indices of the cell(s) edited % PreviousData: previous data for the cell(s) edited % EditData: string(s) entered by the user % NewData: EditData or its converted form set on the Data property. Empty if Data was not changed % Error: error string when failed to convert EditData to appropriate value for Data % handles structure with handles and user data (see GUIDATA)

% --- Executes on button press in pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles)

u=get(handles.popupmenu1,'value'); if u==1

matriz=[1 2 3 4 10 31;16 18 21 17 15 12]' set(handles.uitable5,'Data',matriz); elseif u==2 matriz=[1 2 3 4 5 6;16 18 21 17 15 12]' set(handles.uitable5,'Data',matriz); elseif u==3 matriz=[1 2 3 4 5 6;16 18 21 17 15 12]' set(handles.uitable5,'Data',matriz); elseif u==4 matriz=[1 2 3 4 5 6;16 18 21 17 15 12]' set(handles.uitable5,'Data',matriz); elseif u==5 matriz=[1 2 3 4 5 6;16 18 21 17 15 12;-1 -0.5698959 -0.5811571 1 1.2 1.8]' set(handles.uitable1,'Data',matriz); elseif u==6 matriz=[1 2 3 4 5 6;16 18 21 17 15 12]' set(handles.uitable5,'Data',matriz); end % hObject % eventdata % handles handle to pushbutton4 (see GCBO) reserved - to be defined in a future version of MATLAB structure with handles and user data (see GUIDATA)

function edit2_Callback(hObject, eventdata, handles) % hObject handle to edit2 (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 edit2 as text % str2double(get(hObject,'String')) returns contents of edit2 as a double

% --- Executes during object creation, after setting all properties. function edit2_CreateFcn(hObject, eventdata, handles) % hObject handle to edit2 (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

function edit3_Callback(hObject, eventdata, handles) % hObject handle to edit3 (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 edit3 as text % str2double(get(hObject,'String')) returns contents of edit3 as a double

% --- Executes during object creation, after setting all properties. function edit3_CreateFcn(hObject, eventdata, handles) % hObject handle to edit3 (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 grids. function grids_Callback(hObject, eventdata, handles) die=get(hObject,'Value'); handles.die=die; if handles.die==1 set(handles.grids,'String','PLOT'); else set(handles.grids,'String','PLT'); end

% hObject % eventdata % handles

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

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

% --- Executes on button press in pushbutton5. function pushbutton5_Callback(hObject, eventdata, handles)

% hObject % eventdata % handles

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

% --- Executes on mouse press over axes background. function axes5_ButtonDownFcn(hObject, eventdata, handles) % hObject % eventdata % handles handle to axes5 (see GCBO) reserved - to be defined in a future version of MATLAB structure with handles and user data (see GUIDATA)

% --- Executes on mouse press over figure background. function figure1_ButtonDownFcn(hObject, eventdata, handles) % hObject handle to figure1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% --- If Enable == 'on', executes on mouse press in 5 pixel border. % --- Otherwise, executes on mouse press in 5 pixel border or over text12. function text12_ButtonDownFcn(hObject, eventdata, handles) % hObject handle to text12 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

ALGORITMO DE INTEPOLACION EN SCRIP


LAGRANGE
clc,clear %x=imput('ingrese el vector de x') x=[2;4;6;8;10;12;14]; %fx=input('ingrese el valor de f(x)') fx=[5;9;2;1;6;4;9]; %xp=input('ingrese el intervalo de la grafica') xp=(2:0.01:14); N=length(x); for k=1:N for m=1:N if k==m ind=1; else FN(k,m-ind)=x(m); FD(k,m-ind)=x(k)-x(m); end end ind=0; end for k=1:N CN(k,:)=poly(FN(k,:)); end Fac=prod(FD,2); for k=1:N Multi(k,:)=CN(k,:)*fx(k)/Fac(k); end Coef=sum(Multi); Fxp=polyval(Coef,xp); plot(x,fx,'o',xp,Fxp,'r'); legend('puntosiniciales','Polinomios interpolar')

MINIMO CUADRADO
clc,clear %x=imput('ingrese el vector de x') x=[2;4;6;8;10;12;14]; %fx=input('ingrese el valor de f(x)') fx=[5;9;2;1;6;4;9]; %xp=input('ingrese el intervalo de la grafica') xp=(2:0.01:14); N=length(x); P=N-4; M=P+1; for k=1:2*P+1 a(k,1)=sum(x.^(k-1)); end for k=1:M A(:,k)=a(k:M+k-1); end for k=1:M Y(k,1)=sum(fx.*x.^(k-1)); end ck=inv(A)*Y; ck=fliplr((ck.')); Fxp=polyval(ck,xp); plot(x,fx,'o',xp,Fxp,'r'); legend('puntosiniciales','Polinomios interpolar')

NEWTON
%Interpolation de newton clear;clc; disp('metodos numericos'); disp('interpolacion'); disp('interpolacion'); 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 de diferencias divididas es:'); disp(DD); disp('El polinomio 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+');

SPLINE CUBICO
function [a,b,c,d]=spline3(X) %Pasos bsicos del algoritmo obtenidos del libro Anlisis Numrico de %Richard Burden, 2a. Edicin, Grupo Editorial Iberoamrica. 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 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

TCHEBYSHEV
clc,clear; format long g %a=input('ingrese el limite einferior') %b=input('ingrese el limite superior') %N=input('ingrese el numero de terminos o puntos de ortogonalidad') a=8 b=12 N=17; P=N-1; T=cell(1,N); T(1:2)={[1],[1 0]}; for k=2:N-1 T{k+1}=[2*T{k} 0]-[0 0 T{k-1}]; end Tchebyshev(N,N)=zeros; for k=1:N Tchebyshev(k,1:k)=T{k}; end Tshe=Tchebyshev.'; x=(cos(((N-1:-1:0)*pi)/(N-1))).'; %z=(1/2) [(b-a)x+b+a]. z=(1/2).*((b-a).*x+b+a); fz=2.*z-z.^2+z.^3.*sin(z).*exp(-z./10); Shev=[]; Shev=[Shev z fz x]; Pe=[]; for l=1:N Ren(N,N)=zeros; for k=1:N for m=k:-1:1 Ren(m,k)=x(1).^(k-m); end end M=Tshe.*Ren; C=(sum(M)); Pe=[Pe;C]; end Shev=[Shev Pe]; B=(2/(N-1)).*Shev(1,2)*Shev(1,4:3+N)/2; for k=2:N-1 B=B+(2/(N-1)).*Shev(k,2).*Shev(k,4:N+3); end B=B+(2/(N-1)).*Shev(N,2)*Shev(N,4:N+3)/2; A(1)=B(1)/2; A(2:N-1)=B(2:N-1); A(N)=B(N)/2; for k=1:N CO(:,k)=A(k)*Tshe(:,k); end for k=1:N Coef(k)=sum(diag(CO,k-1)); end w=-1:0.01:1; fx=0;

for k=1:N fx=fx+Coef(k)*w.^(k-1); end q=(1/2).*((b-a).*w+b+a); fq=2.*q-q.^2+q.^3.*sin(q).*exp(-q./10); plot(z,fz,'o',q,fx,'r',q,fq,'--'); legend('Puntos creados parahacer el interpolador','interpolador de Tchevishev','funcion analitica')

VANDERMONDE
clc,clear %x=imput('ingrese el vector de x') x=[2;4;6;8;10;12;14]; %fx=input('ingrese el valor de f(x)') fx=[5;9;2;1;6;4;9]; %xp=input('ingrese el intervalo de la grafica') xp=(2:0.01:14); N=length(x); V=[]; for k=1:N V=[V x.^(k-1)]; end A=inv(V)*fx; A=A.'; A=fliplr(A); Fxp=polyval(A,xp); plot(x,fx,'o',xp,Fxp,'r'); legend('puntosiniciales','Polinomios interpolar')

HERMIT
clear all; x=input('Ingrese los valores de x='); F=input('Ingrese los valores de F(x)='); DF=input('ingrese la derivada de D(x)='); n=length(x); Q=zeros(2,n); for i=1:n z(2i-1)=x(i); z(2i)=x(i); Q(2i-1,1)=F(x(i)); Q(2i,1)=F(x(i)); Q(2i,2)=DF(x(i)); if i~=1 Q(2i-1,2)=(Q(2i-1,1)-Q(2i-2,1))/(z(i)-z(i-j+1)); end end for i=3:2n j=3:i Q(i,j)=(Q(i,j-1)-Q(i-1,j-1))/(z(i)-z(i-j+1)); end x(i)=x(i)'; [x(i),Q]

DIFERENCIAS DIVIDIDAS (NEWTON)


clc,clear %x=imput('ingrese el vector de x') x=[2;4;6;8;10;12;14]; %fx=input('ingrese el valor de f(x)') fx=[5;9;2;1;6;4;9]; %xp=input('ingrese el intervalo de la grafica') xp=(2:0.01:14); N=length(x); P=N-1; Tb=zeros(N,P); Tb=[x fx Tb]; for k=1:P for m=1:N-k Num=Tb(m+1,k+1)-Tb(m,k+1); Den=Tb(m+k,1)-Tb(m,1); Tb(m,k+2)=Num/Den; end end Newton=Tb(1,2:P+2); for k=1:N orden=x(1:k-1); Pol=Newton(k)*poly(orden); Coef(k,N-k+1:N)=Pol; end Pol=sum(Coef); Fxp=polyval(Pol,xp); plot(x,fx,'o',xp,Fxp,'r'); legend('puntosiniciales','Polinomios interpolar')

DIFERENCIAS DIVIDIDAS
clear;clc x=[0 1 2 3];y=[1 2 2.5 4]; % entrada de datos xa=x;ya=y; % Formacion de las diferencias divididas 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 % Formacion del polinomio 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 % Presentacion de resultados 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); % Grafica de los puntos 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

DIFERENCIAS FINITAS
% ingreso de datos. x=[-1 0 1 2 3];y=[3 0 -1 1 2]; xa=x;ya=y; %se le dan los valores de xa e ya a las variables x e y que sern utilizadas mas adelante. % Formacion de las diferencias divididas a travs de ciclos for e if. d=zeros(length(y)); %zeros(n) genera una matriz de nxn con todos sus valores iguales a cero, en este caso utiliza el largo del arreglo y d(:,1)=y'; %Se asigna valor de y a la columna de la matriz creada anteriormente for k=2:length(x) %se hacen dos ciclos, uno dentro de otro, donde van variando los valores de j y k, y va entregando cuales son los valores que se utilizaran en 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))); %los valores numericos de la matriz ds en las coordenadas (1,w) se transforman en string, cuyos valores no sern modificados despus, y sern mostrados al final en presentacin de resultados if w>1 %si w es mayor que 1 if x(w-1)<0 %si la multiplicacin de x por (w-1)es menor a cero, se agrega un smbolo + al polinomio de resultado. sg1='+'; else %en caso contrario, el smbolo matemtico es -. sg1='-'; end end if d(1,w)<0 sg2='-'; else sg2='+'; end if w==1 acum=num2str(d(1,1)); %se crea un contador de nombre acum que ir almacenando el polinomio obtenido, y lo mostrar el final del codigo elseif w==2

polinact=['(x' sg1 num2str(abs(x(w-1))) ')' ]; actual=[ds '*' polinact]; acum=[acum sg2 actual]; else polinact=[polinact '.*' '(x' sg1 num2str(abs(x(w-1))) ')' ]; actual=[ds '*' polinact]; acum=[acum sg2 actual]; end end % Presentacion de resultados fprintf('los valores de X e Y son'); disp(xa); disp(ya); %imprimir en pantalla el polinomio obtenido fprintf('El polinomio interpolacin Newton obtenido es: %s ',acum);

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