function coefs = chebpolycoefs(n); if n == 0, coefs = [1]; elseif n == 1 coefs = [1 0]; else coefs_n_minus_1 = [chebpolycoefs(n-1), 0]; coefs_n_minus_2 = [0, 0, chebpolycoefs(n-2)]; coefs = 2*coefs_n_minus_1 - coefs_n_minus_2; % For some reason, MATLAB wasn't happy when I did it like this: % coefs = 2*[chebpolycoefs(n-1), 0] - [0, 0, chebpolycoefs(n-2)]; end