Clear Filters
Clear Filters

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

5 views (last 30 days)
% 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

Answers (1)

Dyuman Joshi
Dyuman Joshi on 6 Jan 2024
Edited: Dyuman Joshi on 6 Jan 2024
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 Comments
John D'Errico
John D'Errico on 6 Jan 2024
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!