Chebyshev Polynomial functions of the first kind

2 Ansichten (letzte 30 Tage)
Katerina Polykarpou
Katerina Polykarpou am 14 Feb. 2020
function y = myChebyshevPoly1(n,x)
%y = myChebyshevPoly1(n,x)
%y is the vector for the first n Chebyshev numbers
if n == 0
out = 1; %first base case
else if n == 1
out = x; %second base case
else
out = 2*x*myChebyshevPoly1((n-1),x)-myChebyshevPoly1((n-2),x);%recursive call
end
end % end myChebyshevPoly1
>>x = myvec/26;
>> n = 5;
>> y = myChebyshevPoly1(n,x)
when i run it and apply the following commands it will appear as error can someone explain to me what am i doing wrong?
also how can i ensure that expression above works for the case when x is a vector.

Antworten (1)

Patrick Gipper
Patrick Gipper am 14 Feb. 2020
You just need to replace "out" with "y". Maybe you already caught this?
function y = myChebyshevPoly1(n,x)
%y = myChebyshevPoly1(n,x)
%y is the vector for the first n Chebyshev numbers
if n == 0
y = 1; %first base case
else if n == 1
y = x; %second base case
else
y = 2*x*myChebyshevPoly1((n-1),x)-myChebyshevPoly1((n-2),x);%recursive call
end
end % end myChebyshevPoly1
  3 Kommentare
Patrick Gipper
Patrick Gipper am 14 Feb. 2020
Oh right, I just used a scalar test case. One change fixes that.
function y = myChebyshevPoly1(n,x)
%y = myChebyshevPoly1(n,x)
%y is the vector for the first n Chebyshev numbers
if n == 0
y = 1; %first base case
else if n == 1
y = x; %second base case
else
y = 2*x.*myChebyshevPoly1((n-1),x)-myChebyshevPoly1((n-2),x);%recursive call
end
end % end myChebyshevPoly1
Katerina Polykarpou
Katerina Polykarpou am 14 Feb. 2020
thank you so much

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by