Q:

How to fix this code so that it stops when the condition becomes true?

I wrote the below code to estimate a specific value of epsilon and the code should stop the simulation when the condition becomes true (i.e. w(t)-f < error). Unfortunately the code didn't stop when the condition became true.
clear;clc;
tic
m=360;   n=20;  d=240;  l=240;error=1e-5;RF=1.8;erin=0;
error_alt=(5e-4)*180/pi;
deltheta=2*pi/m;    delz=1/n;
pin=0;f=1;
s=(d*d)/(l*l);
R=1/deltheta^2;
cor=(deltheta^2/delz^2)*s;
x=0:deltheta:2*pi;
y=0:delz:1;
iter=1000; 
sum=zeros(iter);
[epsilon]=0.01:0.01:0.9;
[theta]=0:deltheta:2*pi;
p0=zeros(m+1,n+1);
p=zeros(m+1,n+1);
cc=zeros(m+1,n+1);
mm=zeros(m+1,n+1);
wr=zeros(length(epsilon));
wt=zeros(length(epsilon));
w=zeros(length(epsilon));
for t=1:length(epsilon)
    h=zeros(m+1);
    p0=zeros(m+1,n+1);
    p=zeros(m+1,n+1);
    cc=zeros(m+1,n+1);
    mm=zeros(m+1,n+1);
    wt(t)=0;wr(t)=0;w(t)=0;
    phi1(t)=0;phi(t)=0;
    for i=1:m+1  
        h(i)=1+epsilon(t).*cos(theta(i));
    end
    sum(1)=0;
    phi1(t)=atan(sqrt(1.0-epsilon(t).^2)./epsilon(t));
    phi1(t)=m+1-(phi1(t)*180/pi);
    for k=1:iter
        sumij=0.0;
        at=round(phi1(t));
        for j=2:n
            for i=(at-9):(at+9);
                p0(i,j)=pin;
            end
        end
        for j=2:n
            p0(m+1,j)=p0(1,j);
        end
        for j=2:n
            for i=1:m
                if (i==1)
                    h1=1+epsilon(t).*cos(theta(i)+0.5*deltheta);
                    h2=1+epsilon(t).*cos(theta(i)-0.5*deltheta);
                    conh=h1-h2;
                    const=2*(1+cor)*R*h(i)^3;
                    A=3*h(i)^2*R*conh/const;
                    C=h(i)^3*R/const;
                    E=h(i)^3*R*cor/const;
                    G=-3*sqrt(R)*conh/const;
                    p(i,j)=(C+A/2)*p0(i+1,j)+(C-A/2)*p0(m,j)+E*(p0(i,j+1)+p0(i,j-1))+G;           
                else
                    conh=(h(i+1)-h(i-1))/2;
                    const=2*(1+cor)*R*h(i)^3;
                    A=3*h(i)^2*R*conh/const;
                    C=h(i)^3*R/const;
                    E=h(i)^3*R*cor/const;
                    G=-3*sqrt(R)*conh/const;
                    p(i,j)=(C+A/2)*p0(i+1,j)+(C-A/2)*p0(i-1,j)+E*(p0(i,j+1)+p0(i,j-1))+G;
                end
                if p(i,j)<0
                    p(i,j)=0.0;
                else
                    sumij=sumij+p(i,j);
                end
            end
        end
    for j=2:n
        p(m+1,j)=p(1,j);
    end
    for j=2:n
        for i=(at-9):(at+9)
            p(i,j)=pin;
        end
    end
    sum(k+1)=sumij;
    errorp=abs(sum(k+1)-sum(k))/abs(sum(k+1));
    if (errorp > error)
        for j=2:n
            for i=1:m
                p0(i,j)=RF*p(i,j)+(1-RF)*p0(i,j);
            end
        end
    else
    end
    end
    for j=2:n
        for i=2:m
            cc(i,j)=p(i,j).*sin(theta(i));  
            mm(i,j)=p(i,j).*cos(theta(i)); 
        end
    end
    wr(t)=trapz(y,trapz(x,cc));
    wt(t)=trapz(y,trapz(x,mm));
    w(t)=sqrt(wr(t).^2+wt(t).^2);
    phi(t) = atan2(wt(t), wr(t));
    phi(t)=m+1+(phi(t)*180/pi);
    erin=abs(phi(t)-phi1(t))/abs(phi(t));
    if (erin > error_alt)
        phi1=RF*phi+(1-RF)*phi1;
        at=round(phi1); 
    else
        if abs((w(t)-f)) < error
        break        
        end
    end
    if abs((w(t)-f)) < error
       epsilon(t)
       break        
    end
end
iterations=k;
itera=epsilon;
figure(1)
surf(p)
figure(2)
plot(p)
toc

A:

You are looking for the keywork return.
The difference between break and return is that 

As said in the documentation, break is used to exit a loop. 
return will exit the function. 

Even thought return is built to exit functions, it also work in scripts.
So in your example, you should replace break by return
if abs((w(t)-f)) < error
    return       
end

Note that this keywork works no matter how deep you are in a nested loop. As an example :
for ii = 1:2
    for jj = 1:2
        for kk = 1:2
            disp(kk)
            return
        end
    end
end

1

while the break keyword will only end one loop :
for ii = 1:2 
    for jj = 1:2 
        for kk = 1:2
            disp(kk)
            break
        end 
    end 
end

1
1
1
1

