function [c ceq]= const_fun_24busp(x)
int=1;
cti=0.2; %CTI values taken as 0.2 following ref paper
tmin=0.02;

TDS1 = x(1,:);
K1 = x(2,:);
TDS2 = x(3,:);
K2 = x(4,:);
Vt_FRT=[79 80 81 82 83 84 85 86];
DFIG_bus=[4 5 6 8 14 17 19 20];
load('Ip.mat');
Ip=Ipp;

load('Fault_analysis_24bus_8dfig.mat')

first_fl=1; %accounting for all fault locations- 29 nodes
last_fl=29;
time_sum=0;


for i=1:4  %types of faults 1=SLG, 4=3phase, 2=DLG, 3=LL 
    for j=1:3
        clear ISC V  t ISCb ISCc Vb Vc Va VFRT
        
        
        if i==1 || i==4
            ISC=Fault_analysis.(['ISCa' num2str(i) num2str(j)]); %KHALED
            V=Fault_analysis.(['Vfa' num2str(i) num2str(j)]);
        elseif i==2 || i==3
            
            ISCb=Fault_analysis.(['ISCb' num2str(i) num2str(j)]);
            ISCc=Fault_analysis.(['ISCc' num2str(i) num2str(j)]);
            
            Vb=Fault_analysis.(['Vfb' num2str(i) num2str(j)]);
            Vc=Fault_analysis.(['Vfc' num2str(i) num2str(j)]);
            ISC=max(ISCb,ISCc);
            V=min(Vb,Vc);
        end
        
        Va=Fault_analysis.(['Vfa' num2str(i) num2str(j)]);
        Vb=Fault_analysis.(['Vfb' num2str(i) num2str(j)]);
        Vc=Fault_analysis.(['Vfc' num2str(i) num2str(j)]);
        VFRT=(Va+Vb+Vc)/3;
        
        
        for fl=first_fl:last_fl
            
            
            MAP=MAPPING(fl,first_fl);
            for k=1:2
                indp(k)=MAP(k,1);
                indb1(k)=MAP(k,2);
                indb2(k)=MAP(k,3);
                indb3(k)=MAP(k,4);
                indb4(k)=MAP(k,5);
                
                % coordination between primary and backup1 constraints and time
                % constraint on primary and backup1
                if indp(k)~=0 && indb1(k)~=0
                    
                    t(indp(k),fl)=((1/(exp(1-V(indp(k),fl))))^K1(indp(k)))*0.14*TDS1(indp(k))/(-1 + (ISC(indp(k),fl)/Ip(indp(k)))^.02 ); %TCV primary relay tripping char. (eqn1)
                    t(indb1(k),fl)=((1/(exp(1-V(indb1(k),fl))))^K2(indb1(k)))*0.14*TDS2(indb1(k))/(-1 + (ISC(indb1(k),fl)/Ip(indb1(k)))^.02 );%TCV backup relay tripping char. (eqn1)
                    
                    const(int)=cti+t(indp(k),fl)-t(indb1(k),fl); %inequality contraint eqn(3) from ref paper
                    int=int+1;
                    const(int)=tmin-t(indp(k),fl); %inequality const - eqn(5) in ref paper
                    int=int+1;
                    const(int)=tmin-t(indb1(k),fl);%inequality const - eqn(5) in ref paper
                    int=int+1;
                end
                
                
                % constraint on primary and backup2
                if indp(k)~=0 && indb2(k)~=0
                    
                    t(indp(k),fl)=((1/(exp(1-V(indp(k),fl))))^K1(indp(k)))*0.14*TDS1(indp(k))/(-1 + (ISC(indp(k),fl)/Ip(indp(k)))^.02 );
                    t(indb2(k),fl)=((1/(exp(1-V(indb2(k),fl))))^K2(indb2(k)))*0.14*TDS2(indb2(k))/(-1 + (ISC(indb2(k),fl)/Ip(indb2(k)))^.02 );
                    
                    const(int)=cti+t(indp(k),fl)-t(indb2(k),fl);
                    int=int+1;
                    const(int)=tmin-t(indb2(k),fl);
                    int=int+1;
                end
                
                
                % constraint on primary and backup3
                if indp(k)~=0 && indb3(k)~=0
                    
                    t(indp(k),fl)=((1/(exp(1-V(indp(k),fl))))^K1(indp(k)))*0.14*TDS1(indp(k))/(-1 + (ISC(indp(k),fl)/Ip(indp(k)))^.02 );
                    t(indb3(k),fl)=((1/(exp(1-V(indb3(k),fl))))^K2(indb3(k)))*0.14*TDS2(indb3(k))/(-1 + (ISC(indb3(k),fl)/Ip(indb3(k)))^.02 );
                    
                    const(int)=cti+t(indp(k),fl)-t(indb3(k),fl);
                    int=int+1;
                    const(int)=tmin-t(indb3(k),fl);
                    int=int+1;
                end
                
                % constraint on primary and backup4
                if indp(k)~=0 && indb4(k)~=0
                    
                    t(indp(k),fl)=((1/(exp(1-V(indp(k),fl))))^K1(indp(k)))*0.14*TDS1(indp(k))/(-1 + (ISC(indp(k),fl)/Ip(indp(k)))^.02 );
                    t(indb4(k),fl)=((1/(exp(1-V(indb4(k),fl))))^K2(indb4(k)))*0.14*TDS2(indb4(k))/(-1 + (ISC(indb4(k),fl)/Ip(indb4(k)))^.02 );
                    
                    const(int)=cti+t(indp(k),fl)-t(indb4(k),fl);
                    int=int+1;
                    const(int)=tmin-t(indb4(k),fl);
                    int=int+1;
                end
                
                if indp(k)~=0 %constraint on primary relay only (backup ignored)
                    t(indp(k),fl)=((1/(exp(1-V(indp(k),fl))))^K1(indp(k)))*0.14*TDS1(indp(k))/(-1 + (ISC(indp(k),fl)/Ip(indp(k)))^.02 );
                    
                    const(int)=tmin-t(indp(k),fl);
                    int=int+1;
                end
                if indb1(k)~=0 %constraint on primary backup1 only 
                    t(indb1(k),fl)=((1/(exp(1-V(indb1(k),fl))))^K2(indb1(k)))*0.14*TDS2(indb1(k))/(-1 + (ISC(indb1(k),fl)/Ip(indb1(k)))^.02 );
                    
                    const(int)=tmin-t(indb1(k),fl);
                    int=int+1;
                end
                if indb2(k)~=0 %constraint on primary backup2 only 
                    t(indb2(k),fl)=((1/(exp(1-V(indb2(k),fl))))^K2(indb2(k)))*0.14*TDS2(indb2(k))/(-1 + (ISC(indb2(k),fl)/Ip(indb2(k)))^.02 );
                    
                    const(int)=tmin-t(indb2(k),fl);
                    int=int+1;
                end
                
                if indb3(k)~=0 %constraint on primary backup3 only 
                    t(indb3(k),fl)=((1/(exp(1-V(indb3(k),fl))))^K2(indb3(k)))*0.14*TDS2(indb3(k))/(-1 + (ISC(indb3(k),fl)/Ip(indb3(k)))^.02 );
                    
                    const(int)=tmin-t(indb3(k),fl);
                    int=int+1;
                end
                
                if indb4(k)~=0 %constraint on primary backup4 only 
                    t(indb4(k),fl)=((1/(exp(1-V(indb4(k),fl))))^K2(indb4(k)))*0.14*TDS2(indb4(k))/(-1 + (ISC(indb4(k),fl)/Ip(indb4(k)))^.02 );
                    
                    const(int)=tmin-t(indb4(k),fl);
                    int=int+1;
                end

            end
        end
    end
end

[c]=[const']; %C-inequality constraint to be fed into objective function
[ceq] = []; %equality const ignored in this function. 

