Test the Chebyshev Function by plotting the 13th order Chebyshev polynomial but return with wrong graph

% Test the Chebyshev Function by plotting the 13th order Chebyshev polynomial
% Figure should match image given in question.
% Define x values as 2000 points between -1.2 and 1.2
x = linspace(-1.2,1.2,2000); % <---- Add code here
% Use the function to find the Chebyshev 13th order polynomial
n = 13
y = Chebyshev(n,x) % <---- Add code here
% Plot the 13th order Chebyshev polynomial
plot(x,y,'r','LineWidth',2)
% Adjust the axis and add a grid
axis([-1.2 1.2 -1.5 1.5]);
grid on
% Add a title
titlestr = ['Chebyshev Polynomial of Order ' num2str(n,'%d')];
title(titlestr,'FontSize',20)
% Label the axis (setting a custom font size)
xlabel('x','FontSize',16)
ylabel('T_n','FontSize',16)
function y = Chebyshev(n,x)
% Calculate the nth order Chebyshev polynomial
% INPUTS:
% n is the order of the Chebyshev polynomial (a constant)
% x is the values where the function is to be evaluated (a vector).
% The number of rows of the polynomial matrix T
nrows = n+1;
% the nunber of elements in x
nx = length(x);
% Create a blank matrix for the values of the polynomials
% The 1st row will be the values of T0
% The 2nd row will be the values of T1 etc
T = zeros(nrows,nx);
% The 1st row is T0 = 1
T(1,:) = ones(1,nx);
% The 2nd row is T1 = x;
T(2,:) = x;
% Use a FOR loop to set the other rows of T
for k = 3 : nrows % <- insert your code here
T(k,:) = 2 * k .* T(k-1,:) - T(k-2,:);
% Use the previous 2 rows of T to set the value of the current row
% using the recurrence formula
% <- insert your code here
end % <- insert your code here to stop repeating
% Access the last row of the matrix to get the values of the nth order Chebyshev polynomial
y = T(nrows,:);
end

Réponses (1)

Given the recurrence definition used, you are trying to plot the 13th order Chebyshev polynomial of the 1st kind.
In that particular definition, polynomials are multiplied, whereas the implementation you have done does not account for that. You have directly multipiled the values where the function/polynomial is supposed to be evaluated at. The polynomial needs to be calculated first, then it can be evaluated.
In numerical methods of MATLAB, polynomials can be represented as row vectors, with numbers denoting coefficients and various operations can be performed accordingly. See here for reference - Create and Evaluate Polynomials
To implement the recurrence definition, we will utilise conv to perform polynomial multiplication and obtain the polynomial. After that polyval will be used to obtained the value.
I have modified the function to provide the nth order polynomial as an output. Thus the value will have to be calculated in the script portion of the code. You can modify the code as per requirement/liking.
% Test the Chebyshev Function by plotting the 13th order Chebyshev polynomial
% Figure should match image given in question.
% Define x values as 2000 points between -1.2 and 1.2
x = linspace(-1.2,1.2,2000); % <---- Add code here
% Use the function to find the Chebyshev 13th order polynomial
n = 13;
%The nth order Chebyshev polynomial of 1st kind
Y = Chebyshev(n,x); % <---- Add code here
%% Displaying the equation
disp(Y)
4096 0 -13312 0 16640 0 -9984 0 2912 0 -364 0 13 0
%corresponding values
y = polyval(Y, x);
% Plot the 13th order Chebyshev polynomial
plot(x,y,'r','LineWidth',2)
% Adjust the axis and add a grid
axis([-1.2 1.2 -1.5 1.5]);
grid on
% Add a title
titlestr = ['Chebyshev Polynomial of Order ' num2str(n,'%d')];
title(titlestr,'FontSize',20)
% Label the axis (setting a custom font size)
xlabel('x','FontSize',16)
ylabel('T_n','FontSize',16)
%% Function definition
function Y = Chebyshev(n,x)
% Calculate the nth order Chebyshev polynomial
% INPUTS:
% n is the order of the Chebyshev polynomial (a constant)
% x is the values where the function is to be evaluated (a vector).
% The number of rows of the polynomial matrix T
nrows = n+1;
% the nunber of elements in x
%nx = length(x);
% Create a blank matrix for the values of the polynomials
% The 1st row will be the values of T0
% The 2nd row will be the values of T1 etc
T = zeros(nrows);
% The 1st row is T0 = 1
T(1,end) = 1;
% The 2nd row is T1 = x;
T(2,end-1) = 1;
for k = 3 : nrows % <- insert your code here
T(k, :) = 2*conv(T(k-1,:), [1 0], 'same') - T(k-2, :);
%T = temp - [zeros(1, numel(temp) - numel(T)) T]
% using the recurrence formula
% <- insert your code here
end % <- insert your code here to stop repeating
% Access the last row of the matrix to get the values of the nth order Chebyshev polynomial
Y = T(end, :);
end

8 commentaires

For comparison, let's check the output from the symbolic function chebyshevT
syms x
n=13;
Y = chebyshevT(n, x)
Y = 
You can see the polynomial here and compare the coefficients with the output obtained above.
You can plot the same as well.
x0 = linspace(-1.2,1.2,2000);
y0 = double(subs(Y, x, x0));
y0 = 1×2000
1.0e+03 * -1.6320 -1.5940 -1.5567 -1.5202 -1.4845 -1.4495 -1.4151 -1.3815 -1.3486 -1.3163 -1.2847 -1.2538 -1.2235 -1.1938 -1.1648 -1.1363 -1.1085 -1.0812 -1.0545 -1.0284 -1.0028 -0.9778 -0.9533 -0.9294 -0.9059 -0.8830 -0.8605 -0.8386 -0.8171 -0.7961
plot(x0,y0,'r','LineWidth',2)
% Adjust the axis and add a grid
axis([-1.2 1.2 -1.5 1.5]);
grid on
% Add a title
titlestr = ['Chebyshev Polynomial of Order ' num2str(n,'%d')];
title(titlestr,'FontSize',20)
And Voila!
Is there any easy way to correct my mistake?
what do you mean by polynomials are multiplied, whereas the implementation you have done does not account for that. I am not quite clear what my mistake is
The recurrence definition, according to in Wikipedia - https://en.wikipedia.org/wiki/Chebyshev_polynomials#Recurrence_definition, is
with,
Here, the multiplication of x and is polynomial muliplication, as both of them are polynomials.
"I am not quite clear what my mistake is"
The objective here is to find the values corresponding to the nth Chebyshev polynomial (of first kind). Now, In order to do that, you need to find the nth order polynomial first. Then put the values of x to get y.
However, you have directly used the x values in the definition above. Instead of using the previous polynomials (as the definition requires). That's the mistake.
I hope it is clear now.
You're welcome!
If my answer solved your problem, please consider accepting it!
There is a point to be made in this.
Look VERY carefully at the final plot of the polynomial. Do you see at the end, it gives values that are greater than 1, and less than -1? As large as 1.5 in magnitude?
That polynomial is only meaningfully employed on the interval [-1,1], NOT on the interval [-1.2,1.2]. And while you can technically evaluate it at other points, it lacks any useful properties on the wider domain.

Connectez-vous pour commenter.

Question posée :

le 6 Jan 2024

Commenté :

le 6 Jan 2024

Community Treasure Hunt

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

Start Hunting!

Translated by