Logical statement applicable to entire matrix

Hi all,
I'm running an iterative code which solves for a value Cd from an initial guess, Cd_org. Currently, I have the code running, but it stops after one iteration because a few of my 27832 data points are within the required tolerance. Is there some way to ensure that every value of my delta_Cd matrix falls within the tolerance?
Cd_org = ones(length(t),1);
tolerance = 0.00001;
delta_Cd = 0;
while delta_Cd < tolerance
for n = 1:length(t)
.
.
.
% Establishes variable Cd, calculates new Cd using Cd_org.
.
.
.
end
delta_Cd = Cd - Cd_org;
Cd_org = Cd;
end
Thanks, Aisha

Respuestas (2)

Honglei Chen
Honglei Chen el 29 de Jun. de 2017
You can try
while all(delta_Cd(:) < tolerance)
HTH

2 comentarios

Aisha McKee
Aisha McKee el 29 de Jun. de 2017
No luck. Still only runs the one time....
Honglei Chen
Honglei Chen el 29 de Jun. de 2017
Editada: Honglei Chen el 29 de Jun. de 2017
Looks like the original condition is problematic, as it says do it while the error is within the tolerance. Probably should be
while any(delta_Cd(:) > tolerance)
as Walter mentioned below
HTH

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 29 de Jun. de 2017
Cd_org = ones(length(t),1);
tolerance = 0.00001;
delta_Cd = inf;
while any(delta_Cd > tolerance)
for n = 1:length(t)
.
.
.
% Establishes variable Cd, calculates new Cd using Cd_org.
.
.
.
end
delta_Cd = Cd - Cd_org;
Cd_org = Cd;
end

2 comentarios

Aisha McKee
Aisha McKee el 29 de Jun. de 2017
This ran for infinity iterations and eventually all my points went to infinity with this error message.
>>Warning: Rank deficient, rank = 0, tol = 1.#INF00e+00.
Thoughts?
Do you use delta_Cd in your calculations? If so then initialize it to something like tolerance*2 instead of inf.
If you do not... remember that there are calculations where it is not possible to have all the outputs simultaneously less than the tolerance.
Also I would recommend modifying to
while any(abs(delta_Cd) > tolerance)

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 29 de Jun. de 2017

Comentada:

el 29 de Jun. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by