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!!

 채택된 답변

Fabio Freschi
Fabio Freschi 2021년 8월 16일

1 개 추천

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개)

Walter Roberson
Walter Roberson 2021년 8월 16일

1 개 추천

while (abs(a_0-a) > 1e-6) || (abs(B_0-B) > 1e-6) || (abs(C_0-C) > 1e-6)

댓글 수: 5

Elia Paini
Elia Paini 2021년 8월 16일
편집: Elia Paini 2021년 8월 16일
I got this error:
Operands to the logical and (&&) and or (||) operators must be convertible to logical scalar values.
while any(abs(a_0-a) > 1e-6) || any(abs(B_0-B) > 1e-6) || any(abs(C_0-C) > 1e-6)
Elia Paini
Elia Paini 2021년 8월 16일
Same error
while any(abs(a_0-a) > 1e-6, 'all') || any(abs(B_0-B) > 1e-6, 'all') || any(abs(C_0-C) > 1e-6, 'all')
The implication is that at least one of your subtractions is giving back a matrix that is 2 or more dimensions.
Please check that a_0 and a, and B_0 and B, and C_0 and C, have the same orientation if they are vectors. For example,
(1:3).' - (1:3)
is not the vector [0 0 0]
Elia Paini
Elia Paini 2021년 8월 16일
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)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2021년 8월 16일

댓글:

2021년 8월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by