A problem with While command...
2 views (last 30 days)
Show older comments
Hi everyone,
I have a problem using While command so Matlab becomes busy.
This is a part of code (the variables are all defined):
a=zeros(length(r),1);
anew=1./(((4.*((sind(fi)).^2)./(sigma.*Cn)))+1);
err=(anew-a)./anew;
toll=1e-3;
while abs(err)>=toll a=anew; end
If I break the loop pressing Ctrl+C and I ask "a" value from command window it returns me the "anew" value calculated in first step of loop. If i calculate by myself the other values (second, third ecc. step)the condition of while loop is still true. There is a syntax error or is only the function that doesn't converge?
Thanks for any help and sorry for my english!
0 Comments
Answers (6)
Jan
on 17 Jan 2013
Edited: Jan
on 17 Jan 2013
But in the body of the loops the values of teh variables do not change at all. All that happens is, that the value of anew is assigned to a again and again. and anew is not calculated in the first step of the loop, but only once before the loop.
It seems like you have confused the variable anew with a function, and the same for err, which does not change its value inside the loop also.
0 Comments
Thorsten
on 17 Jan 2013
Based on Jan's comments you may want to change your code as sketched below:
a=zeros(length(r),1);
anew=1./(((4.*((sind(fi)).^2)./(sigma.*Cn)))+1);
err=(anew-a)./anew;
toll=1e-3;
while abs(err)>=toll
a=anew;
Change values in the loop such that anew gets a new value in every iteration
fi =
Cn =
anew=1./(((4.*((sind(fi)).^2)./(sigma.*Cn)))+1);
Compute new error WITHIN loop
err=(anew-a)./anew;
end
1 Comment
Jan
on 17 Jan 2013
In addition err is a vector. What should happen in while abs(err)>=toll exactly? If all elements >= 0, or any elements?
I assume, err should be a scalar.
Giacomo
on 17 Jan 2013
1 Comment
Jan
on 18 Jan 2013
Edited: Jan
on 18 Jan 2013
When you have just started to program Matlab, where does the "close all, clear all, clc" come form? This appears very frequently, but is inspite of this not useful. There is simply no reasons to close all figures and "clear all" removes beside other things all loaded functions from the memory also. While a pure "clear" would remove all variables only, which could have an advantage, "clear all" is useful only, if you have modified all M-files during the program runs - a really rare situation. Therefore I'm interested to know, who did suggest the brute clearing method.
Thorsten
on 18 Jan 2013
Exactly, you have to put all values that change in the body of the while loop. And all values that do not change can be moved outside the body of the while loop to save computation time and to make the code clearer. And you can go on if the minimum of all values in err is above your tolerance. Use if you are happy if either a or a1 is below toll; use && if both have to be below tolerance
while min(abs(errl)) >= toll && min(abs(err)) > = toll
And make sure that you do not confuse a and a1. For example, the 6. line from below should read (replace a with a1):
err1=(a1new-a1)./a1new;
0 Comments
Jan
on 18 Jan 2013
Matlab code looks much nicer, when you operate on vectors, matrices and arrays directly. Instead of:
alpha = zeros(length(r),1);
for i=1:length(r)
alpha(i)=fi(i)-teta(i);
end
write:
alpha = fi - teta;
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!