% cheb_interp.m % A near-minimax approximation polynomial interpolation of f(x) % Using Chebyshev-Gauss points as nodes function cheb_interp(n,a,b) f = @(x) cos(x); % Function to be interpolated xjc = cos(pi*(2*[0:n]+1)./(2*n+2)); % Chebyshev points [-1,1] xje = linspace(-1,1,n+1); % Equispaced points xjc = (xjc+1)/2*(b-a)+a; xje = (xje+1)/2*(b-a)+a; % transform to [a,b] yjc = f(xjc); yje = f(xje); % y-data xv = linspace(a,b,201); % x-values for figure fv = f(xv); % y-values of function f(x) yvc = interp_dd(xjc,yjc,xv); % y-values of interpolation with Cheby yve = interp_dd(xje,yje,xv); % y-values of interpolation with Equispace pts % plot plot(xv,fv-yvc,'r',xv,fv-yve,'b'); grid title('Error plots of interpolation using Chebyshev(Red), Equispace(Blue)') xlabel X ; ylabel Y
%%%% Subroutine %%% % Find interpolation using divide difference method function yvalues = interp_dd(nodes,fnodes,xvalues); m = length(nodes); dd = divdif(nodes,fnodes); yvalues = dd(m)*ones(size(xvalues)); for i=m-1:-1:1 yvalues = dd(i) + ( xvalues-nodes(i) ).*yvalues; end % Find divide differences function dd = divdif(x,f) dd = f; m = length(x); for i=2:m for j=m:-1:i dd(j) = ( dd(j)-dd(j-1) )/( x(j)-x(j-i+1) ); end end
|
|
|