MATLAB: How do you stop the rest of a script from running after a loop break

if statementwhile loop

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')

Best Answer

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.