Why does the value of tolerance stop at n=2 (third value of the iteration) within the while loop?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Asher
el 18 de Sept. de 2024
Comentada: Star Strider
el 18 de Sept. de 2024
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 comentario
Aquatris
el 18 de Sept. de 2024
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
Respuesta aceptada
Star Strider
el 18 de Sept. de 2024
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.
.
2 comentarios
Más respuestas (1)
Torsten
el 18 de Sept. de 2024
Movida: Torsten
el 18 de Sept. de 2024
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)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!