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.

x2t.m
function c = x2t(v,kind)
% v: physical representation f(x) of a function
% c: spectral representation, i.e.
% the Chebyshev polynomial coefficients such that
% f(x) = c(n) T_n-1(x) + ... + c(2) T_1(x) + c(1)/2 T_0(x) 
% where T_n(x) denotes the n-th Chebyshev polynomial.
% v is a matrix, each column contains the values of some other function
%
%   Inspired by chebpoly.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,m]=size(v);if n==1, c = 2*v; return; end
if kind==2 % For values from Chebyshev points of the 2nd kind
    gvals=v; c = [gvals(end:-1:2,:) ; gvals(1:end-1,:)];
    if isreal(gvals)
        c = fft(c)/(2*n-2);c = real(c);
    elseif isreal(1i*gvals)
        c = fft(imag(c))/(2*n-2);c = 1i*real(c);
    else
        c = fft(c)/(2*n-2);
    end
    c = c(n:-1:1,:);if (n > 2), c(2:end-1,:)=2*c(2:end-1,:); end
else % For values from Chebyshev points of the 1st kind
    gvals = v(end:-1:1,:);
    if isreal(gvals)
        c = realcoefs(gvals);
    elseif (isreal(1i*gvals))
        c = 1i*realcoefs(imag(gvals));
    else
        c = realcoefs(real(gvals))+1i*realcoefs(imag(gvals));
    end
end
c=flipud(c);c(1,:)=2*c(1,:);
function cc = realcoefs(v)
% Real case - Chebyshev points of the 1st kind
w = (2/n)*exp(-1i*(0:n-1)*pi/(2*n)).';
if rem(n,2) == 0 % Even case
    vv = [v(1:2:n-1,:); v(n:-2:2,:)];
else % odd case
    vv = [v(1:2:n,:); v(n-1:-2:2,:)];
end
cc = flipud(real(repmat(w,1,m).*fft(vv)));cc(end,:) = cc(end,:)/2;
end
end

Contact us