필터 지우기
필터 지우기

atan Taylor Polynomial using polyval function.

조회 수: 4 (최근 30일)
Jim Oste
Jim Oste 2015년 2월 17일
답변: Evelia Coss 2021년 2월 18일
I need to write a function that computes the Taylor polynomial P_n(x) for a general odd number n>=1 using the built-in Matlab function polyval. I have the following function so far for the Taylor polynomial:
[ y ] = ost_arctanTaylor(n, x)
%Computes the Taylor polynomial for f(x) = atan(x)
for i = 1:2:n
y = ((-1).^(i)).*((x.^(2.*i-1))./(2.*i-1));
end
end
I am confused on how to integrate the polyval function into this code. Any help?

채택된 답변

John D'Errico
John D'Errico 2015년 2월 17일
You are trying to evaluate the polynomial itself in a loop, although the loop as you wrote it will not do what you think.
The requirement was for polyval to evaluate it, NOT you. So you need to build the coefficients of the polynomial, and pass them into polyval. Let it do the work.
  댓글 수: 2
Jim Oste
Jim Oste 2015년 2월 18일
Do you know how I would go about coding the coefficients? I know that polyval evaluates the function given the coefficients of descending powers but with the Taylor series for arctan(x) I only need odd powers. How would the polyval function know not to evaluated anything for even powers?
John D'Errico
John D'Errico 2015년 2월 18일
Polyval does not care if you give it zero coefficients. Make the even term coefficients zero. It WILL use those zero coefficients, effectively multiplying by zero, but who cares? You will just create a vector of coefficients, with the HIGHEST order coefficient first. So, for example, the atan series looks like
x - x^3/3 + x^5/5 ...
then the polynomial as polyval would care about it would be:
P = [1/5 0 -1./3 0 1 0];
Don't forget that zero'th order (constant) term at the end. It is important.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Evelia Coss
Evelia Coss 2021년 2월 18일
x = input('Number that you want to analyze');
i = input('Number of iterations:');
% Create an cumulative variable
y = 0;
for n = 0:i
arctang = ((-1).^n).*(x.^(2.*n+1)./(2.*n+1));
y = y + arctang;
end
disp('Arctangent value of x is:');
disp(y);

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by