How to correct Error 'Undefined variable x' in generating normalized value of chebyshev polynomial. Need to generate normalized chebyshev polynomial

This code generates coefficients and then roots of chebyshev polynomial:
function T = ChebT(n,x)
% Coefficients T of the nth Chebyshev polynomial of the first kind.
% They are stored in the descending order of powers.
t0 = 1;
t1 = [1 0];
if n == 0
T = t0;
elseif n == 1;
T = t1;
else
for k=2:n
T = [2*t1 0] - [0 0 t0];
t0 = t1;
t1 = T;
end
end
>> coeff6= ChebT(5);
>> roots(coeff6)
>> polyval(coeff6,3)/Sqrt[1 - x^2], {x, -1, 1}

Answers (1)

You defined
function ChebT(n,x)
so ChebT expects up to two parameters to be passed to it. However, your code never uses the second parameter.
You call
coeff6= ChebT(5)
which works because the code ChebT which permits up to two parameters does not use the second parameter.
You invoke
polyval(coeff6,3)/Sqrt[1 - x^2], {x, -1, 1}
However, you have not defined x in your code.
MATLAB only uses [] for list and array building: int MATLAB it is never valid to follow a variable name or function name by [] without an operator in-between, such as
polyval(coeff6,3)/Sqrt*[1 - x^2], {x, -1, 1}
If that was your meaning, then note that you have not defined any variable or function named Sqrt . Sqrt will not match the MATLAB sqrt function, because MATLAB is case-sensitive.
polyval(coeff6,3) is valid MATLAB. coeff6 at that point would be [16 0 -20 0 5 0] so polyval(coeff6,3) would be 16*3^5 + 0*3^4 -20 * 3^3 + 0*3^2 + 5*3^1 + 0*3^0 = 120126 . Notice that this does not involve any variable 'x'
... What it looks like to me is that you attempted to use Mathematica syntax in MATLAB.
My guess at what you want:
syms x
f = poly2sym(coeff6,x) / sqrt(1-x^2)
fplot(f, [-1 1])

2 Comments

Thanks! But how to get the normalized values of Chebyshev from roots or coefficients generated ?
I am not sure what normalized means in this context. I find one definition that divides Tn by 2^n; if so then
normChebT = @(N) ChebT(N)./2^N;
coeff6 = normChebT(5);
syms x
f = poly2sym(coeff6,x) / sqrt(1-x^2)
fplot(f, [-1 1])

Sign in to comment.

Asked:

AD
on 19 Nov 2017

Commented:

on 20 Nov 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!