Code covered by the BSD License  

Highlights from
Chebpack

image thumbnail

Chebpack

by

 

15 Jul 2011 (Updated )

The MATLAB package Chebpack solves specific problems for differential or integral equations.

barycheb.m
function fx = barycheb(x,fk,xk,kind)  
%  Barycentric interpolation through Chebyshev points
%  fx = barycheb(x,fk,xk,kind) interpolates the values fk at nodes 
%  xk in the points x. The barycentric weights are calculated
%  depending on kind. All inputs should be column vectors.
%
%   Inspired by bary.m from chebfun package
%   Copyright  2011, 
%   The Chancellor, Masters and Scholars of the University of Oxford, 
%   and the Chebfun Developers. All rights reserved.
%   See http://www.maths.ox.ac.uk/chebfun/ for Chebfun information.
%
n = length(fk); 
if n == 1               % The function is a constant
    fx = fk*ones(size(x));return;
end
fx = zeros(size(x));% init return value
if kind == 2           % Default to Chebyshev weights
    ek = [.5 ; ones(n-1,1)]; ek(2:2:end) = -1;ek(end) = .5*ek(end);
else
    ek = sin((2*(0:n-1)+1)*pi/(2*n)).';ek(2:2:end) = - ek(2:2:end);
end
if length(x) < length(xk)
    for i = 1:numel(x)
        xx = ek./(x(i)-xk);fx(i) = (xx.'*fk)/sum(xx);
    end      
else
    num = zeros(size(x)); denom = num;
    for i = 1:numel(xk)
        y = ek(i)./(x-xk(i));num = num+(fk(i)*y);denom = denom+y;
    end
    fx = num./denom;
end
% clean-up nans
for i=find(isnan(fx(:)))'
    indx = find(x(i)==xk,1);fx(i) = fk(indx);
end
end

Contact us