I want to create an if loop inside multiple if loops that are already present

3 ビュー (過去 30 日間)
A Poyser
A Poyser 2023 年 3 月 30 日
編集済み: A Poyser 2023 年 3 月 30 日
I want to stop a piece of code once a certain variable goes past a certain number.
I am not sure exactly where to put it but essentially I want the code to stop once Vr is < arbitrary number, 0.004, let us say
if Vf_new < Vr(3,1) && delta_Vfc >= 1E-6
%
disp('update')
Vr(3,1) = Vf_new;
iter = iter + 1;
%
elseif delta_Vfc < 1E-6 || Vf_new <= Vr(3,1)
%
disp('do not update')
continue_iteration = 0;
%
end
It wants to appear here.
I am just not sure how to nest it inside an already existent if elif loop. I think it should look like this...
if Vf_new < 0.004
return
end
Help please
Thanks
Alex

採用された回答

Image Analyst
Image Analyst 2023 年 3 月 30 日
Not sure what you have: a for loop or multiple if/else blocks. An "if" block is not called a loop because it does not loop/iterate.
Let's say that you have a for loop and inside the for loop is your if tests and you want to check Vf_new at the beginning of the loop
for k = 1 : 10000
if Vf_new < 0.004
break; % Breaks out of the for loop and continues the program after the for loop.
% OR you can exit the loop and the entire function or program you're
% in by calling return;
% return; % Step out of the entire program.
elseif Vf_new < Vr(3,1) && delta_Vfc >= 1E-6
fprintf('Updating. Now Vf_new = %f.\n', Vf_new);
Vr(3,1) = Vf_new;
iter = iter + 1; % If iter is your for loop variable, then you don't need this line. If it's a while loop you may need this or something like it.
elseif delta_Vfc < 1E-6 || Vf_new <= Vr(3,1)
fprintf('NOT updating. Now Vf_new = %f.\n', Vf_new);
continue_iteration = 0; % If your loop is a for loop, then you don't need this line. If it's a while loop you may need this or something like it.
break; % Get out of loop. Do not continue iterating.
end
end
  1 件のコメント
A Poyser
A Poyser 2023 年 3 月 30 日
編集済み: A Poyser 2023 年 3 月 30 日
It is quite a long for loop so I only added what I thought was important for brevity, I will add a little more for context.
N = 1000;
%
% materials properties and preset variables goes here
%
for k = 1:N
%
k;
%
sigma_infty(:,k) = [10*k 0 0 0 0 0]'; % far-field stress
%
%
% Crack density parameter (from Budiansky and O'Connel, IJSS 12:81-97,
% 1976) in composite
cdp = 0.05; %
Vcc = 4/3*pi*cdp*a3_over_a1; % compute volumetric density of cracks in composite
%
if k == 1
Vr(3,1) = Vfc; % volume fraction of fibres
else
Vr(3,1) = Vfc*exp(-(sigma_f(1,k-1)/sYW)^mW); % volume fraction of the fibres corrected for failure based on the previous iteration
end
delta_Vfc = 1E6;
iter = 1;
continue_iteration = 1;
%
while continue_iteration && iter < 10
%
% some maths to calculate mechanical properties goes here...
%
Vf_new = Vfc*exp(-(sigma_f(1,k)/sYW)^mW);
delta_Vfc = abs(Vf_new-Vr(3,1));
%
if Vf_new < Vr(3,1) && delta_Vfc >= 1E-6
%
disp('update')
Vr(3,1) = Vf_new;
iter = iter + 1;
%
elseif delta_Vfc < 1E-6 || Vf_new <= Vr(3,1)
%
disp('do not update')
continue_iteration = 0;
%
end
%
end
end
%
I tried adding the piece of code that you suggested but it didn't seem to be able to break the loop.
Thanks
Alex

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by