How to get this while cycle?

2 views (last 30 days)
Elia Paini
Elia Paini on 16 Aug 2021
Commented: Elia Paini on 16 Aug 2021
Hi, I'm trying to get a while cycle which checks convergence every iteration.
Within the cycle, I'm solving a system which returns each iteration 3 types of solutions: a (vector), B, C (matrices).
Subscript "0" is related to the (l-1) iteraton, the other refers to the (l) iteration.
I wrote this code:
while (abs(a_0-a) > 1e-6) & (abs(B_0-B) > 1e-6) & (abs(C_0-C) > 1e-6)
....
end
It starts, but it do only one iteration, I don't know why.
It should iterate until ALL residues are lower than 1e-6.
Probably it's wrong, maybe due to parentheses or logical operators. I've already tried to change them, but in the other cases it doesn't work.
What should I do?
Thank you for your help!!

Accepted Answer

Fabio Freschi
Fabio Freschi on 16 Aug 2021
if B and C are matrices, make them vectors using :
while any(abs(a_0-a) > 1e-6) || any(abs(B_0(:)-B(:)) > 1e-6) || any(abs(C_0(:)-C(:)) > 1e-6)
  1 Comment
Elia Paini
Elia Paini on 16 Aug 2021
Great, it works! Thank you!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 16 Aug 2021
while (abs(a_0-a) > 1e-6) || (abs(B_0-B) > 1e-6) || (abs(C_0-C) > 1e-6)
  5 Comments
Elia Paini
Elia Paini on 16 Aug 2021
I checked, the couples have the same dimension and orientation.
a_0 and a are (4x1), B_0 and B are (4x2), C_0 and C are (4x3)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!