Why does the value of tolerance stop at n=2 (third value of the iteration) within the while loop?

조회 수: 1 (최근 30일)
I am using a while loop to determine the taylor expansion of cos(x), I am trying to work out how many iterations (with the result of each iteration) it takes to reach the tolerance value (tol) for a given value of x (in this case sin(pi/5)) and tolerance of exp(-7).
When I run the for loop, I get 3 values output (n=0,1,2) but not a third value, as the answer for e suggests that I should get to n=3, yet my code seems to stop as soon as I reach n=2. However, the output value is still greater than the allowed tolerance, so I am unsure why the while loop does not complete another iteration (to be within the tolerance value).
clear;clc;
tol = exp(-7);
x = sin(pi/5);
target = cos(x);
counter = 1;
n(counter) = 0;
result(counter) = (((-1)^n(counter)) * (x^(2*n(counter)))/(factorial(2*n(counter))));
while abs(result(counter)-target) > tol
counter = counter + 1;
n(counter) = n(counter-1) + 1;
result(counter) = result(counter-1) + (((-1)^n(counter)) * (x^(2*n(counter)))/(factorial(2*n(counter))))
end
  댓글 수: 1
Aquatris
Aquatris 2024년 9월 18일
The output value is not greater than the allowed tolerance since the difference between result(counter) and target becomes less than the tol.
Can you explain what you mean by "as the answer for e suggests that I should get to n=3"?
clear;clc;
tol = exp(-7);
x = sin(pi/5);
target = cos(x);
counter = 1;
n(counter) = 0;
result(counter) = (((-1)^n(counter)) * (x^(2*n(counter)))/(factorial(2*n(counter))));
while abs(result(counter)-target) > tol
counter = counter + 1;
n(counter) = n(counter-1) + 1;
result(counter) = result(counter-1) + (((-1)^n(counter)) * (x^(2*n(counter)))/(factorial(2*n(counter))));
end
n
n = 1×3
0 1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[result(end) target]
ans = 1×2
0.8322 0.8322
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
abs(result(end)-target)<tol
ans = logical
1

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

채택된 답변

Star Strider
Star Strider 2024년 9월 18일
The loop appears to be working correctly.
Adding ‘Check_Convergence’ and examiining the results demonstrates this —
clear;clc;
tol = exp(-7)
tol = 9.1188e-04
x = sin(pi/5);
target = cos(x)
target = 0.8322
counter = 1;
n(counter) = 0;
result(counter) = (((-1)^n(counter)) * (x^(2*n(counter)))/(factorial(2*n(counter))));
while abs(result(counter)-target) > tol
counter = counter + 1;
n(counter) = n(counter-1) + 1
result(counter) = result(counter-1) + (((-1)^n(counter)) * (x^(2*n(counter)))/(factorial(2*n(counter))))
Check_Convergence = abs(result(counter)-target)
end
n = 1×2
0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
result = 1×2
1.0000 0.8273
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Check_Convergence = 0.0049
n = 1×3
0 1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
result = 1×3
1.0000 0.8273 0.8322
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Check_Convergence = 5.6925e-05
The calculation meets the criterion after the second iteration, and the loop then stops.
.

추가 답변 (1개)

Torsten
Torsten 2024년 9월 18일
이동: Torsten 2024년 9월 18일
Maybe you mean
tol = 1e-7
instead of
tol = exp(-7)
?
However: It's correct that MATLAB quits the while-loop after three values:
tol = exp(-7)
tol = 9.1188e-04
x = sin(pi/5);
target = cos(x);
counter = 1;
n(counter) = 0;
result(counter) = (((-1)^n(counter)) * (x^(2*n(counter)))/(factorial(2*n(counter))));
while abs(result(counter)-target) > tol
counter = counter + 1;
n(counter) = n(counter-1) + 1;
result(counter) = result(counter-1) + (((-1)^n(counter)) * (x^(2*n(counter)))/(factorial(2*n(counter))));
end
abs(result(counter)-target)
ans = 5.6925e-05

카테고리

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

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by