function f = myfun_24busp(x)
%objective is to minimize operating time 
%function finds total time according to the dual TCV relay charac. (eqn(1))

TDS1 = x(1,:);
K1 = x(2,:);
TDS2 = x(3,:);
K2 = x(4,:);

load('Ip.mat');
Ip=Ipp;

load('Fault_analysis_24bus_8dfig.mat')

first_fl=1;
last_fl=29;
time_sum=0;


for i=1:4
    for j=1:3
        
        clear ISC V  t ISCb ISCc Vb Vc
        
        if i==1 || i==4
            ISC=Fault_analysis.(['ISCa' num2str(i) num2str(j)]);
            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)]);
            ISC=max(ISCb,ISCc);
            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
        
        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);
                if indp(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 );
                    time_sum=time_sum+ t(indp(k),fl);
                end                
                
                if indb1(k)~=0
                    
                    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 );
                    time_sum=time_sum+ t(indb1(k),fl);
                end                
                
                if indb2(k)~=0
                    
                    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 );
                    time_sum=time_sum+ t(indb2(k),fl);
                end                
                
                if indb3(k)~=0
                    
                    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 );
                    time_sum=time_sum+ t(indb3(k),fl);
                end          
                
                if indb4(k)~=0
                    
                    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 );
                    time_sum=time_sum+ t(indb4(k),fl);
                end
                                          
            end
        end
    end
end
f=time_sum;



