Académique Documents
Professionnel Documents
Culture Documents
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
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
3 de 3
Mario R. ROSENBERGER