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!