필터 지우기
필터 지우기

Calculate taylor series using while loop

조회 수: 26 (최근 30일)
Fabian Schön
Fabian Schön 2021년 11월 23일
댓글: Fabian Schön 2021년 11월 25일
I want to calculate the taylor series of ln(5) using a while loop.
x = 1/5;
tol = 1e^-9;
s = 0;
i = 0;
term = (x-1)
while abs(term) > tol
s = s+term;
i = i+1;
term = (-1.^i)*(((x-1).^i)./1);
end
disp(s)
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2021년 11월 24일
편집: Dyuman Joshi 2021년 11월 25일
Why are you defining x = 1/5 when you are calculating expansion for the value 5?
Also, the formula for term in the loop is incorrect.

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

답변 (1개)

James Tursa
James Tursa 2021년 11월 25일
편집: James Tursa 2021년 11월 25일
Looks like you are planning to use the identity ln(5) = ln((1/5)^-1) = -ln(1/5) and then use 1/5 as the x in the Taylor Series expansion that is good for 0 < x <= 2. However, the problems are:
  • The divisor in your term needs to be i, not 1
  • The initial value of i should be 1, not 0
  • The -1 factor needs to be wrapped in parentheses and have a different exponent, i.e. (-1)^(i-1)
  • Per above, you need to display -s, not s

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by