Usando Archivos Interpolacion

Embed Size (px)

Citation preview

  • 7/29/2019 Usando Archivos Interpolacion

    1/3

    Anlisis NumricoUniversidad Nacional de Misiones

    Mario R. ROSENBERGER 1 de3

    Usando los archivos para interpolacin

    Funciones para interpolacin

    Las siguientes funciones han sido extradas del libroAPPLIED NUMERICAL METHODS USINGMATLAB, W. Y . Yang, W. Cao, T.-S. Chung, J .Morris. Wiley Interscience, 2005. USA. No se hara unadescripcin de los algoritmos, sino ms bien se emplearn para estudiar las caractersticas de losmtodos de Interpolacin por polinomios de Lagrange y de Newton.

    lagranp:

    La siguiente funcin contiene el algoritmo para calcular los coeficientes del polinomio deLagrange, para utilizarlo debe cargase en un mdulo m.

    function [l,L] = lagranp(x,y)%Input : x =[x0 x1 ... xN], y =[y0 y1 ... yN]%Output: l =Lagrange polynomial coefficients of degree N% L =Lagrange coefficient polynomialN =length(x)-1; %the degree of polynomiall =0;for m =1:N +1P = 1;for k = 1:N + 1if k ~=m, P =conv(P,[1 -x(k)])/(x(m)-x(k)); endendL(m,:) =P; %Lagrange coefficient polynomiall =l +y(m)*P; %Lagrange polynomial (3.1.3)

    end

    usando lagranp

    Los siguientes comandos pueden ejecutarse desde la lnea de comandos o desde un mdulo m.

    %do_lagranp.mx =[-2 -1 1 2]; y =[-6 0 0 6]; % given data pointsl =lagranp(x,y) % find the Lagrange polynomialxx =[-2: 0.02 : 2]; yy =polyval(l,xx); %interpolate%clf,plot(xx,yy,'b', x,y,'*') %plot the graph

    *************************************************************************************************************

    newtonp

    La siguiente funcin contiene el algoritmo para calcular los coeficientes del polinomio de Newton,para utilizarlo debe cargase en un mdulo m.

    function [n,DD] = newtonp(x,y)%Input : x =[x0 x1 ... xN]% y = [y0 y1 ... yN]%Output: n =Newton polynomial coefficients of degree NN = length(x)-1;DD = zeros(N + 1,N + 1);

    DD(1:N + 1,1) = y';

  • 7/29/2019 Usando Archivos Interpolacion

    2/3

    Usando los ... Interpolacin

    Mario R. ROSENBERGER

    2de3

    for k = 2:N + 1for m =1: N +2 - k %Divided Difference TableDD(m,k) =(DD(m +1,k - 1) - DD(m,k - 1))/(x(m +k - 1)- x(m));endenda = DD(1,:); %Eq.(3.2.6)

    n =a(N+1); %Begin with Eq.(3.2.7)for k = N:-1:1 %Eq.(3.2.7)n =[n a(k)] - [0 n*x(k)]; %n(x)*(x - x(k - 1))+a_k - 1end

    usando newtonp

    Los siguientes comandos pueden ejecutarse desde la lnea de comandos o desde un mdulo m.

    %do_newtonp.mx =[-2 -1 1 2 4]; y =[-6 0 0 6 60];n =newtonp(x,y) %l =lagranp(x,y) for comparisonx =[-1 -2 1 2 4]; y =[ 0 -6 0 6 60];

    n1 =newtonp(x,y) %with the order of data changed for comparisonxx =[-2:0.02: 2]; yy =polyval(n,xx);%clf,plot(xx,yy,'b-',x,y,'*')

    y tambin

    function y =f31(x)y=1./(1+8*x. 2);

    %do_newtonp1.m plot Fig.3.2x =[-1 -0.5 0 0.5 1.0]; y =f31(x);n =newtonp(x,y)xx =[-1:0.02: 1]; %the interval to look overyy =f31(xx); %graph of the true functionyy1 =polyval(n,xx); %graph of the approximate polynomial functionsubplot(221), plot(xx,yy,'k-', x,y,'o', xx,yy1,'b')subplot(222), plot(xx,yy1-yy,'r') %graph of the error function

    *************************************************************************************************************

    cspline

    function [yi,S] =cspline(x,y,xi,KC,dy0,dyN)%This function finds the cubic splines for the input data points (x,y)%Input: x = [x0 x1 ... xN], y = [y0 y1 ... yN], xi=interpolation points% KC =1/2 for 1st/2nd derivatives on boundary specified% KC =3 for 2nd derivative on boundary extrapolated% dy0 =S(x0) =S01: initial derivative% dyN =S(xN) =SN1: final derivative%Output: S(n,k); n =1:N, k =1,4 in descending orderif nargin

  • 7/29/2019 Usando Archivos Interpolacion

    3/3

    Usando los ... Interpolacin

    Mario R. ROSENBERGER

    3de3

    A = zeros(N + 1,N + 1); b = zeros(N + 1,1);S =zeros(N +1,4); % Cubic spline coefficient matrixk =1:N; h(k) =x(k +1) - x(k); dy(k) =(y(k +1) - y(k))/h(k);% Boundary conditionif KC