HELP! Checking if variable increases after each iteration is completed
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
更多回答(1 个)
John D'Errico
2019-3-9
编辑:John D'Errico
2019-3-9
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!