How do you stop the rest of a script from running after a loop break?
1 view (last 30 days)
Show older comments
I have attached my code, Where I am trying to use if statement inside a while loop to break the loop if a certain condition was not met. I don't want the rest of the script to run as the loop is broken. Any ideas on how this can work?
X1=input('First Initial Guess')%Input First guess for tm
X0=input('Second Initial Guess')%Input Second guess for tm
for i=1:N %iterating from 1 to N for DOT and Time
f = @(X1) ((-(100-DOT(i)))/(100))+(1/(X1-60))*(X1*exp(-Time(i)/X1)-60*exp(-Time(i)/60));%anonymous function solving for the initial condition
f = @(X0) ((-(100-DOT(i)))/(100))+(1/(X0-60))*(X0*exp(-Time(i)/X0)-60*exp(-Time(i)/60));%anonymous function solving for the second guess of tm
n=0;% iteration counter
while abs((f(X1)))>0.001 %While statment, if satisfied the loop will stop and move to next i
X2=X1-((X1-X0))*(f(X1)/(f(X1)-f(X0)));% the new value of tm calculated using Newton-Raphson Method, where it eventually converge to a final value
n=n+1%the counter of the numbers of while loop ran before the statment condition is satisfied
X0=X1;%setting the second conditon as intital condition to solve f(x0)
X1=X2%setting the new value as the second conditoin to solve for f(x1 the new value of x2
if n>100 %if X1 doesnt converge to exact root (Keep looping)
disp('Consider Changing Your Initial Guesses')%Display a Guiding message
break %breaks out the loops
elseif X1==Inf || X1==-Inf
disp('Consider Changing Your Initial Guesses')%Display a Guiding message
break %breaks out the loops
end
end
Counter(i)=n; %The number of while loops for every i is stored in a vector
tm(i)=X1;% the values of calculated tm for every i is stored in a vector
end
tm_Average=mean(tm)%the average of tm (seconds)
%solving for KlA (Dissolving Rate)
KLa=(1/tm_Average)*(3600)% The Dissolving Rate per hour
KLA=(tm.^-1)*3600;%Individual KLas(1/hr) for each tm
%Copying the outputs in Excel file (OutputData)
xlswrite('OutputData.xlsx',tm',1,'A2:A1000')
xlswrite('OutputData.xlsx',KLA',1,'B2:B1000')
xlswrite('OutputData.xlsx',Counter',1,'C2:C1000')
xlswrite('OutputData.xlsx',KLa,1,'E2:E2')
xlswrite('OutputData.xlsx',tm_Average,1,'D2:D2')
0 Comments
Accepted Answer
Star Strider
on 5 Nov 2016
I am not certain what you want to do. Replacing break with return will stop the script, in addition to breaking the loop.
Experiment to get the result you want.
2 Comments
Star Strider
on 5 Nov 2016
In that instance, keep the break calls as they are because they’re doing what you want them to. Don’t replace them.
Instead, I would set a flag in the if/elseif blocks, and then test it at the end of the loop to do the return, something like:
if n>100 %if X1 doesnt converge to exact root (Keep looping)
disp('Consider Changing Your Initial Guesses')%Display a Guiding message
brk_flg_1 = true; % Set Flag
break %breaks out the loops
elseif X1==Inf || X1==-Inf
disp('Consider Changing Your Initial Guesses')%Display a Guiding message
brk_flg_2 = true; % Set Flag
break %breaks out the loops
end
Then somewhere after the end call, test for the flags and execute the return. For example:
end % <— End Of ‘for’ Loop
if (brk_flg_1 || brk_flg_2) % Test Flags, ‘return’ If Either Is Set
return
end
tm_Average=mean(tm)%the average of tm (seconds)
< REST OF YOUR CODE >
You will have to define the logic to do what you want. This is the essential approach I would use.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!