HELP! Checking if variable increases after each iteration is completed
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hawki005
el 9 de Mzo. de 2019
Comentada: Hawki005
el 11 de Mzo. de 2019
I have a piece of code that uses an assumed value of x = 1 for the first iteration. The code then calculates a value of (y). For the best accuracy of (y), succesive iterations continue until (x) is less than 0.00001 for example.
This issue im having is that depending on other conditions, (y) can be positive or negative. To converge to zero, on each iteration, if (y) is positive then 0.001 must be subtracted. If (y) is calculated to be negative, 0.001 must be added to converge to zero.
I need some way of itentifying if (y) is postive or negative on each iteration, and then add or subract a small value to (y) before the next iteration starts depending if (y) is positive or negative.
Any help would be greatly appreciated! Thanks in advance.
0 comentarios
Respuesta aceptada
Más respuestas (1)
John D'Errico
el 9 de Mzo. de 2019
Editada: John D'Errico
el 9 de Mzo. de 2019
Surely there are many things you can do, and your question is very confusing. Perhaps this?
if abs(y) < 0.001
stuff
end
It is not clear what you are doing, but why would this not work?
if y < 0
% stuff for y < 0
elseif y > 0
% stuff for y > 0
else
disp('Y is zero!!!!!!!')
load handel
sound(y,Fs)
end
Solving a problem by perturbing a variable in steps of 0.001 seems like a poor programming style, but if it floats your boat, then break out the paddles and be happy. I'd instead suggest you might consider learnign about tools like fzero as a good way to improve your code in the future.
1 comentario
Ver también
Categorías
Más información sobre Loops and Conditional Statements 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!