pom=pom;
Remove["Global`*"];
SetDirectory[NotebookDirectory[]];
$HistoryLength=3;
pom=Import["dat1a.xls"][[1]];
startdat=pom[[1,1]];
vykony=(10^3)*(Last/@pom);
PmaxOdber=Max[vykony];
PminOdber=Min[vykony];
Print["Pmax= ",PmaxOdber," W,  Pmin= ",PminOdber," W"];
hodina=3600;
ctvrthodina=0.25*hodina;

nden=24*4;

(* utvareni funkce pro VT a NT *)

v1 = ConstantArray[False,2*4+3];(* VT 0to2h45  *)
v2= ConstantArray[True,4*4]; (*NT 2h45to6h45 *)
v3= ConstantArray[False,8*4];(* VT 6h45to14h45 *)
v4 =  ConstantArray[True,2*4];(*NT 14h45to16h45 *)
v5 = ConstantArray[False,4*4+2];(*VT*)
v6 = ConstantArray[True,2*4];(*NT 21h15to23h15*)
vKonec = ConstantArray[False,3];
sablonaDen = Join[v1,v2,v3,v4,v5,v6,vKonec];
sablonaDen=RotateLeft[sablonaDen,-3];

datP =vykony;
 base[n_] :=sablonaDen[[n]];

U=400;
(* cena za elektrinu pro NT,VT a prikon*)
cena1JoulePeak=(3.35549/(hodina*1000));(*cena VT*)
cena1JouleBase=(1.08081/(hodina*1000));(*cena NT*)
cosFi=0.95;
cenazaPrikon[PmaxPeakSjednany_]:=Module[{PmaxOdberCelkem,Proud,cenaPrikonu},
Proud=(PmaxPeakSjednany/((Sqrt[3])*U*cosFi));
Proud*13.43
]

ηAccu=0.9;
PvybMax=30000;
fPenale[ΔP_]:=Exp[(10^(-4))ΔP]-1;
dobaZivota=10*365*24*4; (*doba zivota baterie ve ctvrthodinach, na 10 let*)

cenaJoule=7000*25.92/(14000*3600);(* Tesla powerwall 2*)
 (*Tesla powerpack pri 100 kWh*)
baterieSystem=0;

Letni udaje

(*brezen-31,duben-30,kveten-31,cerven-30,cervenec-31,srpen-31,zari-30,rijen-31,listopad-30,prosinec-31*)
(*pocatecniden -(*patek*)*)
letniVykony=Take[vykony,{nden*(31+30),nden*(31+30+31+30+31+31)}];
(*hodnoty za cerven-srpen*)
(*zacatek letnich hodnot - streda*)

dnyLetniVykony=Partition[letniVykony,24*4];
tydenLetniVykony=Partition[dnyLetniVykony,7];

prumLetniSt=Mean[tydenLetniVykony[[All,1]]];
prumLetniCt=Mean[tydenLetniVykony[[All,2]]];
prumLetniPa=Mean[tydenLetniVykony[[All,3]]];
prumLetniSo=Mean[tydenLetniVykony[[All,4]]];
prumLetniNe=Mean[tydenLetniVykony[[All,5]]];
prumLetniPo=Mean[tydenLetniVykony[[All,6]]];
prumLetniUt=Mean[tydenLetniVykony[[All,7]]];

prumVsedLetDen=(prumLetniPo+prumLetniUt+prumLetniSt+prumLetniCt+prumLetniPa)/5;
prumLetVikend=(prumLetniSo+prumLetniNe)/2;

ListPlot[{prumVsedLetDen}]
ListPlot[{prumLetniPo,prumLetniUt,prumLetniSt,prumLetniCt,prumLetniPa,prumLetniSo,prumLetniNe},Joined->True,PlotLegends->{"Po","Ut","St","Ct","Pa","So","Ne"},GridLines->Automatic,PlotLabel->"Prubeh spotreby pro Kveten - Zari"]

Zimni udaje

zimniVykony=Take[vykony,{nden*(31+30+31+30+31+31+30),nden*(31+30+31+30+31+31+30+31+30+30)}];

dnyZimniVykony=Partition[zimniVykony,24*4];
tydenZimniVykony=Partition[dnyZimniVykony,7];

prumZimniSt=Mean[tydenZimniVykony[[All,2]]];
prumZimniCt=Mean[tydenZimniVykony[[All,3]]];
prumZimniPa=Mean[tydenZimniVykony[[All,4]]];
prumZimniSo=Mean[tydenZimniVykony[[All,5]]];
prumZimniNe=Mean[tydenZimniVykony[[All,6]]];
prumZimniPo=Mean[tydenZimniVykony[[All,7]]];
prumZimniUt=Mean[tydenZimniVykony[[All,1]]];

prumVsedZimDen=(prumZimniPo+prumZimniUt+prumZimniSt+prumZimniCt+prumZimniPa)/5;
prumZimVikend=(prumZimniSo+prumZimniNe)/2;

ListPlot[{prumVsedZimDen,prumZimVikend,prumLetVikend,prumVsedLetDen},GridLines->Automatic]
ListPlot[{prumZimniPo,prumZimniUt,prumZimniSt,prumZimniCt,prumZimniPa,prumZimniSo,prumZimniNe},Joined->True,PlotLegends->{"Po","Ut","St","Ct","Pa","So","Ne"},GridLines->Automatic,PlotLabel->"Prubeh spotreby pro Rijen - Prosinec"]

max=Max[prumVsedLetDen];
dataLetoVsed=ConstantArray[0,96];
dataLetoVikend =ConstantArray[0,96];
dataZimaVsed=ConstantArray[0,96];
dataZimaVikend=ConstantArray[0,96];

(*utvareni dat pro DateListPlot*)
For[i=1,i<96+1,i++,{dataLetoVsed[[i]]=List[List[2006,10,10,Floor[i/4],Mod[i,4]*15],prumVsedLetDen[[i]]]}]
For[i=1,i<96+1,i++,{dataLetoVikend[[i]]=List[List[2006,10,10,Floor[i/4],Mod[i,4]*15],prumLetVikend[[i]]]}]
For[i=1,i<96+1,i++,{dataZimaVsed[[i]]=List[List[2006,10,10,Floor[i/4],Mod[i,4]*15],prumVsedZimDen[[i]]]}]
For[i=1,i<96+1,i++,{dataZimaVikend[[i]]=List[List[2006,10,10,Floor[i/4],Mod[i,4]*15],prumZimVikend[[i]]]}]

prumVsedZimDen=RotateLeft[prumVsedZimDen,-3];
DateListPlot[{dataLetoVsed,dataLetoVikend,dataZimaVsed,dataZimaVikend},GridLines->Automatic,PlotLabel->"Průběh spotřeby pro průměrné dny",PlotLegends->{"Léto-všední den","Léto-víkend","Zima-všední den","Zima-víkend"},FrameLabel->"[ W ]"]
(* cena za spotrebu pro prumerne dny
*)

cenaZimVikendBB=4223.69;
cenaZimDenBB=6604;
cenaLetDenBB=4425.28;
cenaLetVikendBB=1924.57;

denCtvrthodiny=dataLetoVsed[[All,1]]; (* vektor dennich hodnot pro vykreslovani DateListPlot grafu*)

(* kolik muzu maximalne vybit z baterie pro zachovani SOC jako na zacatku prumerneho dne*)
muzuDobit2[data_,n_,Pmax_]:=Module[{NTPozice,NTHodnoty,Pdisponibilni,PdisponibilniNT},Pdisponibilni=ConstantArray[0,Length[data]];
For[i=1,i<97,i++,Pdisponibilni[[i]]=Max[{Pmax-data[[i]],0}]];PdisponibilniNT=Pdisponibilni*Boole[sablonaDen];Total[Take[PdisponibilniNT,{1,96}]]/Length[Take[sablonaDen,{1,96}]]]

Vypocet optimalni baterie

cena[{PmaxSjednany_,EnergieAkumulaceMax_,data_,dopocet_,diskont_}]:=Module[{krokPeak,krokBase,krok,pocatek,nEnd,WaccuEnd,WbaseEnd,WpeakEnd,penaleEnd,cenaEnergie,cenaCelkem,prubehNabijeni,vystup,PmaxVybijim,cenaNT,cenaVT,cenaPrikon,cenaBaterieJ,cenaSystem,penaleStart,usetreno,cashFlow,NPV,maxPrikon,a,b},
penaleStart=0;
(*overeni aby nepouzivla program zapornou hodnotu baterie,zaporny vykon nez je mozny a jine blbosti*)
If[EnergieAkumulaceMax<0,penaleStart=-EnergieAkumulaceMax*1111111111111111];
If[PmaxSjednany<0,penaleStart=-PmaxSjednany*1111111111111111];

(*1.]] 
rozepsana funkce co delat kdyz je nizsi tarif*)
krokBase[{n_,Waccu_,WodebranaBase_,WodebranaPeak_,penale_}]:=Module[{Ppozadovany,Pdisponibilni,EnergieCoMuzuDobit,WodebranaPeakNew,WodebranaBaseNew,nNew,WaccuNew,EnergiePozadovana,penaleNew,PmuzuVybit,WakuMax,PmaxNabijim},
WodebranaPeakNew=WodebranaPeak;

nNew=n+1;
Ppozadovany=data[[n]];
WakuMax=3.3/6.7/4*EnergieAkumulaceMax;
EnergiePozadovana=ctvrthodina*Ppozadovany;
Pdisponibilni=PmaxSjednany-Ppozadovany;(*JAKOU ENERGII MUZEME DOBIJET*)

PmuzuVybit=WakuMax;
EnergieCoMuzuDobit=Min[{Pdisponibilni*ctvrthodina,WakuMax,EnergieAkumulaceMax-Waccu}];

a={Pdisponibilni*ctvrthodina,WakuMax,EnergieAkumulaceMax-Waccu};
(*vybere minimum z mozne dobijene energie, *)
If[EnergieCoMuzuDobit>0,(WaccuNew=Min[Waccu+EnergieCoMuzuDobit*ηAccu,EnergieAkumulaceMax];WodebranaBaseNew=WodebranaBase+Min[{(EnergieCoMuzuDobit+EnergiePozadovana),PmaxSjednany*ctvrthodina}];penaleNew=penale+0;)];
(*situace, kdy dobijim,OK *)

If[(EnergieCoMuzuDobit<=0&&(Ppozadovany<=PmaxSjednany+PmuzuVybit)),(WaccuNew=Min[{(Waccu-ctvrthodina*(Ppozadovany-PmaxSjednany)),EnergieAkumulaceMax}];
WodebranaBaseNew=WodebranaBase+Min[{PmaxSjednany*ctvrthodina,ctvrthodina*Ppozadovany}];
penaleNew=penale+0;(*Print[EnergieCoMuzuDobit,nNew,"aku: ",WaccuNew/3600000,"puvodni aku ",Waccu ];*))];
(*nedobijim, ale dokazu vykon pokryt z nasmlouvaneho a z akumulace,OK*)

If[(EnergieCoMuzuDobit<0)&&(Ppozadovany>PmaxSjednany+PmuzuVybit),(WaccuNew=Waccu-ctvrthodina*PmuzuVybit;
WodebranaBaseNew=WodebranaBase+ctvrthodina*(Ppozadovany-PmuzuVybit);
penaleNew=1000000000000;)];
(*situace, kdy aku a sjednany vykon dohromady nepokryji potrebu, je penalizovano, NEZADOUCI*)

(*overeni aby nepouzivla program zapornou hodnotu baterie,zaporny vykon nez je mozny a jine blbosti*)
If[Waccu<0,(penaleNew=-Waccu*1111111111111111;WaccuNew=Waccu;WodebranaBaseNew=WodebranaBase+EnergiePozadovana;
)];
If[PmaxSjednany<0,(penaleNew=-PmaxSjednany*1111111111111111;WaccuNew=Waccu;WodebranaBaseNew=WodebranaBase+EnergiePozadovana;)];
If[EnergieAkumulaceMax==0,(WodebranaPeakNew=WodebranaPeak;WodebranaBaseNew=WodebranaBase+EnergiePozadovana;
WaccuNew=Waccu;
penaleNew=penale;)];



{nNew,WaccuNew,WodebranaBaseNew,WodebranaPeakNew,penaleNew}
];
(*2.]] 
rozepasna funkce co delat jestli se pohzbujem ve vyssim tarifu*)
krokPeak[{n_,Waccu_,WodebranaBase_,WodebranaPeak_,penale_}]:=Module[{nNew,WaccuNew,WodebranaBaseNew,penaleNew,Ppozadovany,WdrzimSchovanou,WakuDisponibilni,Wpozadovana,WodebranaPeakNew,PmuzuVybit,WvybMax,MaxWOdebrana,zbyvaPeakN,WakuMax},
nNew=n+1;
Ppozadovany=data[[n]];
Wpozadovana=Ppozadovany*ctvrthodina;
WodebranaBaseNew=WodebranaBase;
WvybMax=3.3/6.7/4*EnergieAkumulaceMax;
MaxWOdebrana=Max[{muzuDobit2[data,n,PmaxSjednany]*ctvrthodina*ηAccu,0}];
WakuDisponibilni=Min[{WvybMax,Waccu(*,MaxWOdebrana*),(*Wpozadovana,*)MaxWOdebrana}];(*,MaxWOdebrana*)
b={WvybMax,Waccu(*,MaxWOdebrana*),Wpozadovana,MaxWOdebrana};
If[Wpozadovana≤WakuDisponibilni,(WaccuNew=Max[{(Waccu-WakuDisponibilni),0}];(*Print["boom",WakuDisponibilni,nNew,"aku: ",WaccuNew/3600000,"puvodni aku ",Waccu ];*)
WodebranaPeakNew=WodebranaPeak;penaleNew=penale;)];
(*mam-li v aku dost, dam ji z aku*)
If[(Wpozadovana>WakuDisponibilni)&&(Wpozadovana<=ctvrthodina*(PmaxSjednany)+WakuDisponibilni),(WaccuNew=Max[{(Waccu-WakuDisponibilni),0}];(*Print[WakuDisponibilni,nNew,"aku: ",WaccuNew/3600000,"puvodni aku ",Waccu ];*)WodebranaPeakNew=WodebranaPeak+Wpozadovana-(Waccu-WaccuNew);
penaleNew=penale;)];
If[(Wpozadovana>WakuDisponibilni)&&(Wpozadovana>ctvrthodina*(PmaxSjednany)+WakuDisponibilni),(WodebranaPeakNew=WodebranaPeak+(Wpozadovana-WakuDisponibilni);WaccuNew=Max[{(Waccu-WakuDisponibilni),0}];
penaleNew=(Wpozadovana-(ctvrthodina*(PmaxSjednany)+WakuDisponibilni))*10000000000000;)];
(*pro pripad, co by nemal nastat, s penale*)
If[EnergieAkumulaceMax==0,(WodebranaPeakNew=WodebranaPeak+Wpozadovana;WodebranaBaseNew=WodebranaBase;
WaccuNew=Waccu;
penaleNew=penale;)];

{nNew,WaccuNew,WodebranaBaseNew,WodebranaPeakNew,penaleNew}
];
krok[stav:{n_,Waccu_,WodebranaBase_,WodebranaPeak_,penale_}]:=If[base[n],krokBase[stav],krokPeak[stav]];
(*rozlisuje jestli to je nizzsi nebo vyssi tarif a pote vzhodnocuje *)
pocatek={1,EnergieAkumulaceMax,0,0,penaleStart};
(*pocatecni hodnota pro vypocet, zacinam s nabitou baterii*)

prubehNabijeni=NestList[krok,pocatek,95];
WpeakEnd=Last[prubehNabijeni][[4]];
WbaseEnd=Last[prubehNabijeni][[3]];
penaleEnd=Last[prubehNabijeni][[5]];
maxPrikon=Max[{Differences[prubehNabijeni[[4]]],Differences[prubehNabijeni[[3]]]}]/ctvrthodina;

cenaVT=cena1JoulePeak*WpeakEnd;
cenaNT=cena1JouleBase*WbaseEnd;
cenaPrikon=cenazaPrikon[PmaxSjednany]*1/30;
cenaBaterieJ=EnergieAkumulaceMax*(96/dobaZivota)*cenaJoule;
cenaSystem=0;

cenaEnergie=cena1JoulePeak*WpeakEnd+cena1JouleBase*WbaseEnd+cenazaPrikon[PmaxSjednany]*1/30+EnergieAkumulaceMax*(96/dobaZivota)*cenaJoule+penaleEnd;

(*vypocet NPV*)
(*usetreno - za 52 tydnu, prvni cast je o vsednich dnech (52*5/2) druha je ovikendech (52)*)
If[dopocet==False,(usetreno=(cenaLetDenBB-(cena[{PmaxSjednany,EnergieAkumulaceMax,prumVsedLetDen,True,diskont}][[2]]-cena[{PmaxSjednany,EnergieAkumulaceMax,prumVsedLetDen,True,diskont}][[6]])+cenaZimDenBB-(cena[{PmaxSjednany,EnergieAkumulaceMax,prumVsedZimDen,True,diskont}][[2]]-cena[{PmaxSjednany,EnergieAkumulaceMax,prumVsedZimDen,True,diskont}][[6]]))*52*5/2+(cenaZimVikendBB-(cena[{PmaxSjednany,EnergieAkumulaceMax,prumZimVikend,True,diskont}][[2]]-cena[{PmaxSjednany,EnergieAkumulaceMax,prumZimVikend,True,diskont}][[6]])+cenaLetVikendBB-(cena[{PmaxSjednany,EnergieAkumulaceMax,prumLetVikend,True,diskont}][[2]]-cena[{PmaxSjednany,EnergieAkumulaceMax,prumLetVikend,True,diskont}][[6]]))*52*2/2;cashFlow=Cashflow[{Join[{-cenaJoule*EnergieAkumulaceMax},ConstantArray[usetreno,10]]}[[1]]]; (* cashflow  za dobu projektu*)
NPV=-N[TimeValue[cashFlow,diskont,0],8];)(*cenaList={cenaEnergie,cenaNT,cenaVT,cenaPrikon,cenaBaterieJ,cenaSystem};*)];
vystup={prubehNabijeni,cenaEnergie,cenaNT,cenaVT,cenaPrikon,cenaBaterieJ,penaleEnd,cashFlow,N[NPV,10],usetreno}];

Clear[mycena,minimum,PmaxOdebirany,BaterieMin]
cenaJoule=7000*25.92/(14000*3600)*1
mycena[PmaxOdber1_?NumericQ,Baterie_?NumericQ]:=cena[{PmaxOdber1,Baterie,prumVsedZimDen,False,0.05}][[9]]
(*minimum=NMinimize[{mycena[PmaxOdber1,Baterie],200000>PmaxOdber1>100000,10000000000>Baterie>0},{PmaxOdber1,Baterie}]*)
minimum=FindMinimum[{mycena[PmaxOdber1,Baterie]},{{PmaxOdber1,200000.},{Baterie,10000000}},Method->"PrincipalAxis"]
minimumHodnoty=minimum[[2]];

PmaxOdebirany=PmaxOdber1/.minimumHodnoty;
(*BaterieMin=PmaxUvazovany*10*hodina;*)
BaterieMin = (Baterie /. minimumHodnoty);
minimumPrubeh=cena[{PmaxOdebirany,BaterieMin,prumVsedZimDen,False,0.05}][[1]];
cenaRozbor=cena[{PmaxOdebirany,BaterieMin,prumVsedZimDen,False,0.05}]


(* samotny graf *)

sablona2=Boole[RotateLeft[sablonaDen,3]]; (* posunuti vysledu na puvodni cas*)
tarify=DateListPlot[{Transpose[{denCtvrthodiny,sablona2*1000000}]},Filling->Axis,PlotLabel->"Baterie",FrameLabel->kapacita [kWh],GridLines->Automatic,FillingStyle->Automatic,PlotStyle->Opacity[0.01],PlotLegends->"Nizsi tarif"];
pl1=DateListPlot[{Transpose[{denCtvrthodiny,RotateLeft[minimumPrubeh[[All,2]]/3600000,3]}]},Joined-> True,PlotLabel->"Baterie",FrameLabel->kapacita [kWh],GridLines->Automatic];
pl2=DateListPlot[{Transpose[{denCtvrthodiny,RotateLeft[Differences[Join[{minimumPrubeh[[1,3]]},minimumPrubeh[[All,3]]]/ctvrthodina],4]}],Transpose[{denCtvrthodiny,RotateLeft[prumVsedZimDen,3]}],Transpose[{denCtvrthodiny,RotateLeft[ConstantArray[PmaxOdebirany,96],3]}]},PlotLabel->"Nižší tarif ",AxesLabel->{t[15 min],e[W]},GridLines->Automatic,PlotLegends->Placed[{"Odebíraný výkon v NT ","Průběh bez baterie","Max odebíraný výkon"},Bottom],FrameLabel->příkon  [W]];
pl3=DateListPlot[{Transpose[{denCtvrthodiny,RotateLeft[Differences[Join[{minimumPrubeh[[1,4]]},minimumPrubeh[[All,4]]]/ctvrthodina],4]}],Transpose[{denCtvrthodiny,RotateLeft[prumVsedZimDen,3]}],Transpose[{denCtvrthodiny,RotateLeft[ConstantArray[PmaxOdebirany,96],3]}]},Joined-> True,PlotLabel->"Vyšší tarif ",AxesLabel->{t[15 min],e[W]},GridLines->Automatic,PlotLegends->Placed[{"Odebíraný výkon v VT ","Průběh bez baterie","Max odebíraný výkon"},Bottom],FrameLabel->příkon [W]];

pl4=ListPlot[minimumPrubeh[[All,5]],Joined-> True,PlotLabel->"Penale"];

Show[pl1,tarify]
Show[pl2,tarify]
Show[pl3,tarify]

Print["Cena za NT : ",cenaRozbor[[3]]]
Print["Cena za VT : ",cenaRozbor[[4]]]
Print["Cena za jistic : ",cenaRozbor[[5]]]
Print["Cena za baterii : ",cenaRozbor[[6]]]
Print["Cena celkem : ",cenaRozbor[[2]]]

(*vykresleni ceny pri zmene kapacitz baterie a delsi doba trvani pro vykresleni*)


(*grafMinim2=Plot3D[cena[{maxPrikon,Baterie,prumVsedZimDen,False,0.05}][[9]],{maxPrikon,150000,200000},{Baterie,10000,1000000000},PlotRange->{Automatic,0}]*)

(*Plot3D[cena[{maxPrikon,Baterie,prumVsedZimDen,False,0.03}][[9]],{maxPrikon,150000,200000},{Baterie,100000,1000000000},PlotRange->{Automatic,30000}]*)

(*Show[grafMinim2,AxesLabel->{Style["Max. prikon [W]",Bold,Black],Style["Kapacita baterie[J]",Bold,Black],Style["NPV[Kc]",Bold,Black]}]
Show[grafMinim2,PlotRange->All]*)

(*prubeh bez baterie *)
BezBaterieCena=cena[{PmaxOdber,0,prumVsedZimDen,True,0.05}];
Print["Cena za NT : ",BezBaterieCena[[3]]]
Print["Cena za VT : ",BezBaterieCena[[4]]]
Print["Cena za jistic : ",BezBaterieCena[[5]]]
Print["Cena za baterii : ",BezBaterieCena[[6]]]
Print["Cena za bateriovy system : ",BezBaterieCena[[7]]]
Print["Cena celkem : ",BezBaterieCena[[2]]-BezBaterieCena[[7]]]


p1=ListPlot[{BezBaterieCena[[1,All]][[All,2]]/3600,prumVsedZimDen},Joined-> True,PlotLabel->"Baterie",AxesLabel->{t[15 min],kapacita[W]},GridLines->Automatic];
p2=ListPlot[{Join[{0},Differences[BezBaterieCena[[1,All]][[All,3]]]/ctvrthodina],prumVsedZimDen},Joined-> True,PlotLabel->"nizssi tarif [J]",AxesLabel->{t[15 min],e[W]},GridLines->Automatic(*PlotLegends->{"Odebirany vykon v NT ","Prubeh bez baterie","MaxOdebirana energie"}*)];
p3=ListPlot[{Join[{0},Differences[BezBaterieCena[[1,All]][[All,4]]]/ctvrthodina],prumVsedZimDen,Join[{0},Differences[minimumPrubeh[[All,4]]]/ctvrthodina]},Joined-> True,PlotLabel->"Vyssi tarif [J] ",AxesLabel->{t[15 min],e[W]},GridLines->Automatic(*,PlotLegends->{"Odebirany vykon v VT","Prubeh bez baterie","MaxOdebirana energie"}*)];
p4=ListPlot[BezBaterieCena[[1,All]][[All,5]],Joined-> True,PlotLabel->"Penale"];
Grid[{{"Prubehy bez baterie",""},{p1,p2},{p3,p4}},Frame->All]

(*porovnavani grafu*)

ListPlot[{minimumPrubeh[[All,3]],BezBaterieCena[[1,All]][[All,3]]},Joined-> True,PlotLabel->"nizssi tarif [J]",AxesLabel->{t[15 min],energie[J]},GridLines->Automatic,PlotLegends->{"Baterie ","Prubeh bez baterie"}]
ListPlot[{minimumPrubeh[[All,4]]/ctvrthodina,BezBaterieCena[[1,All]][[All,4]]/ctvrthodina},Joined-> True,PlotLabel->"Vyssi tarif [J] ",AxesLabel->{t[15 min],e[W]},GridLines->Automatic,PlotLegends->{"Baterie","Prubeh bez baterie"}]

Porovnani jednotlivych dnu

dobaZivotaBaterie=10;
UsetrenoRocne=cenaRozbor[[10]];
(*NPV*)
diskont=0.05;
baterieInvestice=cenaJoule*BaterieMin;
cashFlow=Cashflow[{Join[{-baterieInvestice},ConstantArray[UsetrenoRocne,dobaZivotaBaterie]]}[[1]]]; (* cashflow  za dobu projektu*)
NPV=TimeValue[cashFlow,diskont,0];
(*IRR*)
IRR=FindRoot[ TimeValue[cashFlow,r,0]==0,{r,.05}];
Print["NPV je : ",NPV," KC a IRR je : ",Round[((r/.IRR)*100),0.05]," pro dobu zivota : ",dobaZivotaBaterie," let"]

cashFlowMan[investice_,doba_]:=Cashflow[{Join[{-investice},ConstantArray[UsetrenoRocne,doba]]}[[1]]]

cashFlowMan[investice_,doba_]:=Cashflow[{Join[{-investice},ConstantArray[UsetrenoRocne,doba]]}[[1]]]
Manipulate[ListPlot[Table[TimeValue[cashFlowMan[investice,x],diskont,0],{x,0,nZivota,1}],AxesLabel->{"cas [roky]","kumulovane disk. CF [Kc]"}],{{nZivota,10,"Doba zivota"},1,20,Appearance->Labeled},{{diskont,0.05,"diskont"},0,0.20},{{investice,0,"investice"},0,800000}]

(*citlivostni analyza*)
cenaJoule=7000*25.92/(14000*3600);
citAn=ConstantArray[0,5]; (*uloziste pro jednotlive cenove varianty*)
snizeniCeny=cenaJoule/5;
cenaJoule=cenaJoule+snizeniCeny;
i=1
For[k=1,k<6,k++,(cenaJoule=cenaJoule-snizeniCeny;minimum=FindMinimum[{mycena[PmaxOdber1,Baterie]},{{PmaxOdber1,200000.},{Baterie,10000000.}},Method->"PrincipalAxis"];minimumHodnoty=minimum[[2]];
Print[minimumHodnoty];PmaxOdebirany=PmaxOdber1/.minimumHodnoty;
BaterieMin = (Baterie /. minimumHodnoty);citAn[[k]]=cena[{PmaxOdebirany,BaterieMin,prumVsedZimDen,False,0.05}])]
cenaJoule=7000*25.92/(14000*3600);

BaterieAll=ConstantArray[0,5];
NTAll=ConstantArray[0,5];
VTAll=ConstantArray[0,5];
cashFlowAll=citAn[[All,8]];
NPVAll=citAn[[All,9]];
IRRAll=Table[FindRoot[ TimeValue[cashFlowAll[[x,All]],r,0]==0,{r,.05}],{x,1,5}];

For[i=1,i<6,i++,BaterieAll[[i]]=citAn[[All,1]][[i,All]][[All,2]]/3600000];
For[i=1,i<6,i++,NTAll[[i]]=Differences[citAn[[All,1]][[i,All]][[All,3]]]/ctvrthodina];
For[i=1,i<6,i++,VTAll[[i]]=Differences[citAn[[All,1]][[i,All]][[All,4]]]/ctvrthodina];
For[i=1,i<6,i++,cashFlowAll[[i]]=citAn[[i]][[8]]];


tarify=DateListPlot[{Transpose[{denCtvrthodiny,sablona2*1000000}]},Filling->Axis,PlotLabel->"Baterie",FrameLabel->kapacita [kWh],GridLines->Automatic,FillingStyle->Automatic,PlotStyle->Opacity[0.01]];
prubehNT=Table[Transpose[{denCtvrthodiny,RotateLeft[Join[{NTAll[[1,3]]},NTAll[[x,All]]],4]}],{x,1,5}];
prubehVT=Table[Transpose[{denCtvrthodiny,RotateLeft[Join[{VTAll[[1,1]]},VTAll[[x,All]]],4]}],{x,1,5}];
prubehPrumDen=Transpose[{denCtvrthodiny,RotateLeft[prumVsedZimDen,3]}];

pl1=DateListPlot[Table[Transpose[{denCtvrthodiny,RotateLeft[BaterieAll[[x,All]],3]}],{x,1,5}],Joined-> True,PlotLabel->"Baterie",FrameLabel->kapacita [kWh],GridLines->Automatic,PlotLegends->Placed[{"Plná cena baterie","4/5 ceny baterie","3/5 ceny baterie","2/5 ceny baterie","1/5 ceny baterie"},Bottom],PlotRange->All];


pl2=DateListPlot[Append[prubehNT,prubehPrumDen],Transpose[{denCtvrthodiny,RotateLeft[prumVsedZimDen,3]}],PlotLabel->"Nižší tarif ",AxesLabel->{t[15 min],e[W]},GridLines->Automatic,PlotLegends->Placed[{"Plná cena baterie","4/5 ceny baterie","3/5 ceny baterie","2/5 ceny baterie","1/5 ceny baterie","prbeh celeho dne bez baterie"},Bottom],FrameLabel->příkon  [W],PlotRange->All];

pl3=DateListPlot[Append[prubehVT,prubehPrumDen],PlotLabel->"Vyssi tarif ",AxesLabel->{t[15 min],e[W]},GridLines->Automatic,PlotLegends->Placed[{"Plna cena baterie","4/5 ceny baterie","3/5 ceny baterie","2/5 ceny baterie","1/5 ceny baterie","prubeh celeho dne bez baterie"},Bottom],FrameLabel->prikon  [W],PlotRange->All];
pl4=DateListPlot[Transpose[{denCtvrthodiny,RotateLeft[prumVsedZimDen,3]}],PlotLegends->Placed[{"prubeh celeho dne bez baterie"},Bottom]];

ListPlot[BaterieAll,Joined->True,PlotLegends->Automatic,PlotRange->All,GridLines->Automatic];
ListPlot[NTAll,Joined->True,PlotLegends->Automatic,PlotRange->All,GridLines->Automatic];
ListPlot[VTAll,Joined->True];

Show[pl1,tarify]
Show[pl2,tarify]
Show[pl3,tarify]

IRR2=Table[FindRoot[ TimeValue[cashFlowAll[[z]],r,0]==0,{r,.05}],{z,1,5}];
investice=Table[cashFlowAll[[z]][[1,1]],{z,1,5}];
usetreno=Table[cashFlowAll[[z]][[1,2]],{z,1,5}];

Grid[{{"Cena baterie", "Plna cena","4/5","3/5","2/5","1/5"},Prepend[BaterieAll[[All,1]],"kapacita baterie [kWh]"],Prepend[-NPVAll,"NPV [Kc]"],Prepend[(r/.IRR2)*100,"IRR [%]"],Prepend[-investice,"investice do baterioveho systemu [Kc]"],Prepend[usetreno,"usetreno za rok [Kc]"]},Frame->All]