While loop for a stopping criterion and check if it positive

1 回表示 (過去 30 日間)
Rémi BERNARD
Rémi BERNARD 2019 年 9 月 2 日
コメント済み: Rémi BERNARD 2019 年 9 月 2 日
Hello everyone,
Sorry if the title is not clear enough, it is quite hard to put in to simple word. In the current Matlab code I am working on, I have a while loop of the type:
while and(X > eps1, Y > eps2) && count < it_max
%calculations
end
with X, Y the variables of interest; eps1, eps2 the stopping criterias. My problem is that eps1 and eps2 are positive, and X and Y can take negative values. However these must be positive and I would like to add a condition to make the while loop continue when X and Y are negative (without any absolute value function).
Thank you.

回答 (1 件)

Image Analyst
Image Analyst 2019 年 9 月 2 日
I don't know why you don't want to use the abs() function but you can do it without it like this:
while ((X > eps1 && Y > eps2) || (X < eps1 && Y < eps2)) && count < it_max
%calculations
count = count + 1;
end
  1 件のコメント
Rémi BERNARD
Rémi BERNARD 2019 年 9 月 2 日
I give an example which is not my case but I think can illustrate it well. Let's say in this loop I can have viscosity, which cannot be negative but for computation purpose I say it can be.
So take eps = 0.1:
- if for a certain iteration, for example 5, I have my viscosity which is -0.06; with abs() my criteria is respected but the result is not physically acceptable. (ie Matlab accepts it but I don't)
- for iteration +1 I have viscosity = 0.05; so I would like to accept that result instead of the previous one.
Your code does more or less the same thing no?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2014b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by