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
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
[result(end) target]
abs(result(end)-target)<tol
採用された回答
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)
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))))
Check_Convergence = abs(result(counter)-target)
end
The calculation meets the criterion after the second iteration, and the loop then stops.
.
その他の回答 (1 件)
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)
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)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!