What does X(counter) do in this piece of code?
Afficher commentaires plus anciens
This code uses a while loop to determine which value of x can be taken to say that the value of e^-x is approximately equal to zero. In order to do this, a tolerance value has been added (1e-3) so that if the output exceeds the tolerance, the for loop terminates.
For context, this code was given to us to demonstrate how to use a "counter" within a while loop.
I am struggling to understand how the "counter" value works within this code.
1) There seems to be x(counter), yet we have not yet determined what the value of x is equal to. I understand that it is effectively saying x(1) = 0 but surely we need to assign a value to x?
2) Why do we need to rewrite result(counter)= e^-x(counter) inside the while loop, why will the code not work if we simply put result(counter)?
clear; clc;
tol = 1E-3;
counter = 1;
x(counter) = 0; %we haven't yet determined the value of x, so why can we have a value for x(counter)?
result(counter) = exp(-x(counter))
while result(counter)>tol;
counter = counter + 1;
x(counter) = x(counter-1) + 1;
result(counter) = exp(-x(counter)) %why do we need to rewrite the full equation?
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!