Quiet@Remove["Global`*"];
$HistoryLength=2;
SetDirectory[NotebookDirectory[]];
SetAttributes[exp,{HoldAll,Listable}];
exp[co_]:=Module[{a,graf},a=ToString[Unevaluated[co]];graf:=Evaluate[co];Export[Evaluate[a<>".png"],graf,ImageResolution->300]];

Quiet@Remove["Global`*"];
T1=70;
T2=40;
T3=5;

Δ1=T1-T3;
Δ2=T2-T4;
m1=1;
m2=1;
c=4186;

reseni=Solve[{P==m1*c*(T1-T2),m1*c*T1+m2*c*T3==m1*c*T2+m2*c*T4},{T4,P}][[1]]




Δ1=T1-T3;
Δ2=T2-T4;
m1=1;
m2=1;
c=4186;

ksko=kS/.Solve[P==kS*((Δ1-Δ2)/(Log[((Δ1)/(Δ2))]))/.reseni,kS][[1]]//N;
Print["kS= ",ksko," W/K"]
Clear[T2];

m1=1.5;
m2=2;
reseni2=FindRoot[{P==m1*c*(T1-T2),m1*c*T1+m2*c*T3==m1*c*T2+m2*c*T4, P==ksko*((Δ1-Δ2)/(Log[((Δ1)/(Δ2))]))},{{T4,20},{P,125000},{T2,30}}]

Quiet@Remove["Global`*"];
T1=70;
T2=40;
T3=5;

Δ1=T1-T3;
Δ2=T2-T4;
m1=1;
m2=1;
c=4186;

reseni=Solve[{P==m1*c*(T1-T2),m1*c*T1+m2*c*T3==m1*c*T2+m2*c*T4},{T4,P}][[1]]




Δ1=T1-T3;
Δ2=T2-T4;
m1=1;
m2=1;
c=4186;

ksko=kS/.Solve[P==kS*((Δ1+Δ2)/2)/.reseni,kS][[1]]//N;
Print["kS= ",ksko," W/K"]
Clear[T2];

m1=1.5;
m2=2;
reseni2=FindRoot[{P==m1*c*(T1-T2),m1*c*T1+m2*c*T3==m1*c*T2+m2*c*T4, P==ksko*((Δ1+Δ2)/2)},{{T4,20},{P,125000},{T2,30}}]

Quiet@Remove["Global`*"];
T1=100.;
T5=20;
m1=0.5;
m2=0.5;
cw=4186;
c=1000;
Δ11=T1-T4;
Δ12=T2-T3;
Δ21=T4-T6;
Δ22=T3-T5;
kS=500;
rce[mw_]={m1*c*T1+mw*cw*T3==m1*c*T2+mw*cw*T4,
mw*cw*T4+m2*c*T5==mw*cw*T3+m2*c*T6,
P1==kS*((Δ11-Δ12)/(Log[((Δ11)/(Δ12))])),P1==m1*c*(T1-T2),
P2==kS*((Δ21-Δ22)/(Log[((Δ21)/(Δ22))])),P2==mw*cw*(T4-T3)
};
rceLin[mw_]={m1*c*T1+mw*cw*T3==m1*c*T2+mw*cw*T4,
mw*cw*T4+m2*c*T5==mw*cw*T3+m2*c*T6,
P1==kS*((Δ11+Δ12)/2),P1==m1*c*(T1-T2),
P2==kS*((Δ21+Δ22)/2),P2==mw*cw*(T4-T3)
};

(*Length@(Cases[rceLin[0.5],_Symbol,{0,∞}]//Union)
Length@rceLin[0.5]*)
ClearAll[vyres];
vyres[mw_]:=Module[{resLin,obory,res},
resLin=Solve[rceLin[mw]][[1]];
obory=resLin/.Rule[a_,b_]:>{a,b};
res=Quiet@FindRoot[rce[mw],obory,MaxIterations->10,AccuracyGoal->Infinity,PrecisionGoal->8]

];
vykon[mw_]:=P2/.vyres[mw];
mwMin=0.001;
mwMax=2;
Nmw=60;
mwcka=mwMin*((((mwMax/mwMin)))^((#-1)/Nmw))&/@Range[Nmw+1];
dataP={#,vykon[#]}&/@mwcka;
ListLogLogPlot[Take[dataP,All]]

ClearAll[vyres,vykon];
vyres[mw_]:=Module[{resLin,obory,res,proMin},
resLin=Solve[rceLin[mw]][[1]];
obory=resLin/.Rule[a_,b_]:>{a,b};
proMin=Plus@@(rce[mw]/.Equal[a_,b_]:>(((Abs[a]-Abs[b]))^2));
Quiet@FindMinimum[proMin,obory][[2]]

];
vykon[mw_]:=P2/.vyres[mw];
mwMin=0.001;
mwMax=2;
Nmw=60;
mwcka=mwMin*((((mwMax/mwMin)))^((#-1)/Nmw))&/@Range[Nmw+1];
dataP={#,vykon[#]}&/@mwcka;
ListLogLogPlot[Take[dataP,All]]

(*ulohy za body z IAP*)
(*spravit solver, aby to neklesalo*)
(*kS ekvivalentniho jednoho vymeniku jako funkce mw, graf*)