Solving questions related to taylor series expansion

조회 수: 3 (최근 30일)
Max Altshuler
Max Altshuler 2019년 4월 11일
편집: James Tursa 2019년 4월 11일
x = pi/4;
i = 2;
term(1) = -0.080746;
while term(i-1) >= 0.0001
term(i) = ((-1)^i/factorial(2*i+1))*x^(2*i+1)
i = i+1
end
term1(1) = -0.080746;
a = 2;
while term1(a-1) >= 1*10^-10
term1(a) = ((-1)^a/factorial(2*a+1))*x^(2*a+1)
a = a+1
end
fprintf('It takes %i terms',i)
fprintf('It takes %i terms',a)
My code is above and what follows is the question. I do not understand why this is not working. The first while loop works correctly but the second does not. Also, would a correct answer for the last part be "You can make it so that if 'i' reaches a certain number, break the loop."
Thanks for any help...

채택된 답변

James Tursa
James Tursa 2019년 4월 11일
편집: James Tursa 2019년 4월 11일
You need to compare the absolute value of the term to your tolerance. Remember, some of the terms are negative.
while abs(term(i-1)) >= 0.0001
etc.
And yes, you would break if your counter (i or a) reached a predetermined limit.
You will also need to double check and fix up the indexing you are using for your calculations ...

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by