Need to fit polynomial using chebyshev polynomial basis

14,725

Solution 1

I think you are looking for the Chebfun toolbox. It overloads, amongst others, the polyfit function using Chebychev points and Chebychev interpolants.

Next to the above, you can always code this yourself. It is not that difficult. EDIT: See the post of mathematician1975 :). EDIT2: updated chebfun website.

Solution 2

I will assume here that you want Chebyshev polynomials of the first kind. As far as I know, Matlab does not have this inbuilt. It is easy to code yourself though. Chebyshev polynomials are only defined on [-1,1] so first you must map your x data to this range. Then use the recurrence relation for generating Chebyshev polynomials http://en.wikipedia.org/wiki/Chebyshev_polynomials#Definition

T_(n+1)(x) = 2xT_(n)x - T_(n-1)(x)

If x are your abscissae and y your data points generate your observation matrix A (this is the equivalent of the Vandermonde matrix for monomial basis) for a degree n polynomial fit using:

n = degree;
m = length(x);
%% Generate the z variable as a mapping of your x data range into the 
%% interval [-1,1]

z = ((x-min(x))-(max(x)-x))/(max(x)-min(x));

A(:,1) = ones(m,1);
if n > 1
   A(:,2) = z;
end
if n > 2
  for k = 3:n+1
     A(:,k) = 2*z.*A(:,k-1) - A(:,k-2);  %% recurrence relation
  end
end

then you can just solve the linear system for your solution parameters (approximation coefficients) b using matrix divide

b = A \ y

What you have to remember here is that when you evaluate your approximation you must map the value to the interval [-1,1] before evaluating it. The coefficients will only be valid on the initial range of x you provide for the approximation. You will not be able to evaulate the approximation (in a valid sense) outside of your initial x range. If you want to do that you should use a wider interval than your data has, that way when you map points interior to that using the transform, your points will always lie in [-1,1] and therefore evaluation of your approximation is valid.

I have not used matlab for a while so new versions may actually have an inbuilt function that will do all of this for you. It was not the case when I last used it and if all else fails the above will allow you to generate least-squares polynomial approximations using chebyshev basis (first kind)

Share:
14,725
user1593853
Author by

user1593853

Currently a maths undergraduate. I code with Matlab and am learning c++

Updated on June 21, 2022

Comments

  • user1593853
    user1593853 almost 2 years

    I have been fitting linear least-squares polynomials to data using the polyfit function in matlab. From what I read, this uses standard polynomial basis (monomial basis). I have read that using Chebyshev polynomial basis to fit leads to greater numerical stability so I would like to do this. Does matlab have this option?

  • user1593853
    user1593853 almost 12 years
    Thanks for your answer. This toolbox does seem to be very useful and have some other useful functions. I have not actually tried yet but the documentation suggests it could be really useful
  • Erik
    Erik about 11 years
    Note that chebfun does not use least square polynomials but it uses interpolation through all points.