
A=xlsread('numeros aleatorios.xlsx','AS3:BL52562');
B=xlsread('numeros aleatorios.xlsx','BO3:CH52562');
P=xlsread('potencia, fallos y energia.xlsx','D4:D52563');


M=zeros(52560,20);
FP=zeros(1,20);
FM=zeros (1,20);


for j=1:20
    i=1;
    while i<=52560
        if A(i,j)==0 && B(i,j)==0
            M(i,j)=P(i);
            i=i+1;
        
        
        elseif A(i,j)==0 && B(i,j)==1 && P(i)>0
                 FM(j)=FM(j)+1; 
                i=i+1095;
            
                
        elseif A(i,j)==1 && B(i,j)==0 && P(i)>0 
                   FP(j)=FP(j)+1; 
                    i=i+720;
            
                    
        elseif A(i,j)==1 && B(i,j)==1 &&  P(i)>0
               FM(j)=FM(j)+1;
               FP(j)=FP(j)+1;
               i=i+1095;
            
        else
            i=i+1;
        end
              
            
     end
    
    
end
xlswrite('potencia, fallos y energia.xlsx',M,1,'L4:AE52563');
xlswrite('potencia, fallos y energia.xlsx',FP,1,'AH4:BA4');
xlswrite('potencia, fallos y energia.xlsx',FM,1,'AH10:BA10');

EENS=zeros(1,20);

for j=1:20
    i=1;
    while i<=52560
        EENS(j)=EENS(j)+((P(i)-M(i,j))*10/60);
        i=i+1;
    end
end
xlswrite('potencia, fallos y energia.xlsx',EENS,1,'AH16:BA16');

N=M*(10/60);
EES=zeros(1,20);
for j=1:20
    i=1;
    while i<=52560
        EES(j)=EES(j)+N(i,j);
        i=i+1;
    end
end

xlswrite('potencia, fallos y energia.xlsx',N,1,'BD4:BW52563');
xlswrite('potencia, fallos y energia.xlsx',EES,1,'BD52566:BW52566');

PM=xlsread('datos economicos.xlsx',1,'L62:L81');
PP=xlsread('datos economicos.xlsx',1,'S62:S81');
TAMA=xlsread('datos economicos.xlsx',1,'H13');

COM=20*EES;
ING=zeros(1,20);
CEENSSP=zeros(1,20);
CEENS=zeros(1,20);
CRP=zeros(1,20);
INGSP=zeros(1,20);
COMA=zeros(1,20);
LI=xlsread('resultados.xlsx',1,'F38');
WACC=xlsread('resultados.xlsx',1,'F34');
LCOE=zeros(1,20);
preciop=xlsread('resultados.xlsx',1,'F29');
preciom=xlsread('resultados.xlsx',1,'F30');
CAF=xlsread('resultados.xlsx',1,'D11:W11');
BAI=zeros(1,20);
BAISP=zeros(1,20);
IMP=zeros(1,20);
IMPSP=zeros(1,20);
tipoimp=xlsread('resultados.xlsx',1,'F36');
BN=zeros(1,20);
BNSP=zeros(1,20);
CFO=zeros(1,20);
CFOSP=zeros(1,20);
RINV=xlsread('resultados.xlsx',1,'W17');

RINVI=zeros(1,20);

for j=1:19
    RINVI(j)=0;
end
RINVI(20)=RINV;

CFI=zeros(1,20);
CFISP=zeros(1,20);
CFIA=zeros(1,20);
CFIASP=zeros(1,20);
INVI=xlsread('resultados.xlsx',1,'C7');

PMC=zeros(1,20);

for j=1:20
    PMC(j)=PM(j)*(TAMA^j);
end

for j=1:20
    ING(j)=(PMC(j)+PP(j))*EES(j);
end

for j=1:20
    INGSP(j)=PMC(j)*EES(j);
end

for j=1:20
    CEENS(j)=(PMC(j)+PP(j))*EENS(j);
end

for j=1:20
    CEENSSP(j)=(PMC(j))*EENS(j);
end

for j=1:20
    CRP(j)=FP(j)*preciop+FM(j)*preciom;
end


for j=1:20
    COMA(j)=COM(j)/((1+WACC)^j);
end

for j=1:20
    LCOE(j)=(LI+COMA(j))/EES(j);
end



for j=1:20
    BAI(j)=ING(j)-COM(j)-CRP(j)-CAF(j);
end

for j=1:20
    BAISP(j)=INGSP(j)-COM(j)-CRP(j)-CAF(j);
end


credf=0;
for j=1:20
    if BAI(j)>=0
        if credf<(BAI(j)*tipoimp)
   IMP(j)=BAI(j)*tipoimp-credf;
   credf=0;
        elseif credf>(BAI(j)*tipoimp)
            IMP(j)=0;
            credf=credf-(BAI(j)*tipoimp); 
        end
    else
        IMP(j)=0;
        credf=credf+abs(tipoimp*BAI(j));
        
    end
end
credf=0;
for j=1:20
    if BAISP(j)>=0
        if credf<(BAISP(j)*tipoimp)
   IMPSP(j)=BAISP(j)*tipoimp-credf;
   credf=0;
        elseif credf>(BAISP(j)*tipoimp)
            IMPSP(j)=0;
            credf=credf-(BAISP(j)*tipoimp); 
        end
    else
        IMPSP(j)=0;
        credf=credf+abs(tipoimp*BAISP(j));
        
    end
end




for j=1:20
    BN(j)=BAI(j)-IMP(j);
end

for j=1:20
    BNSP(j)=BAISP(j)-IMPSP(j);
end



for j=1:20
    CFO(j)=BN(j)+CAF(j);
end

for j=1:20
    CFOSP(j)=BNSP(j)+CAF(j);
end



for j=1:19
    CFI(j)=CFO(j);
end
CFI(20)=CFO(20)+RINV;

for j=1:19
    CFISP(j)=CFOSP(j);
end
CFISP(20)=CFOSP(20)+RINV;

for j=1:20
    CFIA(j)=CFI(j)/((1+WACC)^j);
end

for j=1:20
    CFIASP(j)=CFISP(j)/((1+WACC)^j);
end

SUMP=0;
VAN=0;

for j=1:20
    SUMP=SUMP+CFIA(j);
end
VAN=-INVI+SUMP;

VANSP=0;
SUMP=0;

for j=1:20
    SUMP=SUMP+CFIASP(j);
end
VANSP=-INVI+SUMP;

SUMP=0;
PB=0;
for j=1:20
    SUMP=SUMP+CFI(j);
    if SUMP>INVI
        PB=j;
        break;
    end
    
end
    
SUMP=0;
PBSP=0;
for j=1:20
    SUMP=SUMP+CFISP(j);
    if SUMP>INVI
        PBSP=j;
        break;
    end
    
end

MAT1=[ING;COM;CRP;CAF;BAI;IMP;BN;CAF;CFO;RINVI;CFI];

MAT2=[INGSP;COM;CRP;CAF;BAISP;IMPSP;BNSP;CAF;CFOSP;RINVI;CFISP];

xlswrite('resultados.xlsx',MAT1,1,'D8:W18');
xlswrite('resultados sin primas.xlsx',MAT2,1,'D8:W18');
    


xlswrite('resultados sin primas.xlsx',LCOE,1,'D42:W42');
xlswrite('resultados.xlsx',LCOE,1,'D42:W42');
xlswrite('calculo de costes.xlsx',COM,1,'A9:T9');
xlswrite('calculo de costes.xlsx',ING,1,'A14:T14');
xlswrite('calculo de costes.xlsx',INGSP,1,'W14:AP14');
xlswrite('calculo de costes.xlsx',CEENS,1,'A19:T19');
xlswrite('calculo de costes.xlsx',CEENSSP,1,'W19:AP19');
xlswrite('calculo de costes.xlsx',CRP,1,'A24:T24');

xlswrite('resultados sin primas.xlsx',VANSP,1,'E46');
xlswrite('resultados.xlsx',VAN,1,'E46');

xlswrite('resultados sin primas.xlsx',PBSP,1,'E50');
xlswrite('resultados.xlsx',PB,1,'E50');

VAN
VANSP
PB
PBSP

disp('HECHO');