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 Center および File ExchangeLogical についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by