loop until condition met
古いコメントを表示
im trying to create a loop until a certain condition exists by using the results at the end of each loop to calculate the next iteration. I've shown part of the code below. Iteration to terminate when Va = Vo = Vn.
Vo = [30 15 13 10];% initial V old to calc Re and f.
Va=zeros(size(Vo));
Vn=zeros(size(Vo));
%
while ~all([Va==Vo,Vo==Vn])
%
Re1 = (D(1).*Vo(1)) / nu;
%
Re2 = (D(2).*Vo(2)) / nu;
%
Re3 = (D(3).*Vo(3)) / nu;
%
Re4 = (D(4).*Vo(4)) / nu;
%
A = [Matrix];
b = [Matrix];
Vn = A\b;% solves four unknowns at A\b
%
for i = 1:4
Va(i) = (alpha * Vn(i)) + ((1-alpha) * Vo(i));
Vo(i) = Va(i); %Used as Vo for each in next iteration.
end
end
採用された回答
その他の回答 (1 件)
kjetil87
2013 年 9 月 1 日
A while loop is what you are looking for it seems.
while ~all([Va==Vo,Vo==Vn])
%your code
end
5 件のコメント
kjetil87
2013 年 9 月 1 日
or maybe its more efficient with
while (Va~=Vo) && (Vo~=Vn)
end
harley
2013 年 9 月 1 日
harley
2013 年 9 月 1 日
harley
2013 年 9 月 1 日
kjetil87
2013 年 9 月 1 日
The error is because Va and Vn is not defined yet. Either pre define or use the answer below. Also note the different compare method used, because i doubt the 3 vectors will be EXACTLY equal so therefor it is better to compare using a threshold.
カテゴリ
ヘルプ センター および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!