Apendice de Practica 04

download Apendice de Practica 04

of 26

Transcript of Apendice de Practica 04

  • 7/27/2019 Apendice de Practica 04

    1/26

    APENDICEALGORITMO 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

  • 7/27/2019 Apendice de Practica 04

    2/26

    ALGORITMO DE INTERPOLACION EN GUIFUNCTION VARARGOUT = INTERPOLACION VARARGIN)% INTERPOLACION M-file for interpolacion.fig% INTERPOLACION, by itself, creates a new INTERPOLACION or raisesthe existing

    % singleton*.%% H = INTERPOLACION returns the handle to a new INTERPOLACION or thehandle to% the existing singleton*.%% INTERPOLACION('CALLBACK',hObject,eventData,handles,...) calls thelocal% function named CALLBACK in INTERPOLACION.M with the given inputarguments.%% INTERPOLACION('Property','Value',...) creates a new INTERPOLACIONor raises the

    % existing singleton*. Starting from the left, property value pairsare% applied to the GUI before interpolacion_OpeningFcn gets called.An% unrecognized property name or invalid value makes propertyapplication% stop. All inputs are passed to interpolacion_OpeningFcn viavarargin.%% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows onlyone% 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 EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...

    'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @interpolacion_OpeningFcn, ...'gui_OutputFcn', @interpolacion_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);

    ifnargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});

    end

    ifnargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});

    elsegui_mainfcn(gui_State, varargin{:});

    end

  • 7/27/2019 Apendice de Practica 04

    3/26

    % End initialization code - DO NOT EDIT

    % --- Executes just before interpolacion is made visible.functioninterpolacion_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');

    ifisnumeric(A)A=A;

    elseA=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 interpolacionhandles.output = hObject;

    % Update handles structureguidata(hObject, handles);

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

    % uiwait(handles.figure1);

    % --- Outputs from this function are returned to the command line.functionvarargout = 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 structurevarargout{1} = handles.output;

    % --- Executes on selection change in popupmenu1.functionpopupmenu1_Callback(hObject, eventdata, handles)u=get(handles.popupmenu1,'value');ifu==1

    set(handles.uitable1,'visible','off');

  • 7/27/2019 Apendice de Practica 04

    4/26

    set(handles.uitable5,'visible','on');set(handles.text9,'visible','off');

    elseifu==2set(handles.uitable1,'visible','off');

    set(handles.uitable5,'visible','on');set(handles.text9,'visible','off');

    elseifu==3set(handles.uitable1,'visible','off');

    set(handles.uitable5,'visible','on');set(handles.text9,'visible','off');

    elseifu==4set(handles.uitable1,'visible','off');set(handles.uitable5,'visible','on');

    set(handles.text9,'visible','off');elseifu==5

    set(handles.uitable1,'visible','on');set(handles.uitable5,'visible','off');

    set(handles.text9,'visible','on');elseifu==6

    set(handles.uitable1,'visible','off');

    set(handles.uitable5,'visible','on');set(handles.text9,'visible','off');

    end

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

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

    popupmenu1

    % --- Executes during object creation, after setting all properties.functionpopupmenu1_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 CreateFcnscalled

    % Hint: popupmenu controls usually have a white background on Windows.% See ISPC and COMPUTER.ifispc && isequal(get(hObject,'BackgroundColor'),

    get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');

    end

    % --- Executes on button press in pushbutton1.functionpushbutton1_Callback(hObject, eventdata, handles)

    valor=get(handles.popupmenu1,'value')

  • 7/27/2019 Apendice de Practica 04

    5/26

    switchvalor

    case1msgbox('elija una opcion','ERR0R')

    case2

    A=get(handles.uitable5,'Data');A=get(handles.uitable5,'Data');

    ifisnumeric(A)A=A;

    elseA=str2double(A);

    endx=A(:,1)'y=A(:,2)'symx = sym('x')

    u=symx%y=[-5 -6 -1 16]%x=9[1 4.2 5 5.6]f=xn = length(x);v = zeros(size(u));fork = 1:nw = ones(size(u));forj = [1:k-1 k+1:n]w = (u-x(j))./(x(k)-x(j)).*w;endv = v + w*y(k);endP=vP = 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 onplot(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);

    case3

    A=get(handles.uitable5,'Data');

  • 7/27/2019 Apendice de Practica 04

    6/26

    A=get(handles.uitable5,'Data');

    ifisnumeric(A)A=A;

    elseA=str2double(A);

    end x=A(:,1)'

    y=A(:,2)'

    r=str2double(get(handles.edit2,'String'));fori=1:length(x)-1

    ifx(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,'-')

    case4A=get(handles.uitable5,'Data');

    A=get(handles.uitable5,'Data');

    ifisnumeric(A)A=A;

    elseA=str2double(A);

    endx=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 programaxa=x;ya=y;% Formacion de las diferencias divididasd=zeros(length(y));d(:,1)=y';fork=2:length(x)

    forj=1:length(x)+1-kd(j,k)=(d(j+1,k-1)-d(j,k-1))/(x(j+k-1)-x(j));

    endend% Formacion del polinomio

  • 7/27/2019 Apendice de Practica 04

    7/26

    forw=1:length(x)ds=num2str(abs(d(1,w)));ifw>1

    ifx(w-1)

  • 7/27/2019 Apendice de Practica 04

    8/26

  • 7/27/2019 Apendice de Practica 04

    9/26

    hold onplot(u,v,'r')x=str2double(get(handles.edit2,'String'));

    g=eval(P)set(handles.edit3,'String',g);

    case6

    A=get(handles.uitable5,'Data');A=get(handles.uitable5,'Data');

    ifisnumeric(A)A=A;elseA=str2double(A);

    end

    x=A(:,1)'f=A(:,2)'

    s=xplot(x,f,'o')

    n=length(x)a=ffori=2:n

    h(1,i-1)=x(i)-x(i-1)

    endfori=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)

    fori=1:n-2

    A(i+1,i)=h(i)A(i+1,i+1)=2*(h(i)+h(i+1))

  • 7/27/2019 Apendice de Practica 04

    10/26

    A(i+1,i+2)=h(i+1)

    endalfa2(1,1)=0alfa2(1,2:length(alfa)+1)=alfaalfa2(1,length(alfa)+2)=0alfa2=alfa2'

    c=inv(A)*alfa2

    c=c'n=length(c)

    forj= 1:n-1b(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)))/3d(1,j)= ((c(1,j+1)-c(1,j)))/(3*(x(1,j+1)-x(1,j)))

    endx=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]

    forj= 1:n-1

    syms xpol=[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=eqx=s(1,j):0.01:s(1,j+1)eq=eval(eq)hold onplot(x,eq,'k')

    r=str2double(get(handles.edit2,'String'));

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

    x=rtt=eval(ecuacion)set(handles.edit3,'String',tt);

    else

    end

  • 7/27/2019 Apendice de Practica 04

    11/26

    end

    end

    % hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB

    % handles structure with handles and user data (see GUIDATA)

    % --- Executes on button press in radiobutton1.functionradiobutton1_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');

    ifisnumeric(A)

    A=A;elseA=str2double(A) ;

    end

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

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

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

    ifu==1n1=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');ifisnumeric(A)A=A;

    elseA=str2double(A) ;

    endelseifu==2n1=str2double(get(handles.edit1,'string'));

    matriz=cell(n1,2);matriz(:,:)={''};

  • 7/27/2019 Apendice de Practica 04

    12/26

    set(handles.uitable5,'Data',matriz);set(handles.uitable5,'ColumnEditable',true(1,n1));A=get(handles.uitable5,'Data');ifisnumeric(A)A=A;

    elseA=str2double(A) ;

    endelseifu==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');ifisnumeric(A)A=A;

    elseA=str2double(A) ;

    end

    elseifu==4n1=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');ifisnumeric(A)A=A;

    elseA=str2double(A) ;

    endelseifu==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');ifisnumeric(A)A=A;

    elseA=str2double(A) ;

    end

    elseifu==6n1=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');ifisnumeric(A)A=A;

  • 7/27/2019 Apendice de Practica 04

    13/26

    elseA=str2double(A) ;

    endend

    % hObject handle to edit1 (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 edit1 as text% str2double(get(hObject,'String')) returns contents of edit1 as adouble

    % --- Executes during object creation, after setting all properties.functionedit1_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 CreateFcnscalled

    % Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.ifispc && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');end

    % --- Executes on button press in pushbutton2.functionpushbutton2_Callback(hObject, eventdata, handles)

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

    % --- Executes when entered data in editable cell(s) in uitable1.functionuitable1_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 appropriatevalue for Data% handles structure with handles and user data (see GUIDATA)

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

  • 7/27/2019 Apendice de Practica 04

    14/26

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

    matriz=[1 2 3 4 10 31;16 18 21 17 15 12]'set(handles.uitable5,'Data',matriz);

    elseifu==2

    matriz=[1 2 3 4 5 6;16 18 21 17 15 12]'set(handles.uitable5,'Data',matriz);

    elseifu==3matriz=[1 2 3 4 5 6;16 18 21 17 15 12]'

    set(handles.uitable5,'Data',matriz);elseifu==4

    matriz=[1 2 3 4 5 6;16 18 21 17 15 12]'set(handles.uitable5,'Data',matriz);

    elseifu==5matriz=[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);elseifu==6

    matriz=[1 2 3 4 5 6;16 18 21 17 15 12]'set(handles.uitable5,'Data',matriz);end

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

    functionedit2_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 adouble

    % --- Executes during object creation, after setting all properties.functionedit2_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 CreateFcnscalled

    % Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.ifispc && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');end

  • 7/27/2019 Apendice de Practica 04

    15/26

    functionedit3_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 adouble

    % --- Executes during object creation, after setting all properties.functionedit3_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 CreateFcnscalled

    % Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.ifispc && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');end

    % --- Executes on button press in grids.functiongrids_Callback(hObject, eventdata, handles)die=get(hObject,'Value');handles.die=die;ifhandles.die==1

    set(handles.grids,'String','PLOT');else

    set(handles.grids,'String','PLT');end

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

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

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

  • 7/27/2019 Apendice de Practica 04

    16/26

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

    % --- Executes on mouse press over axes background.

    functionaxes5_ButtonDownFcn(hObject, eventdata, handles)

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

    % --- Executes on mouse press over figure background.functionfigure1_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 overtext12.functiontext12_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)

  • 7/27/2019 Apendice de Practica 04

    17/26

  • 7/27/2019 Apendice de Practica 04

    18/26

    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;fork=1:2*P+1

    a(k,1)=sum(x.^(k-1));endfork=1:M

    A(:,k)=a(k:M+k-1);endfork=1:M

    Y(k,1)=sum(fx.*x.^(k-1));endck=inv(A)*Y;ck=fliplr((ck.'));Fxp=polyval(ck,xp);plot(x,fx,'o',xp,Fxp,'r');legend('puntosiniciales','Polinomios interpolar')

  • 7/27/2019 Apendice de Practica 04

    19/26

    NEWTON

    %Interpolation de newtonclear;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');fori=1:n+1

    fprintf('x%.0f=',i-1);X(i)=input(' ');fprintf('y%.0f=',i-1);Y(i)=input(' ');

    endDD=zeros(n+1);DD(:,1)=Y;fork=2:n+1

    forJ=k:n+1DD(J,k)=[DD(J,k-1)-DD(J-1,k-1)]/[X(J)-X(J-k+1)];

    endenddisp('La matriz de diferencias divididas es:');disp(DD);disp('El polinomio de newton es');syms x;polnew=DD(1,1);P=1;fori=1:n

    P=P*(x-X(i));polnew=polnew+P*DD(i+1,i+1);

    endpolnew=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+');

  • 7/27/2019 Apendice de Practica 04

    20/26

    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,:));fori=1:n;a(i)=X(2,i);

    endfori=1:n-1;

    h(i)=X(1,i+1)-X(1,i);endfori=2:n-1;

    alfa(i)=3/h(i)*(a(i+1)-a(i))-3/h(i-1)*(a(i)-a(i-1));endl(1)=1;mu(1)=0;z(1)=0;fori=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);

    endl(n)=1;z(n)=0;c(n)=0;fori=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));

    endfori=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');

    endfori=1:n;

    hold on;plot (X(1,i),X(2,i),'*','MarkerEdgeColor','r','LineWidth',1);title('Interpolacin por "splines" de orden 3.');

    end

  • 7/27/2019 Apendice de Practica 04

    21/26

    TCHEBYSHEV

    clc,clear;format longg%a=input('ingrese el limite einferior')

    %b=input('ingrese el limite superior')%N=input('ingrese el numero de terminos o puntos de ortogonalidad')a=8b=12N=17;P=N-1;T=cell(1,N);T(1:2)={[1],[1 0]};fork=2:N-1

    T{k+1}=[2*T{k} 0]-[0 0 T{k-1}];endTchebyshev(N,N)=zeros;fork=1:N

    Tchebyshev(k,1:k)=T{k};endTshe=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=[];forl=1:N

    Ren(N,N)=zeros;fork=1:N

    form=k:-1:1

    Ren(m,k)=x(1).^(k-m);endendM=Tshe.*Ren;C=(sum(M));Pe=[Pe;C];

    endShev=[Shev Pe];B=(2/(N-1)).*Shev(1,2)*Shev(1,4:3+N)/2;fork=2:N-1B=B+(2/(N-1)).*Shev(k,2).*Shev(k,4:N+3);endB=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;fork=1:N

    CO(:,k)=A(k)*Tshe(:,k);endfork=1:N

    Coef(k)=sum(diag(CO,k-1));endw=-1:0.01:1;fx=0;

  • 7/27/2019 Apendice de Practica 04

    22/26

    fork=1:Nfx=fx+Coef(k)*w.^(k-1);

    endq=(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=[];fork=1:N

    V=[V x.^(k-1)];endA=inv(V)*fx;A=A.';

    A=fliplr(A);Fxp=polyval(A,xp);plot(x,fx,'o',xp,Fxp,'r');legend('puntosiniciales','Polinomios interpolar')

  • 7/27/2019 Apendice de Practica 04

    23/26

    HERMITclear 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);fori=1:nz(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));ifi~=1

    Q(2i-1,2)=(Q(2i-1,1)-Q(2i-2,1))/(z(i)-z(i-j+1));end

    endfori=3:2n

    j=3:iQ(i,j)=(Q(i,j-1)-Q(i-1,j-1))/(z(i)-z(i-j+1));

    endx(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];fork=1:P

    form=1:N-kNum=Tb(m+1,k+1)-Tb(m,k+1);Den=Tb(m+k,1)-Tb(m,1);Tb(m,k+2)=Num/Den;

    endend

    Newton=Tb(1,2:P+2);fork=1:N

    orden=x(1:k-1);Pol=Newton(k)*poly(orden);Coef(k,N-k+1:N)=Pol;

    endPol=sum(Coef);Fxp=polyval(Pol,xp);plot(x,fx,'o',xp,Fxp,'r');legend('puntosiniciales','Polinomios interpolar')

  • 7/27/2019 Apendice de Practica 04

    24/26

    DIFERENCIAS DIVIDIDASclear;clcx=[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';fork=2:length(x)

    forj=1:length(x)+1-kd(j,k)=(d(j+1,k-1)-d(j,k-1))/(x(j+k-1)-x(j));

    endend% Formacion del polinomioforw=1:length(x)

    ds=num2str(abs(d(1,w)));ifw>1

    ifx(w-1)

  • 7/27/2019 Apendice de Practica 04

    25/26

    pausexg=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 sernutilizadas 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 yd(:,1)=y';%Se asigna valor de y a la columna de la matriz creada anteriormentefork=2:length(x)%se hacen dos ciclos, uno dentro de otro, donde van variando los valoresde j y k, y va entregando cuales son los valores que se utilizaran enforj=1:length(x)+1-kd(j,k)=(d(j+1,k-1)-d(j,k-1))/(x(j+k-1)-x(j));endendforw=1:length(x)ds=num2str(abs(d(1,w)));%los valores numericos de la matriz ds en las coordenadas (1,w) setransforman en string, cuyos valores no sern modificados despus, y

    sern mostrados al final en presentacin de resultadosifw>1%si w es mayor que 1ifx(w-1)

  • 7/27/2019 Apendice de Practica 04

    26/26

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