Recursion function-Chebyshev polynomials

9 ビュー (過去 30 日間)
Tho Gonzalez
Tho Gonzalez 2016 年 9 月 4 日
編集済み: Walter Roberson 2016 年 9 月 5 日
Question:
Chebyshev polynomials are defined recursively. Chebyshev polynomials are separated into two kinds: first and second. Chebyshev polynomials of the first kind, Tn(x), and of the second kind, Un(x), are defined by the following recurrence relations:
Tn(x) = 1 if n = 0;
= x if n = 1;
= 2xTn1(x) Tn2(x) otherwise;
Write a function with header [y] = myChebyshevPoly1(n,x), where y is the n-th Chebyshev polynomial of the first kind evaluated at x.Be sure your function can take array inputs for x. You may assume that x is a row vector. The output variable, y, must be a row vector also.
function [y] = myChebyshevPoly1(n,x)
% y = chebyshev polynimial
%Tn(x) = 1 if n=0
%Tn(x) =x if n=1
%Tn(x) = 2xTn-1(x) - Tn-2(x)
%function can take array
% get array x
xleng=length(x);
if n ==0
y=1;
elseif n==1
y = x;
else
for i= 1: xleng
y= 2*(x)*myChebyshevPoly1(n-1,x) - myChebyshevPoly1(n-2,x);
end
end
end
end
Comment: the code doesnt run because the array can't multiply together, how can I fix this?

採用された回答

Walter Roberson
Walter Roberson 2016 年 9 月 4 日
Remember .* instead of *
But notice that in every iteration of your loop, you are overwriting all of y based on all of x. You need to rethink that loop.
  2 件のコメント
Tho Gonzalez
Tho Gonzalez 2016 年 9 月 4 日
can I use for in else?
Tho Gonzalez
Tho Gonzalez 2016 年 9 月 5 日
編集済み: Walter Roberson 2016 年 9 月 5 日
function y = myChebyshevPoly2(n,x)
%--------------------------------------------------------------------------
% y = myChebyshevPoly2(n,x)
% Chebyshev polynomials of the ?rst kind, Tn(x)
% Author: Hien NGUYEN
% Date: 05 Sept 2016
%--------------------------------------------------------------------------
m = length(x);
if n == 0
y = ones(1,m);
else
if n == 1
y = x;
else
y = 2 * x .* myChebyshevPoly2(n-1,x)-myChebyshevPoly2(n-2,x);
end
end
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePolynomials についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by