Vous êtes sur la page 1sur 3

Anlisis Numrico

Universidad Nacional de Misiones

Usando los archivos para interpolacin

Funciones para interpolacin


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

lagranp:
La siguiente funcin contiene el algoritmo para calcular los coeficientes del polinomio de Lagrange, 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 polynomial N = length(x)-1; %the degree of polynomial l = 0; for m = 1:N + 1 P = 1; for k = 1:N + 1 if k ~= m, P = conv(P,[1 -x(k)])/(x(m)-x(k)); end end L(m,:) = P; %Lagrange coefficient polynomial l = 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.m x = [-2 -1 1 2]; y = [-6 0 0 6]; % given data points l = lagranp(x,y) % find the Lagrange polynomial xx = [-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 N N = length(x)-1; DD = zeros(N + 1,N + 1); DD(1:N + 1,1) = y'; Mario R. ROSENBERGER

1 de 3

Usando los ... Interpolacin


for k = 2:N + 1 for m = 1: N + 2 - k %Divided Difference Table DD(m,k) = (DD(m + 1,k - 1) - DD(m,k - 1))/(x(m + k - 1)- x(m)); end end a = 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 - 1 end usando newtonp

2 de 3

Los siguientes comandos pueden ejecutarse desde la lnea de comandos o desde un mdulo m. %do_newtonp.m x = [-2 -1 1 2 4]; y = [-6 0 0 6 60]; n = newtonp(x,y) %l = lagranp(x,y) for comparison x = [-1 -2 1 2 4]; y = [ 0 -6 0 6 60]; n1 = newtonp(x,y) %with the order of data changed for comparison xx = [-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.2 x = [-1 -0.5 0 0.5 1.0]; y = f31(x); n = newtonp(x,y) xx = [-1:0.02: 1]; %the interval to look over yy = f31(xx); %graph of the true function yy1 = polyval(n,xx); %graph of the approximate polynomial function subplot(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 order if nargin < 6, dyN = 0; end, if nargin < 5, dy0 = 0; end if nargin < 4, KC = 0; end N = length(x) - 1; % constructs a set of equations w.r.t. {S(n,2), n = 1:N + 1}

Mario R. ROSENBERGER

Usando los ... Interpolacin


A = zeros(N + 1,N + 1); b = zeros(N + 1,1); S = zeros(N + 1,4); % Cubic spline coefficient matrix k = 1:N; h(k) = x(k + 1) - x(k); dy(k) = (y(k + 1) - y(k))/h(k); % Boundary condition if KC <= 1 %1st derivatives specified A(1,1:2) = [2*h(1) h(1)]; b(1) = 3*(dy(1) - dy0); %Eq.(3.5.5a) A(N + 1,N:N + 1) = [h(N) 2*h(N)]; b(N + 1) = 3*(dyN - dy(N));%Eq.(3.5.5b) elseif KC == 2 %2nd derivatives specified A(1,1) = 2; b(1) = dy0; A(N + 1,N+1) = 2; b(N + 1) = dyN; %Eq.(3.5.6) else %2nd derivatives extrapolated A(1,1:3) = [h(2) - h(1) - h(2) h(1)]; %Eq.(3.5.7) A(N + 1,N-1:N + 1) = [h(N) - h(N)-h(N - 1) h(N - 1)]; end for m = 2:N %Eq.(3.5.8) A(m,m - 1:m + 1) = [h(m - 1) 2*(h(m - 1) + h(m)) h(m)]; b(m) = 3*(dy(m) - dy(m - 1)); end S(:,3) = A\b; % Cubic spline coefficients for m = 1: N S(m,4) = (S(m+1,3)-S(m,3))/3/h(m); %Eq.(3.5.9) S(m,2) = dy(m) -h(m)/3*(S(m + 1,3)+2*S(m,3)); S(m,1) = y(m); end S = S(1:N, 4:-1:1); %descending order pp = mkpp(x,S); %make piecewise polynomial yi = ppval(pp,xi); %values of piecewise polynomial ftn usando cspline: %do_csplines.m KC = 1; dy0 = 2; dyN = 2; % with specified 1st derivatives on boundary x = [0 1 2 3]; y = [0 1 4 5]; xi = x(1)+[0:200]*(x(end)-x(1))/200; %intermediate points [yi,S] = cspline(x,y,xi,KC,dy0,dyN); S %cubic spline interpolation clf, plot(x,y,'ko',xi,yi,'k:') yi = spline(x,[dy0 y dyN],xi); %for comparison with MATLAB built-in ftn hold on, pause, plot(x,y,'ro',xi,yi,'r:') yi = spline(x,y,xi); %for comparison with MATLAB built-in ftn pause, plot(x,y,'bo',xi,yi,'b') KC = 3; [yi,S] = cspline(x,y,xi,KC);%with the 2nd derivatives extrapolated pause, plot(x,y,'ko',xi,yi,'k')

3 de 3

Mario R. ROSENBERGER

Vous aimerez peut-être aussi