Chebyshev Polynomial functions of the first kind

1 回表示 (過去 30 日間)
Katerina Polykarpou
Katerina Polykarpou 2020 年 2 月 14 日
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.

回答 (1 件)

Patrick Gipper
Patrick Gipper 2020 年 2 月 14 日
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 件のコメント
Patrick Gipper
Patrick Gipper 2020 年 2 月 14 日
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 2020 年 2 月 14 日
thank you so much

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by